Hey guys, I am creating a random number generator and have run into a problem with my do while loop. it continually prints one of the lines of output infinitely. what am i missing here?
do
{
if (guess > randomNum)
{
cout << "Your guess is too high. Try again!\n";
cout << "Guess A Number Between 1 and 100: ";
cin >> guess;
}
elseif (guess < randomNum)
{
cout << "Your guess is too low. Try again!\n";
cout << "Guess A Number Between 1 and 100: ";
cin >> guess;
}
else
{
cout << "You guessed the number. Congratulations! The number was " << randomNum;
}
}
while (guess != randomNum);
The congratulations text will only appear if they guess correctly on the first try. If they get the right number on line 7 or line 13 after a wrong guess, the condition of the while loop on line 20 will be no longer be true and the loop will terminate.
flushing cout/using endl didn't do anything to fix the problem, but probably a good idea either way.
If they get the right number on line 7 or line 13 after a wrong guess, the condition of the while loop on line 20 will be no longer be true and the loop will terminate.
this makes sense... very raw to C++, any suggestions?