Could someone please clarify enums for me
Hi everyone,
I am messing about with enums and trying to understand what they are actually used for. I came up with the following 2 programs, none of which demonstrate very well to me the point of enums:
Code:
#include <iostream>
#include <string>
int main()
{
enum TrafficLightColor_t {RED, YELLOW, GREEN};
TrafficLightColor_t currentColour;
//How do I get different values into my new variable currentColour to utilise the following switch?
switch(currentColour)
{
case RED: //How do I get currentColour to equal 'RED'
std::cout << "I am red";
break;
case YELLOW: //How do I get currentColour to equal 'YELLOW'
std::cout << "I am yellow";
break;
case GREEN: //How do I get currentColour to equal 'GREEN'
std::cout << "I am green";
break;
default:
break;
}
}
The other one was:
Code:
#include <iostream>
#include <string>
int main()
{
enum TrafficLightColor_t {RED = 1, YELLOW, GREEN};
int currentColour;
std::cout << "Enter a number. 1=RED, 2=YELLOW and 3=GREEN: ";
std::cin >> currentColour;
switch(currentColour)
{
case RED:
std::cout << "I am red";
break;
case YELLOW:
std::cout << "I am yellow";
break;
case GREEN:
std::cout << "I am green";
break;
default:
break;
}
}
After searching google for example programs, I still can't find an explanation that gives me a full overview.
I understand the point of statements being easier to read i.e. if DEBUGGER_ON, if DEBUGGER_OFF, instead of if == 1, or if == 2 etc... (who knows what 1 or 2 does? Have to look it up).
Any extra clarification is very much appreciated. Thanks.