Enumeration in C++

Enumeration

1. 

/*
 This program illustrates the usage of enum. using enum keyword, week is declared
and has the values shown in braces.
-Then if you declare a variable day, give it any value among present elements and
then print it. It shall print the value of the index of that element.
-Another example is, the for loop shown below,which iteratively prints all indexes.
*/
#include <iostream>
#include <stdio.h>

using namespace std;

enum week{Mon,Tue,Wed,Thur,Fri,Sat,Sun};

int main()
{
    int i;
    for(i=Mon;i<=Sun;i++)
        cout<<i<<endl;

    enum week day;
    day = Thur;
    cout<<day<<endl;

    return 0;
}

2.

#include <iostream>
#include <stdio.h>

using namespace std;

enum week{
    mon,tue,wed,thu,fri,sat,sun
};
int main()
{
    //working day or not
    cout<<"Enter day"<<endl;
    int day;
    cin>>day;
    while(day>=mon && day<sun)
    {
        switch(day)
        {
    case mon:
        cout<<"Class"<<endl;
        break;
    case tue:
        cout<<"Project\n";
        break;
    case wed:
        cout<<"half day off"<<endl;
        break;
    case thu:
        cout<<"Sports"<<endl;
        break;
    case fri:
        cout<<"study"<<endl;
        break;
    case sat:
        cout<<"work"<<endl;
        break;
    default:
        cout<<"its a holiday!"<<endl;
        }
        cout<<"Enter day"<<endl;
        cin>>day;
    }
    cout<<"See ya!"<<endl;
    return 0;
}

3.

#include <iostream>
#include <stdio.h>

using namespace std;

enum week{
    mon=1,tue=2,wed=5,thu,fri,sat,sun
};
int main()
{
    //working day or not
    enum week day;
    day=mon;
    cout<<day<<endl;
    day=tue;
    cout<<day<<endl;
    day=thu;
    cout<<day<<endl;
    day=wed;
    cout<<day<<endl;
    day=sun;
    cout<<day<<endl;
    return 0;
}
/*
output:
1
2
6
5
9
*/
 

Post a Comment

0 Comments