Syntax:
The syntax of declaring enumeration is given below. The enum keyword is used to declare the enumeration data type, and the names of enum values are separated by a comma (,). In addition, the default value of the enumeration starts from 0.
Example 1: Convert Number Into Enum Values
The way to convert any number value into an enum value is shown in the following example. An enum of 12 elements has been declared in the code. An integer value will be taken from the user. If the input value is within 0 to 11, then the value will be converted into the enum value. Next, the position of the month will be set based on the enum value, and the formatted string will be printed later:
#include <iostream>
using namespace std;
int main()
{
//Declare the enum
enum Month { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
//Declare necessary variables
Month enumVar;
string mVal, pos = "th";
int mNum;
//Take any integer value from the user
cout <> mNum;
//Check the input
if(mNum >=0 && mNum <=11)
{
//Convert the number into enum
enumVar = (Month)mNum;
if (enumVar+1 == 1)
pos = "st";
else if(enumVar+1 == 2)
pos = "nd";
else if(enumVar+1 == 3)
pos = "rd";
cout << "The " << enumVar + 1 << pos <<" month of the year." << "\n";
}
else
cout << "Invalid number.";
return 0;
}
Output:
The following output will appear after executing the above code if 6 is taken as the input value:
Example 2: Convert the Enum Values Into the String
In the previous example, the value of the enum variable was accessed. However, if you want to print the particular text for the particular enum value, then you have to use a conditional statement. The way to convert the enum value into the string using a switch case statement is shown in the following example. The enum of 7 elements has been declared in the code where the names of the enum are 7-weekday names. After taking an input within 0 to 6, the value of the corresponding enum value will be converted into the weekday name by using a switch statement. The converted value will be printed later.
#include <iostream>
using namespace std;
//Declare the enum
enum Weekdays { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
/*
The function will convert the enum values
into the string value
*/
void convert_to_string(Weekdays wVal)
{
string weekday;
switch(wVal) {
case Sunday:
weekday = "Sunday";
break;
case Monday:
weekday = "Monday";
break;
case Tuesday:
weekday = "Tuesday";
break;
case Wednesday:
weekday = "Wednesday";
break;
case Thursday:
weekday = "Thursday";
break;
case Friday:
weekday = "Friday";
break;
case Saturday:
weekday = "Saturday";
}
cout << "The weekday name is " << weekday << "\n";
}
int main()
{
//Declare necessary variables
Weekdays enumVar;
string wVal;
int mNum;
//Take any integer value from the user
cout <> mNum;
//Check the input
if(mNum >=0 && mNum <=6)
{
enumVar = (Weekdays)mNum;
convert_to_string(enumVar);
}
else
cout << "Invalid number.";
return 0;
}
Output:
The following output will appear after executing the above code if 3 is taken as the input value:
Example 3: Set the Different Values for the Enum
It is mentioned earlier that the starting default value of the enum is 0. The way to declare enum is by defining each value separately and access the particular value, as shown in the following example. An enum of 5 elements has been declared in the code where a non-sequential value has been assigned for each element. The value of “Blue” will be printed later:
#include <iostream>
using namespace std;
int main() {
//Declare the enum
enum colors { Red = 10, Green = 30, Blue = 20, White = 40, Black = 50};
//Declare enum variable with value
colors enumCol = Blue;
//Print the value of the enum
cout << "The value of 'Blue' is : " << enumCol << "\n";
return 0;
}
Output:
The following output will appear after executing the above code:
Example 4: Count the Size of the Enum
The way to count the size of an enum is shown in the following example. An enum of 5 elements has been declared in the code. A counter variable has been used to count the total number of enum elements. The “for” loop has been used to iterate the enum values from the beginning to ending and increment the value of the counter variable by one in each iteration.
#include <iostream>
using namespace std;
int main()
{
//Declare the enum
enum marks {CT, Attendence, Assignment, Mid, Final };
//Initialize the counter variable
int counter = 1;
/*
Iterate the values of the enum using loop
to count the total number of elements of the enum
*/
for(int i = CT; i < Final; i++)
{
counter++;
}
cout << "Total number of elements is: " << counter << "\n";
return 0;
}
Output:
The following output will appear after executing the above code:
Conclusion:
The enumeration can be used to solve different types of programming problems in C++. The purpose of using the enumeration has been described here by using various examples. I hope, the new C++ coders will be able to understand the use of the enumeration and apply it in their code properly after reading this tutorial.