#include <iostream.h>
int main()
{
       enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,          Â_Saturday };

       Days DayOff;
       int x;

       cout << "What day would you like off (0-6)? ";
       cin  >> x;
       DayOff = Days(x);
       
       if (DayOff == Sunday || DayOff == Saturday)
                   cout << "\nYou're already off on weekends!\n";
       else?
                   cout << "\nOkay, I'll put in the vacation day.\n";   
       return 0;
}

This code appears in an example problem in the tutorial entitled "Teach Yourself c++ in 21 days." I've already started a few other tutorials (they weren't very good), so most of this is review for me. But enums were never covered.

In line 6, what the heck does that mean. It looks like he's declaring variables, but there is no type. Do you think this is just an error.

In line 11, what does days(x) mean? I'm going to guess this is a reference to the values that correspond to each day of the week in the enum. Additionally, it happens to be an x, because cin calls for x.

Lastly, in line 13, where it says "if (DayOff == Sunday || DayOff == Saturday)", could I have used the values of Sunday and Saturday instead. For example, could I have written if (DayOff == 0 || DayOff == 6)?

Thank you in advance.

Recommended Answers

All 6 Replies

>It looks like he's declaring variables, but there is no type.
He is declaring a variable, and the type of the variable is enum Days . DayOff is an instance of the Days enumeration.

>In line 11, what does days(x) mean?
As an example, Days(1) is the same thing as Monday. It's giving you the Days constant that corresponds to the integer value in x. A much more interesting question is what happens if x holds a value outside of the range 0-6? ;)

>For example, could I have written if (DayOff == 0 || DayOff == 6)?
Yes.

>
A much more interesting question is what happens if x holds a value outside of the range 0-6? ;)

I'll take a guess that the text wraps around to the beginning, in a sense. If I declared a unsigned short int of value 65,536, the value would wrap around to 0. Similarly, I'd bet that if called for the value 7, that value would correspond to Sunday, or 0.

Thank you for the help :)

Member Avatar for iamthwee

Narue forgot to mention that #include <iostream.h> should actually be #include <iostream> ... using namespace prefixes.

If you are using a compiler that doesn't throw up warning messages about that - maybe it is time to update to something newer.

>I'll take a guess that the text wraps around to the beginning, in a sense.
Excellent. Now prove your theory[1]. :)

[1] Note that proof doesn't consist only of "it works on my computer". Real proof means you can quote the C++ standard and/or prove that this is predictable behavior everywhere. For that I'd recommend shimmying over to Google groups and looking around comp.lang.c++, comp.lang.c++.moderated, and comp.std.c++. You're likely to get the full story there.

Narue forgot to mention that #include <iostream.h> should actually be #include <iostream> ... using namespace prefixes.

If you are using a compiler that doesn't throw up warning messages about that - maybe it is time to update to something newer.

I was actually curious about this. I have seen both routines used in various examples. One the former causes my compiler to print a warning message claiming the function is "depreciate or antiquated." I've simply started ignoring this message, because my programs have been working, and <iostream.h> is the function that my book has been using. However, I am curious what the difference between the two functions is. What are some differences between the two which can affect the outcome of my program?

@Narue: I think I will read-up on those topics.

Thanks for the help guys.

The standard says that you should be using namspaces, that the iostream header file is located in namespace std, and that the iostream header file should be included as iostream not as iostream.h. The technical differences between iostream and iostream.h header files are so minute that probably less than 10 people (and I'm not in that group) posting to the board can tell you what they are, or can even find them in the documentation, so don't worry about techicalities like that at this time. It will just make your head spin without advancing your skills. When you become more accomplished and can appreciate the minor differences, then you can look up the documentation yourself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.