C++ program to define an enumerator Color with various names of colors as its members

//C++ program to define an enumerator Color with various names of colors as its members (i.e. enumerated list)
//Read an integer from user and display color name corresponding to the integer

#include <iostream>
using namespace std;
int main()
{
    enum Color{red,green,blue,};
    Color c;
    int n;
    cout<<"Enter an integer (0 to 2): ";
    cin>>n;
    c=Color(n);
    //Color c=(Color)n;
    if(c==0)
        cout<<"red";
    else if(c==1)
        cout<<"green";
    else if(c==2)
        cout<<"blue";
    else
        cout<<"Invalid";
    return 0;
}

No comments:

Post a Comment