static_cast and enum value

Why can't I sucessfully get the enum value from the static cast? What am I missing?

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
#include <iostream>
using namespace std;

struct Date{
    int dayOfWeek;
    int dayOfMonth;
    int month;
    int year;
};

int main(){
    Date userDate;
    string userDay;

    enum DayOfWeek {Sunday = 1,
                    Monday = 2,
                    Tuesday = 3,
                    Wednesday = 4,
                    Thursday = 5,
                    Friday = 6,
                    Saturday = 7};

    cout << "What day of the week is it (1-7)?" << endl;
        cin >> userDate.dayOfWeek;
    cout << "What day of the month is it (1-31)?" << endl;
        cin >> userDate.dayOfMonth;
    cout << "What month is it (1-12)?" << endl;
        cin >> userDate.month;
    cout << "What year is it (####)?" << endl;
        cin >> userDate.year;


    DayOfWeek dayString = static_cast<DayOfWeek>(userDate.dayOfWeek);

    cout << dayString << ", " << userDate.dayOfMonth << "/" << userDate.month << "/" << userDate.year << endl;

    return 0;
}
C++ doesn't support converting enum values to strings. Once you've compiled the program, the names "Sunday", "Monday", etc. are gone.
You can do
1
2
3
4
5
6
7
const char *to_string(DayOfWeek d){
    switch (d){
        case Sunday:
            return "Sunday";
        //...
    }
}
at which point the enum becomes kind of pointless.
enums are named constant integers. it looks to me like it works; were you expecting to see the name of the enum variable as a string or something? I don't get any errors and it seems to do what it does ok....
Thanks guys. I must have misunderstood something because this is the hint my Prof gave

Hint: For input, you can convert an int into an enum with static_cast.
1
2
3
4
DayOfWeek day;
int dayNumber;
cin >> dayNumber;
day = static_cast<DayOfWeek>(dayNumber);


and the output I am supposed to get should look like this:
"Tuesday, 6/11/2020"
You can use some macro "magic" to simplify the to_string() code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#define DAYOFWEEK_TO_STR(X) case X: \
    return #X;

enum DayOfWeek
{
    Sunday = 1,
    Monday = 2,
    // ...
};

const char *to_string(const DayOfWeek d)
{
    switch (d)
    {
        DAYOFWEEK_TO_STR(Sunday)
        DAYOFWEEK_TO_STR(Monday)
        // ...
    }
}


________

Another option would be using a std::unordered_map instead of enum:

1
2
3
4
5
6
7
8
9
10
11
12
const std::unordered_map<int, std::string> DayOfWeek =
{
    { 1, "Sunday" },
    { 2, "Monday" },
    // ....
};

const char *to_string(const int d)
{
    std::unordered_map<int, std::string>::const_iterator ret = DayOfWeek.find(d);
    return (ret != DayOfWeek .end()) ? ret->second.c_str() : "N/A";
}
Last edited on
you can indeed convert an int to an enum. But, an enum is just an integer, so you haven't accomplished much there beyond silencing an warning from the compiler.
If you want words, you need some other way.
Perhaps something like:

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
#include <iostream>

struct Date {
	unsigned dayOfWeek {};
	unsigned dayOfMonth {};
	unsigned month {};
	unsigned year {};
};

enum DayOfWeek { Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

constexpr const char* days[Saturday] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

int main() {
	Date userDate;

	do {
		std::cout << "What day of the week is it (1 - 7)? ";
		std::cin >> userDate.dayOfWeek;
	} while ((userDate.dayOfWeek < 1 || userDate.dayOfWeek > 7) && (std::cout << "Invalid week day\n"));

	do {
		std::cout << "What day of the month is it (1 - 31)? ";
		std::cin >> userDate.dayOfMonth;
	} while ((userDate.dayOfMonth < 1 || userDate.dayOfMonth > 31) && (std::cout << "Invalid month day\n"));

	do {
		std::cout << "What month is it (1 - 12)? ";
		std::cin >> userDate.month;
	} while ((userDate.month < 1 || userDate.month > 12) && (std::cout << "Invalid month\n"));

	std::cout << "What year is it (####)? ";
	std::cin >> userDate.year;

	std::cout << '\n' << days[static_cast<DayOfWeek>(userDate.dayOfWeek) - 1] << ", " << userDate.dayOfMonth << "/" << userDate.month << "/" << userDate.year << '\n';
}



What day of the week is it (1 - 7)? 3
What day of the month is it (1 - 31)? 6
What month is it (1 - 12)? 11
What year is it (####)? 2020

Tuesday, 6/11/2020

Topic archived. No new replies allowed.