< Previous | Contents | Next >
Next, I write the game loop.
do
{
cout << "Enter a guess: "; cin >> guess;
++tries;
cout << "Too high!\n\n";
}
else if (guess < secretNumber)
{
}
else
{
}
cout << "Too low!\n\n";
cout << "\nThat’s it! You got it in " << tries << " guesses!\n";
} while (guess != secretNumber);
I get the player’s guess, increment the number of tries, and then tell the player if his guess is too high, too low, or right on the money. If the player’s guess is correct, the loop ends. Notice that the if statements are nested inside the while loop.