当前位置:网站首页>Solution of inputting whole line string after inputting integer
Solution of inputting whole line string after inputting integer
2022-07-26 10:03:00 【Alan_ Lowe】
The solution of entering an integer and then an entire line of string
When the input is an integer , Then when you need to input the whole line of string later , It will be very troublesome to deal with , And I have met several times in the competition , This time, make a good arrangement of the methods .
First, let's talk about my own mistakes , When inputting as follows :
2
4
hello worl d
hello cpp ++a
The input content is two integers , Then two whole lines of string , This situation , If we use string and cin Under normal circumstances, it is necessary to use getline Of :
signed main(){
int a,b;
string t;
string s;
cin>>a>>b;
getline(cin,s);
getline(cin,t);
cout<<a<<" "<<b<<"\n";
cout<<s<<"\n"<<t;
return 0;
}
In this case, the output is as follows :
2 4
hello worl d
We will find that 4 The following carriage return will be used as the end sign of the first string . So we have to find a way to get rid of this carriage return , Then I use it getchar():
signed main(){
int a,b;
string t;
string s;
cin>>a>>b;
getchar();
getline(cin,s);
getline(cin,t);
cout<<a<<" "<<b<<"\n";
cout<<s<<"\n"<<t;
return 0;
}
But sometimes there will be some problems , Sometimes something goes wrong , This is also a problem that has been bothering me .
What if this problem is solved , It's better to use cin.get():
signed main(){
int a,b;
string t;
string s;
cin>>a>>b;
cin.get();
getline(cin,s);
getline(cin,t);
cout<<a<<" "<<b<<"\n";
cout<<s<<"\n"<<t;
return 0;
}
Output :
2 4
hello worl d
hello cpp ++a
边栏推荐
- Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market
- 服务发现原理分析与源码解读
- protobuf的基本用法
- Study notes of the second week of sophomore year
- Study notes of the fifth week of sophomore year
- Wu Enda linear regression of machine learning
- AR model in MATLAB for short-term traffic flow prediction
- JS continuous assignment operation
- Show default image when wechat applet image cannot be displayed
- Sqoop [put it into practice 02] sqoop latest version full database import + data filtering + field type support description and example code (query parameter and field type forced conversion)
猜你喜欢
Sqoop [environment setup 01] CentOS Linux release 7.5 installation configuration sqoop-1.4.7 resolve warnings and verify (attach sqoop 1 + sqoop 2 Latest installation package +mysql driver package res
Docker configuring MySQL Cluster
Principle analysis and source code interpretation of service discovery
Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
2021年山东省中职组“网络空间安全”B模块windows渗透(解析)
点赞,《新程序员》电子书限时免费领啦!
Solve proxyerror: CONDA cannot proceed due to an error in your proxy configuration
Logical architecture of MySQL
Vs Code configures go locale and successfully installs go related plug-ins in vscode problem: Tools failed to install
服务发现原理分析与源码解读
随机推荐
在.NET 6.0中配置WebHostBuilder
Sqoop [environment setup 01] CentOS Linux release 7.5 installation configuration sqoop-1.4.7 resolve warnings and verify (attach sqoop 1 + sqoop 2 Latest installation package +mysql driver package res
Usage of the formatter attribute of El table
Leetcode 504. 七进制数
Getting started with SQL - combined tables
Interview shock 68: why does TCP need three handshakes?
苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收
Use of selectors
时间序列异常检测
2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
分布式网络通信框架:本地服务怎么发布成RPC服务
[fluorescent character effect]
R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)
The use of MySQL in nodejs
Show default image when wechat applet image cannot be displayed
QT handy notes (III) use qtcharts to draw a line chart in VS
数通基础-网络基础知识
Gauss elimination for solving XOR linear equations
在Blazor 中自定义权限验证
Node memory overflow and V8 garbage collection mechanism