一个int型变量接收从键盘读入的值,想在输入为字母时重新输入,应该怎么做呢?
程序代码:
int GetRequirementFromKeyboard(void) { int Requirement_Number=0; int flag=1; cout<<"enter a requiremnet-number:"; cin>>Requirement_Number; while(flag) { if(Requirement_Number>0&&Requirement_Number<7)flag=0;//一次正确输入就跳出循环 else { cin.clear(); cout<<"\nOut of rang,please enter a new requiemnet-number\n"; cin>>Requirement_Number; } //输入为字母时死循环了,请问是怎么回事? } return Requirement_Number; }
[此贴子已经被作者于2016-2-29 16:35编辑过]