Suh*_*pta 19 c++ getline visual-c++
可能重复:
getline没有要求输入?
在我的程序中发生了一些独特的事情.以下是一些命令:
cout << "Enter the full name of student: "; // cin name
getline( cin , fullName );
cout << "\nAge: "; // cin age
int age;
cin >> age ;
cout << "\nFather's Name: "; // cin father name
getline( cin , fatherName );
cout << "\nPermanent Address: "; // cin permanent address
getline( cin , permanentAddress );
Run Code Online (Sandbox Code Playgroud)
当我尝试将此代码段与整个代码一起运行时.输出程序的工作方式如下:

Enter the full name of student:
Age: 20
Father's Name:
Permanent Address: xyz
Run Code Online (Sandbox Code Playgroud)
如果你注意到,该程序没有问我全名,并直接问我年龄.然后它也跳过父亲的名字并询问永久地址. 这可能是什么原因?
我很难发布整个代码,因为它太大了.
Alo*_*ave 58
由于您尚未发布任何代码.我要猜一猜.
使用getlinewith时的常见问题cin是getline不会忽略前导空格字符.
如果函数getline之后使用cin >>时,getline()看到这个换行符作为前导空格,它只是停止读取任何进一步.
怎么解决?
打电话cin.ignore()前打电话getline()
要么
进行虚拟调用getline()以消耗来自的尾随换行符cin >>