SRL Archive - C/C++ Help and Tutorials

enum


Shuttleu

2009-08-15 23:12:18

in the tutorial it told me to do this
#include <iostream> 

using namespace std;

int main() {
enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

int x;
Days DayOff;

cout << "What day would you like off (0-6)? ";
cin >> x;
DayOff = Chapters(x);
if (DayOff == Sunday || DayOff == Saturday)
cout << "\nYou're already off on weekends!\n";
else
cout << "\nOkay, your now off on that day.\n";
return 0;
}
but cant DayOfff jst be declared as a int?
because Chapters(x) returns an int
so what is the advantage of declaring it using the enum name instead of an int

~shut

senrath

2009-08-15 23:13:33

As far as I know, they're mostly used to avoid spaghetti code.

Shuttleu

2009-08-15 23:18:54

so there isnt any advantage of declaring it as "Days DayOff;" instead of "int DayOff;"
?

~shut

JAD

2009-08-16 00:04:34

The reason for enums (short for enumeration) is instead of using an integer to represent a set of values, a type with a restricted set of values is used instead. The compiler will limit assignments to variables of the same type which prevents out of range errors from occurring.

Does that make sense? Yes, you could just use an integer, but it would take more code because you would need to use a switch statement on x (just like cases in SCAR if you didn't know) to go through the days and find the one the user picked instead of using chapter(x).

Shuttleu

2009-08-16 00:56:06

so what would the advantage be?
take these two as an example
#include <iostream> 

using namespace std;

int main() {
enum Chapters {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

int x;
Chapters DayOff;

cout << "What day would you like off (0-6)? ";
cin >> x;
DayOff = Chapters(x);
if (DayOff == Sunday || DayOff == Saturday)
cout << "\nYou're already off on weekends!\n";
else
cout << "\nOkay, your now off on that day.\n";
return 0;
}
#include <iostream> 

using namespace std;

int main() {
enum Chapters {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

int x;

cout << "What day would you like off (0-6)? ";
cin >> x;
if (x == Sunday || x == Saturday)
cout << "\nYou're already off on weekends!\n";
else
cout << "\nOkay, your now off on that day.\n";
return 0;
}
?

~shut

JAD

2009-08-16 00:58:26

x wouldn't equal sunday or saturday in the second script. It would equal a number 0 - 6. The first script works because the chapter(x) converts the number into the appropriate day in the enum above that.


BTW, the script in the thread post wouldn't compile because chapter is supposed to be Days :p

Shuttleu

2009-08-16 01:01:50

the second one does work though
because saturday = 6 and sunday = 0

~shut

EDIT: and i have tried it

JAD

2009-08-16 01:04:02

From: Shuttleu
the second one does work though
because saturday = 6 and sunday = 0

~shut


Wait I was wrong.

Yes it would work because Sunday = 0…etc. because you declared it in the enum. You could do it your way, but it is pretty improper and bad practice. You really should use a data type of the enum to do it instead.

There are instances where it may be necessary to use the data type though. This is just for you to understand the concept. You could also do this without an enum:


#include <iostream>

using namespace std;

int main() {
int x;

cout << "What day would you like off (0-6)? ";
cin >> x;
if (x == 0 || x == 6)
cout << "\nYou're already off on weekends!\n";
else
cout << "\nOkay, your now off on that day.\n";
return 0;
}

So the enum is completely unnecessary in this script. It is just showing you the concept in its simplest form. But yes, you could avoid using the data type you created and just use an int in this case.

Shuttleu

2009-08-16 01:10:59

From: JAD
I don't understand… Why would x = Saturday or Sunday if

1) x is an integer

2) x is equal to the number 0-6 that the user inputted?

Did you actually run the second script? Maybe I'm really missing something, but it shouldn't work.


but sunday =0 and saturday =6
so it should and will work

~shut

EDIT: ok well my next question is if i do this
#include <iostream> 

using namespace std;

int main() {
enum Chapters {Sunday = 100, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

int x;
Chapters DayOff= Chapters(x);

cout << "What day would you like off (0-6)? ";
cin >> x;
DayOff = Chapters(x);
if (DayOff == Sunday || DayOff == Saturday)
cout << "\nYou're already off on weekends!\n";
else
cout << "\nOkay, your now off on that day.\n";
return 0;
}
sunday =100
monday = 101
tuesday = 102
etc.
but if i do Chapters(2) it returns 2 instead of 102

boberman

2009-08-23 03:44:10

From: JAD
The reason for enums (short for enumeration) is instead of using an integer to represent a set of values, a type with a restricted set of values is used instead. The compiler will limit assignments to variables of the same type which prevents out of range errors from occurring.

Does that make sense? Yes, you could just use an integer, but it would take more code because you would need to use a switch statement on x (just like cases in SCAR if you didn't know) to go through the days and find the one the user picked instead of using chapter(x).


It doesn't use more code, nor would you have to use a case statement. Using enums is all about readability, however, anything done with an enum can be done with integers instead, IE Chapters returns something like 6 and 7, so instead of saying Saturday or sunday, he would say 6 or 7.

JAD

2009-08-23 03:50:49

From: boberman
It doesn't use more code, nor would you have to use a case statement. Using enums is all about readability, however, anything done with an enum can be done with integers instead, IE Chapters returns something like 6 and 7, so instead of saying Saturday or sunday, he would say 6 or 7.


Well if the


enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

Wasn't there, then he would need a switch statement.


cin >> x;
switch(x)
{
case 0: cout << "Sunday";
case 1: cout << "Monday";
//....
}

That was what I meant. I guess you could use a normal array to hold those values, but I had misread the question at first.

boberman

2009-08-23 03:54:09

From: JAD
Well if the


enum Days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

Wasn't there, then he would need a switch statement.


cin >> x;
switch(x)
{
case 0: cout << "Sunday";
case 1: cout << "Monday";
//....
}

That was what I meant. I guess you could use a normal array to hold those values, but I had misread the question at first.


if you wanted to display the day's name an enum wouldn't help you there. if you have an enum like he had there and said cout << Days; it would print out the integer value of the enum, not the title of the enum.