enum

Please, can someone write me an easy example for using "enum"- or send a link?
But please, very very simple cause I am the sheer beginenr!

Many thank!
In most cases, you won't be really using enums, but if you want :

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
#include <iostream>

int main()
{
    // enum is like declaring your own type ( like int, char, float etc... )
    // which can only contain specific values :
   enum COLOR { BLACK, WHITE, GREEN, RED, YELLOW, BLUE };
   
   // declare a variable of type COLOR :
   COLOR color1, color2;
   // since the type of color1, 2 and 3 is COLOR, 
   // we can only assign the values inside the enum
   // COLOR to it :
   color1 = YELLOW;
/***********************************
   color2 = PINK; // this will cause compilation error since PINK is not declared
                     // in the COLOR enum
***********************************/
   color2 = BLUE;

   // and you can only use the values in the enum to compare it**
   if( color1 == YELLOW && color2 == BLUE )
        std::cout << "color1 and color2 makes green !!!" << std::endl;
        
   else std::cout << "color1 and color2 doesn't make green :/" << std::endl;
   
   
   return 0;
}


color1 and color2 makes green !!!



**Actually, you can also use integer values, since the first value in enum is assigned an int value of 0 ... and so on
Last edited on
Hello!
Ii saw just a few examples, but they do not seem usefull ???
Topic archived. No new replies allowed.