Thread: Question about ENUM

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    14

    Question about ENUM

    hello there,

    i have something confused.

    if i type this
    Code:
     enum days { sun, mon, tue, wed, thu, fri, sat };
    then typing ;
    Code:
     cout << days(1);
    should not result in outputing "mon" ?
    when i do this it outputs the number '1' instead of the day behind this number (sun=0, mon=1)

    why is that?

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    No. An enum is just a nice way of representing integer values. You're aliasing sun for 0, mon for 1, etc.

    What you would need to do here is have an array of the string values of the days, like this:

    Code:
    const char * const dayVals[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    and then you can use the enum to index into this array to get the name:

    Code:
    cout << dayVals[mon] << endl; // prints "Mon"

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    14
    Quote Originally Posted by rags_to_riches View Post
    No. An enum is just a nice way of representing integer values. You're aliasing sun for 0, mon for 1, etc.

    What you would need to do here is have an array of the string values of the days, like this:

    Code:
    const char * const dayVals[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    and then you can use the enum to index into this array to get the name:

    Code:
    cout << dayVals[mon] << endl; // prints "Mon"

    do i have to cout << dayVals[mon] ?
    what if i want to print all the days in a loop, can't it be printed by dayVals[i] for e.g. ?

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by Jonathan Yaniv View Post
    do i have to cout << dayVals[mon] ?
    Yes.

    Quote Originally Posted by Jonathan Yaniv
    what if i want to print all the days in a loop, can't it be printed by dayVals[i] for e.g. ?
    Yup. You can just run a for-loop over your dayVals array and use the loop index to access individual array elements. Beware that this will not work if your enum definition contains "holes", i.e.

    Code:
    enum Foo
    {
        Bar,
        Blah,
        Meep = 34,  // <-- hole! enum values are not contiguous
        Woosh
    };
    In such a case, your best bet may be writing a function to print enum values:

    Code:
    void printFoo( Foo enumVal, std::ostream& out )
    {
        switch ( enumVal )
        {
        case Bar:
            out << "Bar";
            break;
            
        case Blah:
            out << "Blah";
            break;
            
        case Meep:
            out << "Meep";
            break;
            
        case Woosh:
            out << "Woosh";
            break;
            
        default:
            out << "UNRECOGNIZED Foo enum with integer value " << enumVal << "!";
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. enum question
    By mdoland in forum C# Programming
    Replies: 2
    Last Post: 04-17-2008, 11:19 AM
  2. Question on Enum
    By markcls in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 08:19 AM
  3. One more enum question
    By joenching in forum C++ Programming
    Replies: 5
    Last Post: 05-08-2005, 02:54 PM
  4. Enum question
    By Bill 101 in forum C Programming
    Replies: 4
    Last Post: 10-31-2002, 12:33 AM
  5. enum question
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 08-10-2002, 12:33 PM