Enum help

So I'm just doing a simple code to show use of enumerations in a code of our choice. I kind of understand how they work and I'm just doing a simple code where you enter the week day and it'll tell you what tomorrow is. My issue is I don't know how to output the corresponding week day with the number entered. I may be confused but the index position should go {sunday (0), monday (1)...} I don't know if I'm thinking of arrays but could somebody be so kind to help me as if the tommorow variable equals 0 it'll output "sunday" so on and so forth...

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

using namespace std;

enum weekDays {sunday, monday, tuesday, wednesday, thursday, friday, saturday};

int getDay()
{
    int day;
    cout << "Enter today: ";
    cin >> day;
    cout << endl;
    
    while(!cin || day < 0 || day > 7)
    {
        if(!cin)
        {
            cout << "You must enter a number" << endl;
            cin.clear();
            cin.ignore(INT_MAX, '\n');
        }
        else if(day < 0)
        {
            cout << "You must enter a number 0 - 7" << endl;
        }
        else
        {
            cout << "You must enter a number 0 - 7" << endl; 
        }
        cout << "Enter the day: ";
        cin >> day;
        cout << endl;
        
    }
    return day;
}

int nextDay(int day)
{
    int tommorow = day + 1;
    if(tommorow == 8)
    {
        tommorow = 0;
    }
    return tommorow;
}

int main()
{
    int day = getDay();
    cout << "Today is " << day << endl;
    int tommorow = nextDay(day);
    cout << "Tommorow will be " << tommorow << endl;
    
    
    return 0;
}
Is this specifically an exercise to learn how to use enums, or is a solution without enums acceptable?

"You must enter a number 0 - 7"
I think you have some off-by-one errors. Are there 7 days or 8 days in a week? :)

The problem is that there is inherently no logical mapping between the integers of an enum and strings like "Tuesday"or "Wednesday". Enums are used to help avoid magic numbers like
1
2
3
4
if (option == 42) // wtf does 42 mean?
{
    // ...
}


You could use some clever macro magic to map the numbers to string literals, but I personally avoid it if possible.

To convert an int to a string for the day of the week, consider an array, like:

1
2
3
4
5
6
7
8
9
10
11
// expected input: [1, 7], inclusive
std::string day_of_the_week(int day)
{
    if (day <= 0 || day > 7)
        return ""; // invalid, return empty string

    static const string days[] = { "Sunday", "Monday", "Tuesday", 
        "Wednesday", "Thursday", "Friday", "Saturday" };

    return days[day - 1];
}

Last edited on
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
#include <iostream>
#include <string>
#include <limits>

enum weekDays { sunday, monday, tuesday, wednesday, thursday, friday, saturday };

std::string dayName(weekDays day)
{
	static const std::string names[7] {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

	return names[day];
}

weekDays getDay()
{
	int day {};

	while ((std::cout << "Enter today (0 - 6): ")  && (!(std::cin >> day) || day < 0 || day > 6))
		if (!std::cin) {
			std::cout << "You must enter a number\n";
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		} else
			std::cout << "You must enter a number 0 - 6\n";

	return static_cast<weekDays>(day);
}

weekDays nextDay(weekDays day)
{
	weekDays tommorow {static_cast<weekDays>(day + 1)};

	if (tommorow == saturday + 1)
		tommorow = sunday;

	return tommorow;
}

int main()
{
	const auto day {getDay()};

	std::cout << "Today is " << dayName(day) << '\n';

	const auto tommorow {nextDay(day)};

	std::cout << "Tomorrow will be " << dayName(tommorow) << '\n';
}



Enter today (0 - 6): 6
Today is Saturday
Tomorrow will be Sunday

Last edited on
Topic archived. No new replies allowed.