当前位置:网站首页>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
边栏推荐
- Force deduction DFS
- Study notes of the first week of sophomore year
- IIS error prompt after installing Serv-U: hresult:0x80070020
- 2022 zhongkepan cloud - server internal information acquisition and analysis flag
- A new paradigm of distributed deep learning programming: Global tensor
- 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
- Mysql5.7.25 master-slave replication (one-way)
- Draw arrows with openlayer
- 在.NET 6.0中配置WebHostBuilder
- 新公链Aptos何以拉满市场期待值?
猜你喜欢

【有奖提问】向图灵奖得主、贝叶斯网络之父 Judea Pearl 提问啦

AirTest
![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](/img/8e/265af6b20f79b21c3eadcd70cfbdf7.png)
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

2022年中科磐云——服务器内部信息获取 解析flag
![[datawhale] [machine learning] Diabetes genetic risk detection challenge](/img/98/7981af7948feb73168e5200b3dfac9.png)
[datawhale] [machine learning] Diabetes genetic risk detection challenge

Server and client dual authentication (2)

2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)

regular expression

数通基础-网络基础知识

Customize permission validation in blazor
随机推荐
Tableviewcell highly adaptive
万字详解“用知识图谱驱动企业业绩增长”
Basic knowledge of website design
Basic usage of protobuf
Nodejs service background execution (forever)
SSG框架Gatsby访问数据库,并显示到页面上
In the same CONDA environment, install pytroch first and then tensorflow
Server memory failure prediction can actually do this!
PMM (percona monitoring and management) installation record
spolicy请求案例
分布式网络通信框架:本地服务怎么发布成RPC服务
Gauss elimination for solving XOR linear equations
Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
数通基础-STP原理
Interview shock 68: why does TCP need three handshakes?
面试突击68:为什么 TCP 需要 3 次握手?
Gauss elimination solves the inverse of matrix (Gauss)
Principle analysis and source code interpretation of service discovery
Wechat H5 payment on WAP, for non wechat browsers
In Net 6.0