Enum and Switch Values


I found this topic covering my problem...

http://www.cplusplus.com/forum/beginner/3840/


However, the solution given has brought up a question.

I just can't figure out the connection between "choice" and "level", how does the value of choice get assigned to level so the program knows which you've chosen?

Is it because that "switch" is automatically assigned to the previous enumeration?

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
29
30
31
32
33
34
35
36
37
38
39
40
41
// Menu Chooser rewrite, p70-1
// Demonstrates the switch statement and enumeration

#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";

enum level {easy = 1, normal, hard};
int choice;

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

switch (choice)
{
case easy:
cout << "You picked Easy." << endl;
break;
case normal:
cout << "You picked Normal." << endl;
break;
case hard:
cout << "You picked Hard." << endl;
break;
default:
cout << "You made an illegal choice." << endl;
}

cout << "Press ENTER to continue...";

system("pause");

return 0;
}


Last edited on
enum values are integers so you can easily ( and implicitly ) convert an int to an enum
enum level {easy = 1, normal, hard};

We're saying to start the numeration at 1, thus easy == 1, normal == 2, hard == 3.

Within the switch, "case easy:" is the same as saying "case 1:".

Choice is user input, so only if the user inputs 1 or 2 or 3 will will be checked against case easy, normal, hard. All other input will be caught by the default statement.

OK, so "switch (choice)" brings in the value of choice to the switch and then the "case ..." lines within the switch are the connection to the enum.

Great! Thanks!

(I'm completely new to this, just started a week ago)


Would it then be possible to use "level"?

1
2
If level = 1 then cout << "You picked Easy."
If level = 2 then cout << "You picked Normal."


Et cetera?

'level' is the name you assigned to the enum, it is a not a variable so you can't compare it with other values.

You can use 'choice' since that is the variable containing the user's input. The code would be
1
2
3
4
if( choice == 1 )
	cout << "You picked Easy."
if( choice == 2 )
	cout << "You picked Normal."

Thanks again!
You can do this btw:
1
2
3
level mylevel = choice;
if ( mylevel == easy )
    //... 
An easy way to understand enumerated types is to think of them as very very small classes.

Unfortunately "very small classes" doesn't mean much to me, just started this a week ago.
I know I've read the definition but I don't learn stuff until I do it.

Although I do have one more question about the switch and enum relationship.


Since within the switch, at each case line, you use a specific enumerator(tion?), (east, normal, hard), then it is possible to enumerate and then to do so again but can't use the same words (easy, normal, hard).

I just want to understand if upon enumerating, does the nesxt switch have refer to the previous enumeration or can I have multiple enumerations and then is the switches for them in any order I wish?
You missed the point in the original thread.

An enumeration is a programmer's convenience. Not a user's.
So, when you get input, you must manually transform it into your enumeration value...

For example:
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
enum fruit_t { not_a_fruit, apple, banana, cherry };

fruit_t fruit;

cout << "What is your favorite fruit:\n"
        "  a - apple\n"
        "  b - banana\n"
        "  c - cherry\n"
        "> "
     << flush;
char ch;
cin >> ch;

switch (toupper( ch ))
  {
  case 'A': fruit = apple;  break;
  case 'B': fruit = banana; break;
  case 'C': fruit = cherry; break;
  default:  fruit = not_a_fruit;
  }

if (fruit == not_a_fruit)
  cout << "Hey, that's not a fruit!\n";
else
  cout << "I like that too!\n";

What this does is keep the proper stuff together, and the improper stuff separate.

Lines 11..20 deal with converting the user's input into a fruit_t.
Lines 22..n don't care what the user's input looks like. All it cares about is the fruit.

Hope this helps.
Topic archived. No new replies allowed.