< Previous | Contents | Next >

Introducing the Play Again

Program

The Play Again program simulates the play of an exciting game. (Okay, by simulates the play of an exciting game,I mean the program displays the message **Played an exciting game**.) Then the program asks the user if he wants to play again. The user continues to play as long as he enters y. The program accomplishes this repetition using a while loop. Figure 2.6 shows the program in action.

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_again.cpp.

Using while Loops 55



image

Figure 2.6

The repetition is accomplished using a while loop.

// Play Again

// Demonstrates while loops


#include <iostream> using namespace std;


int main()

{

char again = ’y’; while (again == ’y’)

{

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

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

}


cout << "\nOkay, bye.";


return 0;

}