What's an enumerated type?

closed account (zN0pX9L8)
Please explain it VERY carefully as I am retarded and know none of the lingo.


Thank you
Last edited on
closed account (z05DSL3A)
http://en.wikipedia.org/wiki/Enumerated_type
closed account (zN0pX9L8)
Thanks for the link, but that still didn't really clarify anything
an enum-type is used to "translate" words into numbers.
An example.
You have an array that stores your grades like:

int Grade[4];

Now, you need to assign the grades of your subjects (like math, english, biology, chemistry, history) to your array.

So, in math you have an grade of 1

Grade[0] = 1; // math --> 1

for english you have recieved a grade of 3

Grade[1] = 3; // english --> 3

Later in your code, you want to retrieve the grade of english. For that you will have to remember that in your Grade[] array,the second entry [1] would be english.

With enum you could do it easier.
enum subject{math, english, biology, chemistry, history};

the compiler will do the following.
Every expression in the brackets will get an index-number starting with 0.
So you get
math = 0
english=1
chemistry =2 a.s.o.

Now,what good does that do?
The expression Grade[0] is the same as Grade[math]
You do not need to know which subject has which index any more, you simply call them by their name.
This means that instead of a figure you can really NAME the index you need. It makes your code easier to read and to debug.

But note: you can have as many enum as you like, but you can never have a name stored in all those enums more then once!

hope that helps

int main
Last edited on
Another way of thinking of it is is a fixed/limited list of options that a variable of that type can be set to. A standard int variable can be any valid integer between it's system min and max possibilities. If you defined an enum as
1
2
3
4
5
6
typedef enum
{
   FIRST_VAL,
   SECOND_VAL,
   THIRD_VAL
} ValueType;

Then you can create a variable of ValueType instead of int and it can only be one of those three values. E.g.
 
ValueType the_value = THIRD_VAL;


There is some stuff in here too that should fill out that explanation
http://www.cplusplus.com/doc/tutorial/other_data_types.html

Last edited on
closed account (zN0pX9L8)
Thank you int main. You are a god. I want you in me.
@mrtwinkle

You should also thank the others. Their explanations are also correct.

int main
Topic archived. No new replies allowed.