< Previous | Contents | Next >

Introducing the Play Again 2.0

Program

The Play Again 2.0 program looks exactly the same to the user as the original Play Again program. Play Again 2.0, like its predecessor, simulates the play of an exciting game by displaying the message **Played an exciting game**and asking the user whether he wants to play again. The user continues to play as long as he enters y. This time, though, the program accomplishes the repetition using a do loop. Figure 2.7 shows off the program.

You can download the code for this program from the Course Technology website (www.courseptr.com/downloads). The program is in the Chapter 2 folder; the filename is play_again2.cpp.

// Play Again 2.0

// Demonstrates do loops #include <iostream>

Using do Loops 57

using namespace std; int main()

{

char again; do

{

cout << "\n**Played an exciting game**";

cout << "\nDo you want to play again? (y/n): "; cin >> again;

} while (again == ’y’); cout << "\nOkay, bye.";

return 0;

}


image

Figure 2.7

Each repetition is accomplished using a do loop.