我有一个程序可以计算某些员工的薪水 我的问题是,当程序要求输入数年或加班小时时,我应该输入数字,但是如果输入char,程序将进入无限循环
cout << "ENTER THE NUMBER OF YEARS :"<<endl;
cin >> loyalty;// here is my problem
while (loyalty>20)
{
cout << "the number you enter is not logic" <<endl;
cout <<"ENTER THE NUMBER OF YEARS :" <<endl;
cin >> loyalty;
}
cout << "ENTER THE OVERTIME HOURS :"<<endl;
cin >> overtime ;
while (overtime>72) //to be more logic humen can't work more then
72hr as overtime hour per month (based on fact)
{
cout << "the number you enter is not logic" <<endl;
cout << "ENTER THE OVERTIME HOURS :"<<endl;
cin >> overtime ;
答案 0 :(得分:0)
尝试将输入检索到char数组中,然后调用atoi() 例如。 字符输入[10]; cin >> input;
int数字= atoi(输入)
如果char数组不包含有效数字,atoi将引发异常