+ 1
how do I ensure that a user does not input a number less than 1 or greater than 10 in this code. int a; cin>>a;
2 Respuestas
+ 7
a = 0; //a MUST be initialized so the while condition is true, else it will be skipped over.
cout << "Please enter a number between 1 and 10:\t";
while (a < 1 || a > 10)
{
cin >> a;
if (a < 1 || a > 10)
{
cout << "Error! Must be between 1 and 10!" << endl;
}
}
+ 1
If (a is less than 1 OR a greater than 10) {
Throw error on user
}
else
{
do stuff
}