< Previous | Contents | Next >

Introducing the Menu Chooser

Program

The Menu Chooser program presents the user with a menu that lists three difficulty levels and asks him to make a choice. If the user enters a number that corresponds to a listed choice, then he is shown a message confirming the choice. If the user makes some other choice, he is told that the choice is invalid. Figure 2.5 shows the program in action.


image

Figure 2.5

Looks like I took the easy way out.

Using the switch Statement 53


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

// Menu Chooser

// Demonstrates the switch statement #include <iostream>

using namespace std;


int main()

{

cout << "Difficulty Levels\n\n"; cout << "1 - Easy\n";

cout << "2 - Normal\n"; cout << "3 - Hard\n\n";


int choice;

cout << "Choice: "; cin >> choice;


switch (choice)

{

case 1:


case 2:


case 3:

cout << "You picked Easy.\n"; break;


cout << "You picked Normal.\n"; break;


cout << "You picked Hard.\n"; break;

default:

cout << "You made an illegal choice.\n";

}


return 0;

}

54 Chapter 2 n Truth, Branching, and the Game Loop: Guess My Number