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...
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: