【发布时间】:2013-01-13 14:26:17
【问题描述】:
我需要通过 cin 验证用户输入,我的代码如下:
#include"iostream"
using namespace std;
int main()
{
int choice = 0;
while(1)
{
cout<<"ENTER your choice a value between 0 to 2"<<endl;
cin>>choice;
// Here i need some logic which will work like "choice" can be only
// Integer and within the range i.e. 0 to 2 and if it is not
// satisfy condition then ask user to input again
switch(choice)
{
case 0:
exit(1);
break;
case 1:
fun();
break;
case 2:
fun1();
break;
default: cout<<"Please enter a valid value"<<endl;
break;
}
}
return 0;
}
【问题讨论】:
-
您还没有真正解释问题所在(但请参阅 chris 的评论)。另外,
while (1)=>while (true)我会将exit(1)替换为return 1。 -
@KonradRudolph 是的,我可以做到,我检查了克里斯评论,我认为这将解决我的问题....我只需要从 0 到 2 的用户输入并拒绝其他输入并从用户输入。
标签: c++ visual-c++ cin