< Previous | Contents | Next >

Creating the Game Loop

Next, I write the game loop.

do

{

cout << "Enter a guess: "; cin >> guess;

++tries;


if (guess > secretNumber)

{

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 players 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 players guess is correct, the loop ends. Notice that the if statements are nested inside the while loop.