harrylmh
April 9th, 2001, 09:49
Hello,
anyone who knows C++ pls help me with this problem:
I want to make a user enter a int value between 1 to 2. Anything more or less, or say the user enters a char type value, and the program will ask for the input again. If I enter a char into the input, it would cause an infinite loop without the "!cin>>num" but with it, the program will freeze. How do I get the program to take the error correctly? How do I get the program to ask for input again if there's a char input and not loop infinitely?
// program start
void main(){
int num
cin >> num;
while (!cin >> num || num<1 || num>2)
{
cin >> num;
}
// program end
}
Thank you!
anyone who knows C++ pls help me with this problem:
I want to make a user enter a int value between 1 to 2. Anything more or less, or say the user enters a char type value, and the program will ask for the input again. If I enter a char into the input, it would cause an infinite loop without the "!cin>>num" but with it, the program will freeze. How do I get the program to take the error correctly? How do I get the program to ask for input again if there's a char input and not loop infinitely?
// program start
void main(){
int num
cin >> num;
while (!cin >> num || num<1 || num>2)
{
cin >> num;
}
// program end
}
Thank you!