C++ 检查输入是否为特定类型且在特定范围内
我正在尝试测试用户输入的内容,这样我的程序就不会中断。我使用的线路是:C++ 检查输入是否为特定类型且在特定范围内,c++,while-loop,C++,While Loop,我正在尝试测试用户输入的内容,这样我的程序就不会中断。我使用的线路是: while (check != 0){ cout << "Please enter a number 1 - 4 (area of: circle, rectangle, triangle or quit)" << endl; cin >> response; if ((isdigit(response)) && ((response >=
while (check != 0){
cout << "Please enter a number 1 - 4 (area of: circle, rectangle, triangle or quit)" << endl;
cin >> response;
if ((isdigit(response)) && ((response >= 1) && (response <= 4))){
check = 0;
}
else{
cout << "You did not enter a valid digit (1 - 4)" << endl;
check = 1;
}
} // End of while loop
while(检查!=0){
cout反应;
如果((isdigit(response))&&((response>=1)&&&(response假设response
类型为char
,则需要使用字符常量'1'
和'4'
,而不是整数常量1
和4
if ((isdigit(response)) && ((response >= '1') && (response <= '4'))){
if((isdigit(response))&&((response>='1')&&(responseif(response>='1'&&response您的方法到底出了什么问题?什么是检查?什么是响应?您知道流可以用于检查它是否成功?最后一位意味着您可以将响应声明为int
,并执行类似if(!(cin>>响应)的操作| |!介于(响应,1,4))
之间。为什么不在(1)
时说,然后当用户输入有效时,使用break
语句?噢,谢谢。我完全忘了isdigit是字符值。我会√ 10分钟后回答这个问题。