getline(cin.name)被跳过 [英] getline(cin.name) gets skipped
问题描述
我从C ++中的一个函数调用函数,该函数的行 getline(cin,name)
,其中name是一个字符串。第一次通过循环,程序不会等待输入。它将在所有其他循环中通过。任何想法为什么?
void getName(string& name)
{
int nameLen;
do {
cout<< 输入居民的姓氏。 << endl<< endl
<< 不应该有任何空间和不超过15
<< 名字中的字符。 << ENDL;
getline(cin,name);
cout<< ENDL;
nameLen = name.length(); //将len设置为输入的字符数
cout<< 最后<<名称<< ENDL;
}
while(nameLen< LastNameLength);
return;
确保没有自上次读取cin以来的剩余内容,例如:
在程序的早期版本中:
int number;
cin>>数;
您输入的内容:
<$ p $
$ p
$ b
稍后在程序中:
getline(cin,name);
和 getline
似乎不会被调用,而是从上次输入时收集换行符,因为当您使用 cin>>
时,它会留下新行。
I call a function from a function in C++ that has the line getline(cin,name)
where name is a string. the first time through the loop, the program does not wait for input. It will on all other passes through the loop. Any ideas on why?
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;
}
Make sure there isn't left overs since the last time you read something from cin, like:
In an earlier point in your program:
int number;
cin >> number;
The input you give:
5
Later in the program:
getline(cin,name);
and getline
will seem to not be called, but rather it collected the newline from the last time you took input because when you use cin >>
it leaves new lines.
这篇关于getline(cin.name)被跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!