Public Group

Help with understanding enumerations...

This topic is 2600 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

 enum difficulty {Easy = 1, Normal, Hard}; int choice; 

In this example, how is the value in enum difficulty assigned to int choice? I'm not sure what I'm missing. What do these two variables actually store? How does the switch pull Easy, Normal, Hard? This is really frying my brain for some reason, as simple as it may seem. I can't really even see the purpose of enumerations at this point (listing...or something?), but I'll assume they're important and keep at it haha.

Following is the full program for reference. Thanks for your time.
 // Menu Chooser Enumeration // Rewrite the menu chooser program to use enumerations #include <iostream> using namespace std; int main() { cout << "Difficulty Levels\n\n"; cout << "1 - Easy\n"; cout << "2 - Normal\n"; cout << "3 - Hard\n"; enum difficulty {Easy = 1, Normal, Hard}; int choice; cout << "Please choose a difficulty level: "; cin >> choice; switch (choice) { case Easy: cout << "You picked Easy.\n"; break; case Normal: cout << "You picked Normal.\n"; break; case Hard: cout << "You picked Hard.\n"; } return 0; } 

Share on other sites
There is no assignment of difficulty to choice, the enum is used for comparison.

enum difficulty { Easy = 83, Medium = 49, Hard = 73 };

Then the user would have to enter 83, 49 or 73 which get's assigned to choice.

choice is then compared to the enums in the switch statement to see if they match up.

Enums are VERY useful for giving a name to something....it's much better / more readable to write something like the following....

if ( buttonPressedID == SPACE_BAR ) where SPACE_BAR is an enum

rather than...

if ( buttonPressedID == 27 ) where you have a numeric literal

Does that make any sense ?

Share on other sites
I have no idea what it says in the official standard, but I can tell you from a purely practical experience with enums that they act like integer constants.

So in your enum Easy=1, Normal=2 and Hard=3 (incrementaly).

There is nothing special about them, they are used when you know upfront what kind of stuff you will need.

Share on other sites
Ahh I see, thats much easier to understand, thank you both! So essentially an enumerator is just a way to assign a name to a known or predicted value. Much better than the 'series of incrementing intergers' definition the book gave. My question is now this - where does 'difficulty' tie in then? I see no calls to it aside from its initial creation - how does the switch get the value for easy/normal/hard without pulling from difficult? Again, thank you very much. <BR>The best I can see is: If choice = 1, it gets compared to difficult's value of 1 (easy) and then passed to the switch which compares it to case 1 and executes it as true? Edited by ebontide

Share on other sites
"difficulty" is the name of the type you are defining. Once you've defined an enum, you can often forget it is "actually" an integer altogether. For example:

 enum Difficulty { Easy = 1, Medium, Hard }; int main() { Difficulty diff = Easy; } 

So you never call it an "int", you call it by its type name (Difficulty).

Wow, C++ code looks weird after writing C# almost exclusively for 5 years... I feel like I'm missing... something

Share on other sites
You can declare variables of the enum type:
difficulty d = Easy;
You can give d any of the values Easy, Normal or Hard. This is useful because you can assume it is one of these values. If d was an integer it could have any value.
This will not work:
difficulty d = 1;

An enum type is implicit convertible to int, that is why you can use it as it was an integer as others have pointed out.

Share on other sites
"The best I can see is: If choice = 1, it gets compared to difficult's value of 1 (easy) and then passed to the switch which compares it to case 1 and sees executes it as true? "

That's it in a nutshell...

Other (not recommended) ways of achieving the same thing are....

#define EASY 1
#define MEDIUM 2
#define HARD 3

and then....

switch ( choice )
{
case EASY:
blah blah
break;
case MEDIUM:
blah blah
break;
case HARD:
blah blah
break;
}

Or if you really like it bad....

switch ( choice )
{
case 1:
blah blah
break;
case 2:
blah blah
break;
case 3:
blah blah
break;
}

One of the advantages of enums is that they let you insert values and the rest of your code will just work fine once recompiled.

I use them for text string ID's so that makes localisation easier.

For example....

A basic text table.....

const char * textEnglish[ ] =
{
"Hello",
"Goodbye"
};

const char * textFrench[ ] =
{
"Bonjour",
"Aurevoir"
};

etc.

Then I have an enum as follows....

enum { Hello = 0, Goodbye };

and the rest of the code just uses....Print( Hello ); or Print( Goodbye );

Then to change language all you have to do is change which text table you want...and the rest of the code remains unchanged.

PS...I'm rubbish at explaining things

Share on other sites
Hmm, I feel like I'm right on the edge of really understanding all of this, but much of what you're all explaining are things I haven't had much if any experience with (this is only chapter 2 in the book - I'm fairly fresh). Ok, lets see if this is what you're getting at guys:

>difficulty is a type that is assigned to easy/normal/hard
>easy/normal/hard are essentially names given to values (1,2,3 in this case)
>choice is given a value of 1,2, or 3 by user
>switch (choice) compares the value of choice to the names given those values by difficulty
> if the value of choice == the value of easy/normal/hard, that portion of the switch is executed How close am I?

edit: and the main part that fried my mind: all difficulty is called for is to say something similar to difficulty = easy rather than having to remember numericals etc?

Bang on.

Share on other sites
I really appreciate it guys, one hurdle down - and well....yeah lets not look too far ahead right now

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 12
• 9
• 12
• 37
• 12
• Forum Statistics

• Total Topics
631419
• Total Posts
2999983
×