我从 C++ 中的一个函数调用一个函数,该函数具有 getline(cin,name)
行,其中 name 是一个字符串。第一次通过循环时,程序不等待输入。它将通过循环的所有其他传递。有什么想法吗?
void getName (string& name)
{
int nameLen;
do{
cout << "Enter the last Name of the resident." << endl << endl
<< "There should not be any spaces and no more than 15"
<< " characters in the name." << endl;
getline(cin,name);
cout << endl;
nameLen = name.length();// set len to number of characters input
cout << "last" << name << endl;
}
while (nameLen < LastNameLength);
return;
}
最佳答案
确保自上次从 cin 读取内容后没有遗漏,例如:
在你的程序的前面一点:
int number;
cin >> number;
您提供的输入:
5
程序后面:
getline(cin,name);
和 getline
似乎不会被调用,而是它收集了您上次输入时的换行符,因为当您使用 cin >>
时它会留下新行.
关于c++ - getline(cin.name) 被跳过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8236645/