Enum type

Enum (enumeration) is a custom data type (a data type that can define its type). We can determine what it contains. When defining it, we give a set of values of this type in braces (similarly, e.g., an int has a set of allowed values - a range).


#include <iostream>
using namespace std;

enum dayOfTheWeek {Mo = 1, Tu, We, Th, Fr, Sa, Su};

string getDay(dayOfTheWeek x) {
    switch(x) {
        case Mo:
            return "Monday"; break;
        case Tu:
            return "Tuesday"; break;
        case We:
            return "Wednesday"; break;
        case Th:
            return "Thursday"; break;
        case Fr:
            return "Friday"; break;
        case Sa:
            return "Saturday"; break;
        case Su:
            return "Sunday"; break;
    }
}

int main()
{    
    cout << dayOfTheWeek(Tu) << endl; // just like typing int(5)

    int x;
    cout << "Choose the number of a day of the week: " ;
    cin >> x;
    
    cout << getDay(dayOfTheWeek(x)) << endl;

    return 0;
}
                                    

The elements given in the braces are numbered. If we want to change the numeration, we can assign, for example, one to "Monday" instead of the default zero, and then all subsequent numbers will follow, i.e., "Wednesday" will be the third, etc. We cannot write, e.g., dayOfTheWeek x = 25; because 25 isn't in the range of possible values. We also cannot use the cin statement with variables of this type because it isn't specified for them.