Thread: Help with enumerated data types.

  1. #1
    Registered User
    Join Date
    Oct 2008

    Help with enumerated data types.


    just got a question, why should I use enum instead of an int?

    enum just gives me +1 from the predecessor I picked.
    why not use a fixed number?

    I can understand if im going to write out the numbers on the months of the entire year, then it can be good to get +1 instead of writing:
    january = 1; february = 2; etc etc etc

    but other than that, I just dont get it :P
    DONT link me to another homepage where it is explaining the enum, cuz I have read that.
    Give me some concrete examples.


  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Err, because you want to have a set of values, not every possible integer. Like if you have a set of car models, somebody in the program might pick something that doesn't exist. I mean he can say car.model = "Aubi". If you used enums and somebody (even you by mistake) wrote car.model = Aubi then you get a compile time error. Now, you could have a list of possible options for the cars, but then you have to makes checks like if( car.mode == "Audi" || ....) which is ineffective and bug friendly.
    So you use enums.
    As everything, it helps you, the programmer. If you make something small and simple they are not needed. If you make something big with lots of programmers, then it is a good idea

  3. #3
    Registered User
    Join Date
    Oct 2008
    Aight thx.

    got an exercise where it says that Im going to write a menu program.
    with 3 difficulty levels.
    And I should use the enum to represent the 3 difficulty levels.

    Using switch(), isn't the default: there for correcting my mistakes? (that will say if I didnt press 1-3)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Which do you prefer?
    switch ( month ) {
      case 9: case 4: case 6: case 11:
        numdays = 30;
    or this
    switch ( month ) {
      case september: case april: case june: case november:
        numdays = 30;
    Enums add to the readability of the code.

    > enum just gives me +1 from the predecessor I picked.
    > why not use a fixed number?
    Not always, you can have
    red = 1, amber = 2, green = 4

    So why not have
    #define red 1
    #define amber 2
    #define green 4
    Enums have a measure of type safety, and would be understood by the debugger.

    So for example, consider
    enum { red = 1, amber = 2, green = 4 } traffic_t;
    enum { apple, orange, banana } fruit_t;
    traffic_t light = orange;  // it's a fruit, not a colour
    would draw some comment from the compiler.

    If you just had int, and a bunch of #defines, all the compiler would see is
    int light = 2;

    In a debugger, you're likely to see light = red (meaningful), rather than light = 0 (much harder work).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2008
    aaah okey, thx for the explanation, appriciate it =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extending basic data types.
    By nempo in forum C++ Programming
    Replies: 23
    Last Post: 09-25-2007, 03:28 PM
  2. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM