C++ getline(), the second run of this function doesn't work

时间:2021-05-06 19:48:22

I can't figure out what's my simple IO problem:

我无法弄清楚我的简单IO问题是什么:

this is the code where I do the IO:

这是我执行IO的代码:

cout << "Enter an Employee name: ";
getline(cin, empName);
cout << "Employee Position: "  ;
cin >> empPos;
cout << "Enter the Number of Years of Experience: ";
cin >> numOfExp;
cout << "Enter the deprtment Number: ";
cin >> deptNum;

and here is my wrong output: the first time that the name is read everything is fine but the second time it looks like something is automatically is passed into the name without asking the user to input anything fot the name.

这是我错误的输出:第一次读取名称一切都很好,但第二次看起来像是自动传递到名称而不要求用户输入名称的任何内容。

Here is my output:

这是我的输出:

Name:               Unknown
Department Number:          0
Employee Position:          E
Years of Experience:        0
Salary:                     0
Total Number of Employees:  1
Enter an Employee name: arasd d
Employee Position: s
Enter the Number of Years of Experience: 12
Enter the deprtment Number: 12
Name:                       arasd d
Department Number:          12
Employee Position:          s
Years of Experience:        12
Salary:                     0
Total Number of Employees:  1
Enter an Employee name: Employee Position:

As you see the last line is the problem; any idea how to fix this?

如你所见,最后一行是问题;任何想法如何解决这个问题?

1 个解决方案

#1


1  

The problem is probably that the last thing you read before the std::getline() is an integer (or something else using operator>>(). The formatted input operators stop reading when the first character not matching their format is encountered. For example, for an integer reading stops the monent a non-digit is entered (except for a leading sign). Thus, after reading an integer the newline character used to indicate that the input is done is still in the input buffer.

问题可能是你在std :: getline()之前读到的最后一件事是一个整数(或其他使用operator >>()的东西。格式化的输入操作符在遇到与其格式不匹配的第一个字符时停止读取。例如,对于整数读取停止monent输入一个非数字(前导符号除外)。因此,在读取整数后,用于指示输入完成的换行符仍在输入缓冲区中。

To deal with the stuck newline you can just skip any leading whitespace before calking std::getline():

要处理卡住的换行符,您可以在调用std :: getline()之前跳过任何前导空格:

if (std::getline(std::cin >> std::ws, name)) {
    ...
}

BTW, there is never a situation where you don't want to check user-input! User-input always needs checking, even in the most trivial programs where the input is assumed to be right! It help with locating the actual problems dramatically. Given your output it looks as if tve input actually didn't match what is being read...

顺便说一句,从来没有你不想检查用户输入的情况!用户输入始终需要检查,即使在假设输入正确的最简单的程序中!它有助于显着定位实际问题。鉴于您的输出,看起来好像tve输入实际上与正在读取的内容不匹配...

#1


1  

The problem is probably that the last thing you read before the std::getline() is an integer (or something else using operator>>(). The formatted input operators stop reading when the first character not matching their format is encountered. For example, for an integer reading stops the monent a non-digit is entered (except for a leading sign). Thus, after reading an integer the newline character used to indicate that the input is done is still in the input buffer.

问题可能是你在std :: getline()之前读到的最后一件事是一个整数(或其他使用operator >>()的东西。格式化的输入操作符在遇到与其格式不匹配的第一个字符时停止读取。例如,对于整数读取停止monent输入一个非数字(前导符号除外)。因此,在读取整数后,用于指示输入完成的换行符仍在输入缓冲区中。

To deal with the stuck newline you can just skip any leading whitespace before calking std::getline():

要处理卡住的换行符,您可以在调用std :: getline()之前跳过任何前导空格:

if (std::getline(std::cin >> std::ws, name)) {
    ...
}

BTW, there is never a situation where you don't want to check user-input! User-input always needs checking, even in the most trivial programs where the input is assumed to be right! It help with locating the actual problems dramatically. Given your output it looks as if tve input actually didn't match what is being read...

顺便说一句,从来没有你不想检查用户输入的情况!用户输入始终需要检查,即使在假设输入正确的最简单的程序中!它有助于显着定位实际问题。鉴于您的输出,看起来好像tve输入实际上与正在读取的内容不匹配...