Question about enumerations.

So is it possible to set enumeration variables to a certain value inputted by the user?

example:

1
2
3
4
5
6
7
8
9
enum color {Red, Blue, Green, Yellow, Orange, Purple}
color favorite_color;

cin >> favorite_color;

if (favorite_color == Blue)
{
    cout << "I also like blue" endl;
}

I've been trying to do this and it hasn't worked, so is it possible to do this at all?
Enumeration values in the initialization list work like a series of typedefs. Basically, each value listed in the initialization list of the enum will be replaced by the numeric value it represents. Since you didn't initialize the enum with the numerical values, it defaults to Red = 0, Blue = 1, etc. So in your if statement you are actually testing if favorite color == 1. I'm not sure how exactly the cin >> favorite_color would work to be honest, I don't use enums all that often because they can be a PITA to use.
Yeah, they seem like they are a pain to work with. I don't understand what they are even really useful for. :l
Enums are ideal for grouping a bunch of things together with easily understood names. For example, I made a menu class a while back that allowed another code to select a location for the menu, TOP, CENTER, BOTTOM, and LEFT, RIGHT, CENTER. All of those positions were thrown in an enum to allow easy comparison through my code.

They also work nicely for setting this up:
1
2
3
   void setconsolecolor(int textColor, int bgColor) {
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (textColor + (bgColor * 16)));
   }


I haven't implemented it yet, but simply changing the textColor and bgColor from int's to say an enum of color with convertible values, I can allow the user to manually select the color they want to use without knowing the int value of each color.
Topic archived. No new replies allowed.