Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ Enumeration help


ghost's Avatar
0 0

Well i'm currently having a bit of a stupid problem with enumerations as i just started trying them out so im going to ask a rather stupid question but here goes,

so basically i know how to declare the enumeration and all but i don't know how to call the values out of the enumeration eg;

here is an enumeration

enum color {blue,red,orange etc}

i know that blue is equal to 1 and red is equal to 2 unless specified to another value and i can use them upon there value but the thing i don't know how to do is to instead extract the name rather than getting the digits eg

I write a program and i want to get blue when i type in the digit 0 using enumerations offcourse (yes if and else statements can also be used but they are a much longer name which is why i want to use enumerations}.

heres what i mean in code;

enum color {blue,red,orange etc}mycolor;

int main () { int n; cout << "Please enter a digit between 1-5: " << endl cin >> n; mycolor=color(n) ….. and from here i sort of don't know what to do i want it to print blue when i input the digit 1 for n.

so any experienced C++ programmers or any programmers that can help me please post on what i can do to achieve the desired result.


ghost's Avatar
0 0

I don't really know much about this but enumerated values are really just numbers… So printing one would just print out the relevant number… EG. in this case it would print out "0" instead of blue, "1" instead of red etc…


ghost's Avatar
0 0

yeh i know that but the thing i was wondering is that if it was possible to instead be able to put in a value and get the string which would be a lot useful in my opinion i have been able to make plenty of programs just using the the number output such as calenders etc but yeh like i said before i was wondering whether i would instead get the string instead of the digit as a output.

but luckely i have figured out how to achieve exactly the same result with just the digit input as well as if i got a string of characters haha well i guess my problem is solved anyway thanks for anyone that helped:D