我无法弄清楚什么是我的简单的IO问题:C++函数getline(),这个函数的第二轮不工作
这是我做的代码的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;
这里是我的错误输出: 第一次读取名称一切正常,但第二次它看起来像是自动的东西被传入名称而不要求用户输入任何名称。
这里是我的输出:
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:
正如你所看到的最后一行的问题; 任何想法如何解决这个问题?
'getline'在停止阅读时的行为有所不同。 – chris
不要混合'getline'和'>>'。此外,您完全忘记检查任何输入操作的返回值。这基本上是一场灾难。 –
我不想在这一点做任何错误检查,我只是想确保程序运行,假设用户输入正确的输入。 – EasyQuestions