beginning C++ through game programming third edition solution help

I need help with the first exercise on chapter 2.
here is the exercise:Rewrite the Menu Chooser program using an enumeration to represent the difficulty levels. The variable choice will still be of type int.

original Menu Chooser:
{
cout << "Choose your difficulty level\n\n";
cout << "1-Easy\n";
cout << "2-Normal\n";
cout << "3-Hard\n";

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

switch(choice)
{
case 1:
cout << "You picked easy.\n";
break;
case 2:
cout << "You picked normal.\n";
break;
case 3:
cout << "You picked Hard.\n";
break;
}


Menu chooser with enumerations:
}cout << "Choose your difficulty level\n\n";
cout << "1-Easy\n";
cout << "2-Normal\n";
cout << "3-Hard\n";

int choice;
enum difficulty{Easy, Normal, Hard};
difficulty myDifficulty = Easy;
cin >> choice;

switch(myDifficulty + choice)
{
case 1:
cout << "Easy\n";
break;
case 2:
cout << "Normal\n";
break;
case 3:
cout << "Hard\n";
break;
}


Is this how I'm suppose to apply enumerations?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
enum { Easy = 1, Normal, Hard };

cout << "Choose your difficulty level\n\n";
cout << "1-Easy\n";
cout << "2-Normal\n";
cout << "3-Hard\n";

int choice;
cin >> choice;

 switch ( choice )
 {
   case Easy:
      cout << "Easy\n";
      break;

   case Normal:
      cout << "Normal\n";
      break;

   case Hard:
      cout << "Hard\n";
      break;

   default:
      cout << "Invalid choice\n"
      break;
 }

Last edited on
No, not really because, enums have default values in your case, easy=0, normal=1 and hard =2, you can change these.

You can also use enums this way:
1
2
3
4
5
6
7
8
9
10
11
12
enum EDIFFICULTY
{
     EASY=1,
     NORMAL,  //=2
     HARD  //=3
}myDiff;

cout<<"Difficulty 1,2 or 3";
cin>>myDiff;

if(myDiff=EASY)
     ...

For more info: http://cplusplus.com/doc/tutorial/other_data_types/

HTH,
Aceix.
Thanks for all the answers!
Topic archived. No new replies allowed.