Thread: problems with 'enum'

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    3

    problems with 'enum'

    Hello first,


    i have a tiny problem with the datatyp enum.

    Code:
    enum weekday{
        Sonntag,
        Montag,
        Dienstag,
        Mittwoch,
        Donnerstag,    
        Freitag,
        Samstag
        };
    
    weekday day;
    so this is what i defined for it. what i know is that every weekday equals a number, beginning with sonntag = 0, Montag = 1 and so on.
    So my question is now can i PRINT the weekday if i only have the number?
    for example:
    i have an..

    Code:
    int weekday = 2;
    how can i print weekday or day that the result look like this:

    Code:
    cout << day << endl;
    what i want it to look like:

    Dienstag

    what it acually look like:

    2


    does anyone has an idea?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You'll need to overload the << operator for your weekday type.

    This will get you started.

    Code:
    std::ostream& operator<< (std::ostream& os, weekday rhs)
    {
        switch (rhs)
        {
            // code to print the various values goes here
        }
        return os;
    }
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    3
    okay, what is the os?
    Last edited by grewq; 12-10-2014 at 02:48 PM.

  4. #4
    Registered User
    Join Date
    Dec 2014
    Posts
    3
    like this just with more prints?


    Code:
    ostream& operator<< (ostream& os, weekday rhs){
        switch (rhs){
            case 0:
            cout << "Sonnstag" << endl;
            }
        }

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by grewq View Post
    okay, what is the os?
    os is the ostream object. If you're printing to the console, it's cout, but it could be a file-based ostream (ofstream) or a string-based (ostringstream) stream. In your example, replace cout with os, add a break after each case, and make sure to return os.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe with enum. Assigning a char to an enum value.
    By Iceboxes in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2013, 09:58 AM
  2. Assigning enum types to enum types?
    By see the big C in forum C Programming
    Replies: 10
    Last Post: 12-21-2010, 02:32 AM
  3. enum
    By siperi in forum C Programming
    Replies: 1
    Last Post: 11-03-2010, 11:35 PM
  4. Problems with Enum and Files
    By Robert_Sitter in forum Windows Programming
    Replies: 7
    Last Post: 11-25-2005, 04:31 PM
  5. Enum
    By swgh in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2005, 11:55 AM

Tags for this Thread