Trouble Printing Contents of Enum

I'm writing a program that will simply print out the month of January for 2015. What I've done is to create two enums, one with just the month of January and one for each day of the week. I then made a struct of the Month and the Day. Then in my displayMonth function I've made it loop through the number of days to print each day of the month.

The problem I am having is once i have gone beyond 8 it stops finding anything in the switch statement to print and can only print the month switch state since it's the only one.

I need to find a way to reset the loop counter but still have it stop once it reaches the total number of days in the month (31 days). Any help is greatly appreciated.

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

using namespace std;

#define DAYS 31

enum Month { JAN }; //, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };
enum Day { THR, FRI, SAT, SUN, MON, TUE, WED };

struct calendar_t
{
    Month month;
    Day day;
};

//Function Prototypes
void displayMonth(calendar_t[]);


int main()
{
    calendar_t cal[DAYS];
    
    displayMonth(cal);
    
    return 0;
}

void displayMonth(calendar_t cal[])
{
    calendar_t  calendar[DAYS];
    
    for (int i = 0; i < 31; i ++)
    {
        calendar[i].month = static_cast<Month>(i);
        calendar[i].day = static_cast<Day>(i);
    }
    
    for ( int i = 0; i < 31; i ++)
    {
        switch (calendar[i].day)
        {
            case THR: cout << "Thursday, ";
                break;
            case FRI: cout << "Friday, ";
                break;
            case SAT: cout << "Saturday, ";
                break;
            case SUN: cout << "Sunday, ";
                break;
            case MON: cout << "Monday, ";
                break;
            case TUE: cout << "Tuesday, ";
                break;
            case WED: cout << "Wednesday, ";
                break;
        }
        
        
        switch (calendar[0].month)
        {
            case JAN: cout << "January " << i + 1;
        }
        
        cout << endl;
    }
    
}
Line 14: day shouldn't really be a enum here. You defined your enum as names for the values 0 - 6. day should really be in an int, since it ranges from 1- 31.

Line 34: We want the value of i to go from 1 to 31.

Line 36: You're setting the month from 0-30.

Line 30: You're passing in cal as an argument, but don't use it.

Line 40: Ditto line 34.

Line 42: calendar[i].day goes from 0 - 30.

Try something like this (untested):
1
2
3
4
5
6
7
8
9
  // I'm renaming and reordering your enum
  enum DOW { SUN, MON, TUE, WED, THR, FRI, SAT };

  DOW dow;  // Day of week
...
  dow = (calendar[i].day + 4) % 7;   // We know Jan 2015 started on Thursday
  switch (dow)
  {
...

Last edited on
Topic archived. No new replies allowed.