当用户输入错误号码时,有没有办法使用while循环?即(默认)或某事。我创建了goto,它正在工作,但它很蹩脚。
default:
{
cin.clear();
cin.sync();
cout << "ERROR! Choose other number: ";
cin >> choose;
goto again;
}
答案 0 :(得分:-1)
我可以建议:
bool OK;
do
{
cin >> choose;
if(OK = !cin.fail()) //if the input is OK
{
switch(choose)
{
case 1: //do something
break;
//case 2: //do something else
//etc.
default:
cin.clear();
cin.sync();
cout << "ERROR! Choose other number: ";
OK = false;
break;
}
}
}
while(!OK);