Thread: enum types

  1. #1
    Unregistered
    Guest

    enum types

    Having probs getting to grips with enum types, specifically how to output an enum. When I write to disk or screen an enum type, it outputs the index number, not the name. I have tried making my methods return enum types and int, neither works.

    What is the trick???

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    enum day { monday, tuesday, wednesday, thursday, friday, saturday, sunday };
    
    day today = monday;
    
    char *getCharDay(day whichDay)
    {
    	switch (whichDay)
    	{
    	case 0:
    		return "monday";
    	case 1:
    		return "tuesday";
    	case 2:
    		return "wednesday";
    	case 3:
    		return "thursday";
    	case 4:
    		return "friday";
    	case 5:
    		return "saturday";
    	case 6:
    		return "sunday";
    	default:
    		return NULL;
    	}
    }
    
    cout << getCharDay(today);
    Not sure if I fully understood your question, but that would be one way.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Try something like this. You'll notice we use array of pointers to aid us in the example. Perhaps this is what you were asking.

    Code:
    #include <iostream>
    using namespace std;
    
    /* Enumerated types */
    enum WORKDAYS { Monday, Tuesday, Wednesday, Thursday, Friday };
    
    /* Array of character strings */
    const char *pDay[] = { "Monday", 
                           "Tuesday", 
                           "Wednesday", 
                           "Thursday", 
                           "Friday" };
    
    int main( void )
    {
      /* Keep track of current day */
      WORKDAYS currentDay = Thursday;
      
      /* This outputs our integer based index... */
      cout << currentDay << endl;
    
      /* We can use our array of pointers and index to get the day */
      cout << *(pDay + currentDay) << endl;
    
      return 0;
    }
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Try to remember enums aren't like structs and classes, they are in my opinion best used to represent related constants. I use them or things like male and female, left and right and similar things

    Code:
    enum Sex
    {
    Male,
    Female
    };
    Essentially they only represent numbers, so when you print one the number is what you get.
    Couldn't think of anything interesting, cool or funny - sorry.

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I like MrWizard's example except I would probably like to reference the name like this:

    pDay[currentDay]; // <- das a string!

  6. #6
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Originally posted by FillYourBrain
    I like MrWizard's example except I would probably like to reference the name like this:

    pDay[currentDay]; // <- das a string!
    Make sure your enums are numbered correctly though, can't remember if they are 0-based....
    Couldn't think of anything interesting, cool or funny - sorry.

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I'm not unregistered by the way (in case that seemed that way)

  8. #8
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    FYB, what Mr. Wizard did was an optimization. His way, all though not as clear, accesses the string more quickly.

  9. #9
    Nick
    Guest
    Well you can always compile it and see for yourself but
    I'd be supprised if the optimized code was different.

  10. #10
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Nick, you wouldn't see the difference. It would only be recognized in many many iterations.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by endo


    Make sure your enums are numbered correctly though, can't remember if they are 0-based....
    If you do not assign a value, the first enumeration will default to zero. Each enumeration after the first will be incremented once. Thus:

    enum color { Black, Red, Green, Blue, Yellow, Cyan, Magenta, White };

    Black = 0, Red = 1, Green 2, ...

    Furthermore, had we done this:

    enum color { Black = 100, Red, Green, Blue, Yellow, Cyan, Magenta, White };

    Then our enums would be as follows:

    Black = 100, Red = 101, Green = 102, ...

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    i recommend this for the faq

  13. #13
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    If you need functionality such as printing out the name of a particular enum, it would be a lot more logical to create a class.

    Once you add functions, and string arrays, its really going above and beyond what an enums job is.
    Edit:: Oooh, I get an error when trying to rate this thread.

  14. #14
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Originally posted by Eibro
    Edit:: Oooh, I get an error when trying to rate this thread.
    I don't , I just gave it healthy 4
    Couldn't think of anything interesting, cool or funny - sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 12:57 PM
  2. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Incrementing enum types
    By JarretteBeast in forum C++ Programming
    Replies: 21
    Last Post: 04-17-2003, 08:34 AM