--我遇到了getline()的问题。我已经搜索过它并检查了多个页面,但是那些答案并不适合像我这样的新手。下面是代码:
#include <iostream> #include <string.h> using namespace std; int main(){ string n,s,a,ad,e; cout<<"Your name: "<<endl; cin>>n; cout<<"Hello, "<<n<<endl; cout<<"Your surname: "<<endl; cin>>s; cout<<"Your age: "<<endl; cin>>a; cout<<"Your address: "<<endl; getline(cin,ad); cout<<"Your email: "<<endl; cin>>e; cout<<"Done."<<endl; cout<<"Name: "<<n<<" "<<s<<endl; cout<<"Age: "<<a<<endl; cout<<"Address: "<<ad<<endl; cout<<"Email: "<<e<<endl; },问题是,每当我启动程序时,getline(cin,ad)就会跳过。它是这样的:如您所见,Your name: name Hello, name Your surname: surname Your age: age Your address: Your email:不能输入我的地址,因为getline()被跳过了。我怎么才能简单地解决这个问题呢?
我解决了问题,我只是应该写:
cin>>ad;
getline(cin, ad);仅此而已。谢谢你的贡献!
发布于 2021-10-03 12:27:31
不要把getline和>>混在一起。
用户将始终在每次输入后按键回车。
通常,使用getline获取用户输入。如果以后需要进一步处理,请使用字符串流或标准字符串到整数函数或字符串到浮动函数之一。
在这种情况下,您所要求的一切都是字符串,因此,除了getline之外,使用任何东西都没有任何意义。
(尤其是在提到名字时。)
https://stackoverflow.com/questions/69424762
复制相似问题