enum














































enum



ENUM- enum stands for enumerate. It is a type that represents enumerated numbers, i.e. , their values are in arithmetic progression with 1 as common difference, unless its previous value is initialized.

Syntax - enum type_name {list of argument(s)};

On creating a variable under the type_name, we can't assign it with a new value or request it's value from the user since cin is not specified for this type.

Code -

// In the given program, we accept a number from the user and print it's corresponding day.

#include<bits/stdc++.h>
using namespace std;
enum DayOfWeek {Mon=1, Tue, Wed, Thrs, Fri, Sat, Sun};  
     // Here we can assign any variable any value and the next variable will be = (previous variable's value+1).
string AcceptDay(DayOfWeek);
int main()
{
    DayOfWeek v=Mon;    // Value of global enum variable can't be anything except the one given inside enum.
    int a;
    cout<<"Enter the day of a week : ";
    cin>>a;
    cout<<AcceptDay(DayOfWeek(a));
    return 0;
}
string AcceptDay(DayOfWeek t_v)
{
    switch(t_v)
    {
    case Mon:
        cout<<"Monday";
        break;
    case Tue:
        cout<<"Tuesday";
        break;
    case Wed:
        cout<<"Wednesday";
        break;
    case Thrs:
        cout<<"Thursday";
        break;
    case Fri:
        cout<<"Friday";
        break;
    case Sat:
        cout<<"Saturday";
        break;
    case Sun:
        cout<<"Sunday";
        break;
    default:
        cout<<"Incorrect Entry";
    }
}

Output -

Enter the day of a week : 5
Friday
Enter the day of a week : asD_f7gh
Incorrect Entry

Comments