【发布时间】:2012-01-04 10:01:37
【问题描述】:
我从 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;
}
【问题讨论】:
-
可能在调用函数之前还有一些数据等待从
stdin读取。 -
请提供一个小而完整的程序来演示这个问题。您描述的问题可能是由您没有向我们展示的某些代码引起的。
-
离题:当你的意思是
'\n'时,请不要使用endl。endl刷新不必要的低效输出流。 -
@Rob 除非您在可能发生崩溃时打印调试语句。