cout << " Please choose between number 1 or 2 " << endl;
cout << " To enter Zoo enter 1 ! " << endl;
cout << " To NOT enter Zoo enter 2 ! " << endl;
cin >> x;
if ( x == 1 )
{
cout << " You have entered the zoo " << endl;
}
else if (x<0||x>2)
{
cout << "That is an invalid number. Please re-run the progra againm" << endl;
}
else if( x == 2 )
{
cout << " You have decided not to take up the challenge, see you again bye ! " << endl;
}
return 0;
}
Hi ! The question goes like this, A boy name Tom has 2 choice, Enter 1 to go Zoo or Enter 2 to not enter zoon. What if the Tom enters a number -1 ? I know the statment will say " That is an invalid number. Please re-run the program again". How do I loop this statement to ask Tom to input the number 1 or 2 without having to compile and re-run my program ?
A simple alternative to consider. (But be careful this assumes all input will be integers. Enter a character, string or decimal and the program will bomb.)
#include <iostream>
usingnamespace std;
int main()
{
int x = 0;
cout << " Please choose between number 1 or 2 " << endl;
cout << " To enter Zoo enter 1 ! " << endl;
cout << " To NOT enter Zoo enter 2 ! " << endl;
while (cout << "Now enter no: " && cin >> x)
{
if ( x == 1 )
{
cout << " You have entered the zoo " << endl;
}
elseif( x == 2 )
{
cout << " You have decided not to take up the challenge, see you again bye ! " << endl;
break;
}
else
{
cout << "That is an invalid number. Please re-run the progra againm" << endl;
}
}
return 0;
}