Going through an Enumerated List

When I try the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
enum DayNames {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

int main ()
    {
    DayNames dayOfWeek;
    for (int count=0; count<7; count++)
        {
        dayOfWeek = count;
        if ((dayOfWeek == Saturday) || (dayOfWeek == Sunday))
            cout << "Its the weekend - YEAH!!! No school today!" << endl;
        else
            cout << "...another day of school..." << endl;
        }
    return 0;
    }

I get a compile error saying:
error: invalid conversion from ‘int’ to ‘DayNames’

How do I add one (or go to the next enum value)? Do I have to typecast it to add one or what? This seems like it should be so simple, but isn't working as expected for some reason...

EDIT: Also when I cout dayOfWeek I get the number instead of the name, is there a way to get the name stored in the enum list instead?
Last edited on
dayOfWeek = (DayNames)count;

No, there is no automatic way of getting the string names as strings so you could cout << "It's " << dayOfWeek.GetName(); //or something like that and get "It's Monday".
Big time kudos on the
dayOfWeek = (DayNames)count;
I had a feeling typecasting was necessary, but was trying the other way around.

But the .GetName() comes up as:
error: request for member ‘GetName’ in ‘dayOfWeek’, which is of non-class type ‘DayNames’|

Is this part of a library maybe?
Please read carefully. I said "No, there is NO automatic way....". The GetName() thing was a sample of what it would have been nice to have.
Gotcha, guess I was overly excited about the type casting solution working. Many thanks webJose.

Final code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
enum DayNames {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

int main ()
    {
    DayNames dayOfWeek;
    for (int count=0; count<7; count++)
        {
        dayOfWeek = (DayNames)count;
        cout << dayOfWeek << " Its ";
        switch (dayOfWeek)
            {
            case Sunday: cout << "Sunday - ";
                break;
            case Monday: cout << "Monday - ";
                break;
            case Tuesday: cout << "Tuesday - ";
                break;
            case Wednesday: cout << "Wednesday - ";
                break;
            case Thursday: cout << "Thursday - ";
                break;
            case Friday: cout << "Friday - ";
                break;
            case Saturday: cout << "Saturday - ";
                break;
            }
        if ((dayOfWeek == Saturday) || (dayOfWeek == Sunday))
            cout << "its the weekend - YEAH!!! No school today!" << endl;
        else
            cout << "another day of school..." << endl;
        }
    return 0;
    }
Get in the habit of encapsulating reusable code. It will help you develop bigger things faster and cleaner. Here's a version with the day of the week functionality encapsulated. I also renamed the enum to Days because let's face it: It doesn't really provide the names.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <limits>

using std::cout;
using std::endl;
using std::cin;

//Here's a useful class to stop the console from closing when the program finishes executing.
class PauseConsole
{
public:
	~PauseConsole()
	{
		cout << endl << "Finished.  Press ENTER to exit. ";
		cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}
};

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

class WeekDay
{
private:
    //Member data
    Days m_id;

    //Constructors
public:
    WeekDay(Days dayID) : m_id(dayID)
    { }

    //Public interface
public:
    const char* GetName()
    {
        char *name = 0;
        switch (m_id)
        {
        case Sunday: name = "Sunday";
            break;
        case Monday: name = "Monday";
            break;
        case Tuesday: name = "Tuesday";
            break;
        case Wednesday: name = "Wednesday";
            break;
        case Thursday: name = "Thursday";
            break;
        case Friday: name = "Friday";
            break;
        case Saturday: name = "Saturday";
            break;
        }
        return name;
    }
    bool IsWeekendDay() { return (m_id == Saturday || m_id == Sunday); }
};

int main()
{
    PauseConsole __p;
    for(int dayID = Sunday; dayID <= Saturday; dayID++)
    {
        WeekDay day = (Days)dayID;
        cout << "It's " << day.GetName() << 
			(day.IsWeekendDay() ? " - it's the weekend - YEAH!!  No school today!"
                : " - another day of school...") << endl;
    }
    return 0;
}
Topic archived. No new replies allowed.