我在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
中读取。 - Seth Carnegieendl
。endl
会冲刷输出流,这是不必要的低效操作。 - Robᵩ