Reputation: 11
I am having trouble figuring out my do-while loop. You enter a number from 1-4 and if it is correct, it'll terminate. But if !(1-4) repeat the loop again to type the number again. I have it where my else statement keeps printing an infinite loop. How do I solve the infinite loop so it can keep asking the user to enter a number?
Code:
cin >> num;
do
{
if (num == 1 || num == 2 || num == 3 || num == 4)
{
cout << "Correct\n";
}
else
{
cout << "Incorrect. Try Again\n";
}
}
while ((num != 1) && (num != 2) && (num != 3) && (num != 4));
Upvotes: 1
Views: 215
Reputation: 36660
Your loop never updates the value of num
, so if the loop enters, it can never end.
You likely wanted:
do {
cin >> num;
if (num >= 1 && num <= 4) {
cout << "Correct\n";
}
else {
cout << "Incorrect. Try Again\n";
}
} while (num < 1 && num > 4);
Alternatively, you are checking that condition twice. You don't need to do this when you can use break
to break the loop on correct input.
while (true) {
cin >> num;
if (num >= 1 && num <= 4) {
cout << "Correct\n";
break;
}
else {
cout << "Incorrect. Try Again\n";
}
}
Further, because meeting the condition num >= 1 && num <= 4
means that control flow skips the rest of the loop, we can write the following.
while (true) {
cin >> num;
if (num >= 1 && num <= 4) {
cout << "Correct\n";
break;
}
cout << "Incorrect. Try Again\n";
}
Of course, all of this assumes that the user input a valid integer. For completeness, you probably want to handle erroneous input.
Upvotes: 2
Reputation: 25385
If you want the user to be able to enter input more than once (for example if the previous input was incorrect), then you must move the line
cin >> num;
inside the loop.
In order to prevent having the condition
num == 1 || num == 2 || num == 3 || num == 4
twice in your code (the second one is identical, except negated), it would be better to write this condition only once. Also, you can simpify it to the following:
1 <= num && num <= 4
I recommend you rewrite the loop to the following:
for (;;) //infinite loop, equivalent to while(true)
{
std::cin >> num;
if ( 1 <= num && num <= 4 )
{
std::cout << "Correct.\n";
break;
}
std::cout << "Incorrect. Try Again.\n";
}
It is worth noting that this code will only work if the user enters a valid integer. If the conversion fails, then the invalid input will stay on the input stream, so that the next input conversion will immediately fail for the same reason, causing an infinite loop.
In order to prevent this infinite loop, you should discard the remainder of the line after every input:
for (;;) //infinite loop, equivalent to while(true)
{
//get input from user
std::cin >> num;
//remember whether input failed or not
bool failed = std::cin.fail();
//discard remainder of line
std::cin.clear();
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
//verify that input conversion succeeded and that
//the converted number is in the correct range
if ( !failed && 1 <= num && num <= 4 )
{
std::cout << "Correct.\n";
break;
}
//print error message
std::cout << "Incorrect. Try Again.\n";
}
Note that you will have to #include <limits>
for this code to work.
Upvotes: 0