【发布时间】:2012-03-30 03:40:44
【问题描述】:
自从我编写 C++ 代码以来已经有一段时间了,我忘记了在收集字符串输入时发生的一件烦人的事情。基本上,如果这循环回来,比如说如果你使用负数,那么它会在第二轮跳过员工姓名行中的 cin。我记得之前遇到过这个问题,并且必须在输入字符串之前或之后清除或执行此类操作。请帮忙!
PS 另外,任何人都可以帮助我在下面的正确循环中获得额外帮助。如何检查字符串输入中的值以确保它们输入了值?
#include <string>
#include <iostream>
#include "employee.h"
using namespace std;
int main(){
string name;
int number;
int hiredate;
do{
cout << "Please enter employee name: ";
getline(cin, name);
cout << "Please enter employee number: ";
cin >> number;
cout << "Please enter hire date: ";
cin >> hiredate;
}while( number <= 0 && hiredate <= 0 && name != "");
cout << name << "\n";
cout << number << "\n";
cout << hiredate << "\n";
system("pause");
return 0;
}
【问题讨论】:
-
您只想在名称为
""时停止循环?我想你想要name == ""?我想你也想使用 or (||) 而不是 and (&&)