Thread: quick simple question regarding enums;

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    47

    quick simple question regarding enums;

    im reading 21 days to c++ and i came upon a curiosity.
    link: http://newdata.box.sk/bx/c/htm/ch03.htm

    Code:
    1:  #include <iostream.h>
    2:  int main()
    3:  {
    4:       enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
    5:
    6:       Days DayOff;
    7:       int x;
    8:
    9:       cout << "What day would you like off (0-6)? ";
    10:      cin  >> x;
    11:      DayOff = Days(x);
    12:
    13:      if (DayOff == Sunday || DayOff == Saturday)
    14:            cout << "\nYou're already off on weekends!\n";
    15:      else
    16:            cout << "\nOkay, I'll put in the vacation day.\n";
    17:       return 0;
    18: }
    on line 6 why does he write "Days DayOff;" instead of "char DayOff;" ?? whats the difference?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Because DayOff isn't supposed to be a char, it's a Days enum.

    An enum is basically a typedef for an int.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Days is an enum. It's like creating a new type. It is not a char, which is a type that holds a character. It is implemented as a type of integer, which is why x is an int, but it is its own type.

    BTW, this book (or at least the version you are reading) is extremely old. You will likely have to re-learn some things that have changed since it was written. You might want to consider getting a newer learning source.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    47
    what kind of newer learning source?
    can i find it on the internet?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There are tutorials on the internet, and there is Thinking in C++ by Bruce Eckel that is available for free on the internet. It's difficult to learn C++ well without an actual book though.

    You might consider going through a tutorial or two and seeing how that adds to what you're getting from the book you're using now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hopefully simple question, input streams
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2006, 01:59 PM
  2. Simple C++ question. Error in code somewhere.
    By Paracropolis in forum C++ Programming
    Replies: 10
    Last Post: 02-06-2006, 08:59 AM
  3. Quick question, should be simple to answer.
    By kabuatama in forum C Programming
    Replies: 4
    Last Post: 01-21-2006, 03:42 PM
  4. Quick question if i may
    By badboy16z in forum C++ Programming
    Replies: 1
    Last Post: 10-20-2003, 10:41 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM