C++ getline 无法正常工作?可能是什么原因?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6649852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:28:24  来源:igfitidea点击:

getline not working properly ? What could be the reasons?

c++visual-c++getline

提问by Suhail Gupta

Possible Duplicate:
getline not asking for input?

可能重复:
getline 不要求输入?

There is some unique thing happening in my program. Here are some set of commands :

我的程序中发生了一些独特的事情。以下是一些命令集:

 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 );

When i try to run this snippet along with the whole code.The output program works like :

当我尝试将这个片段与整个代码一起运行时。输出程序的工作原理如下:

enter image description here

在此处输入图片说明

output:

输出:

Enter the full name of student:
Age: 20

Father's Name:
Permanent Address: xyz

If you notice ,the program didn't ask me the full name and went on directly to ask me the age.Then it skips the father's name also and asks the permanent address. What could be the reason for this ?

如果你注意到,程序并没有问我全名,而是直接问我年龄。然后它也跳过了父亲的名字并询问了永久地址。 这可能是什么原因?

It is difficult for me to post the whole code because it is too large.

我很难发布整个代码,因为它太大了。

回答by Alok Save

Since you have not posted any code. I am going to take a guess.

由于您尚未发布任何代码。我来猜一猜。

A common problem while using getlinewith cinis getlinedoes not ignore leading whitespace characters.

使用getlinewith 时的一个常见问题cingetline不会忽略前导空格字符。

If getline is used after cin >>, the getline()sees this newline character as leading whitespace, and it just stops reading any further.

如果函数getline之后使用cin >>时,getline()看到这个换行符作为前导空格,它只是停止读取任何进一步。

How to resolve it?

如何解决?

Call cin.ignore()before calling getline()

先打电话cin.ignore()再打电话getline()

Or

或者

make a dummy call getline()to consume the trailing newline character from the cin >>

做一个虚拟调用getline()来消耗尾随的换行符cin >>

回答by Sander De Dycker

The problem is that you are mixing getlinewith cin >>input.

问题是您正在getlinecin >>输入混合。

When you do cin >> age;, that gets the age from the input stream, but it leaves whitespace on the stream. Specifically, it will leave a newline on the input stream, which then gets read by the next getlinecall as an empty line.

当你这样做时cin >> age;,它会从输入流中获取年龄,但它会在流上留下空白。具体来说,它将在输入流上留下一个换行符,然后在下一次getline调用时将其作为空行读取。

The solution is to only use getlinefor getting input, and then parsing the line for the information you need.

解决方案是仅getline用于获取输入,然后解析您需要的信息的行。

Or to fix your code, you could do the following eg. (you'll still have to add error checking code yourself) :

或者要修复您的代码,您可以执行以下操作,例如。(您仍然需要自己添加错误检查代码):

cout << "Enter the full name of student: ";  // cin name
getline( cin , fullName );

cout << "\nAge: ";  // cin age
int age;
{
    std::string line;
    getline(cin, line);
    std::istringstream ss(line);
    ss >> age;
}

cout << "\nFather's Name: ";  // cin father name
getline( cin , fatherName );

cout << "\nPermanent Address: ";  // cin permanent address
getline( cin , permanentAddress );

回答by lccarrasco

after the line cin >> age ;there is still the newline character \n(because you pressed enter to input the value) in the input buffer, to fix this you add a line with cin.ignore();after reading the int.

在该行之后,输入缓冲区中cin >> age ;仍然有换行符\n(因为您按 Enter 输入值),为了解决这个问题,您cin.ignore();在读取 int 后添加一行。