Can't quite figure out my Calendar Program

I'm trying to write a program that prompts the user for a year and the first day of the year, and then displays a full calendar for that year, but I'm having some trouble getting the start date to change from month to month and can't figure out why. Any tips would be 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include<iostream> 
#include<cmath> 
using namespace std;
int main( ) 
{ 
    int n_days, start_day, year, counter=0;
    
    cout << "What year would you like to see a calendar for? \n";
    cin >> year;
    
    cout << "Enter the first day of the month. 0 for Sunday ... 6 for Saturday\n";
    cin >> start_day;
    
    while(counter < 12)//This loop displays calender
    {
          switch(counter)//sets the month and total days per month
          {
                case 0:
      		cout << "January \n";
      		n_days=31;
      		break;
     
     		case 1:
	        if(year%4 == 0)
      		{
         	n_days=29;
      		}
      		else
      		{
         	n_days=28;
      		}
      		cout << "February \n";
      		break;
     
     		case 2:
      		cout << "March \n";
      		n_days=31;
     	 	break;
     
     		case 3:
      		cout << "April \n";
      		n_days=30;
      		break;
     
     		case 4:
      		cout << "May \n";
      		n_days=31;
      		break;
     
     		case 5:
      		cout << "June \n";
      		n_days=30;
      		break;
     
     		case 6:
      		cout << "July \n";
      		n_days=31;
      		break;
     
     		case 7:
     	 	cout << "August \n";
      		n_days=31;
      		break;
     
     		case 8:
      		cout << "September \n";
      		n_days=30;
      		break;
     
     		case 9:
      		cout << "October \n";
      		n_days=31;
      		break;
     
     		case 10:
      		cout << "November \n";
      		n_days=30;
      		break;
     
    	 	case 11:
      		cout << "December \n";
      		n_days=31;
      		break;
     	}
        
	cout << "Sun\tMon\tTue\tWed\tThr\tFri\tSat\n"; 
        
	//this loop sets the position for the starting day
        for (int i = 0; i < start_day; i++) 
        	{ 
            		cout << " \t"; 
        	}
        
	//This loop displays the days 
        for (int j = (1+start_day); j <= (n_days+start_day); j++) 
        	{ 
            		cout << (j-start_day) <<"\t"; 
            
            	if( j%7 == 0) //ends the line at the end of each week
            	{
                	cout << endl;            
            	}      
                
        	}
        
	cout << endl;
        cout << endl; 
        
	counter++;
}

        cout << endl; 
        
system("PAUSE");        
return 0; 
}
Last edited on
A couple of advices that will make your code more readable and, eventually, your thread more likely to be attended:

Use code tags to display your code (see at the right of the windows you write in, look for a button like this:<>).

Indent your code. As a result, we should see:
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<iostream> 
#include<cmath> 
using namespace std;
int main( ) 
{ 
   int n_days, start_day, year, counter=0;

   cout << "What year would you like to see a calendar for? \n";
   cin >> year;

   cout << "Enter the first day of the month. 0 for Sunday ... 6 for Saturday\n";
   cin >> start_day;

   while(counter < 12)//This loop displays calender
   {
      switch(counter)//sets the month and total days per month
      {
         case 0:
         cout << "January \n";
         n_days=31;
         break;
   
         case 1:
         if(year%4 == 0)
         {
            n_days=29;
         }
         else
         {
         n_days=28;
         }
         cout << "February \n";
         break;

         case 2:
         cout << "March \n";
         n_days=31;
         break;
   
         case 3:
         cout << "April \n";
         n_days=30;
         break;
      
         case 4:
         cout << "May \n";
         n_days=31;
         break;
   
         case 5:
         cout << "June \n";
         n_days=30;
         break;
   
         case 6:
         cout << "July \n";
         n_days=31;
         break;
   
         case 7:
         cout << "August \n";
         n_days=31;
         break;
      
         case 8:
         cout << "September \n";
         n_days=30;
         break;
   
         case 9:
         cout << "October \n";
         n_days=31;
         break;
   
         case 10:
         cout << "November \n";
         n_days=30;
         break;
   
         case 11:
         cout << "December \n";
         n_days=31;
         break;
      }
      cout << "Sun\tMon\tTue\tWed\tThr\tFri\tSat\n"; 

      //this loop sets the position for the starting day
      for (int i = 0; i < start_day; i++) 
      { 
         cout << " \t"; 
      }
      //This loop displays the days 
      for (int j = (1+start_day); j <= (n_days+start_day); j++) 
      { 
         cout << (j-start_day) <<"\t"; 

         if( j%7 == 0) //ends the line at the end of each week
         {
            cout << endl; 
         } 

      }
      cout << endl;
      cout << endl; 
      counter++;
   }

   cout << endl; 

   system("PAUSE"); 
   return 0; 
}


Now it should become more clear what the problem with the code is
Thank you for the advice. This was my first post here and I didn't think to look for something like that.
I don't see any problem in the code. In fact, I run it and it worked fine! Are you still getting trouble??

The code itself runs with no actual errors. Its just that all of the months start from the same day and I can't wrap my head around how to change that.
You should include something after line 102. Next month will have a different starting day, wich is given by this mathematical calculation (this is not code, there's no need for new variables ):

new_starting_day = (............................ + ...........................) % 7

do you really want someone to tell you?
No, don't tell me what it is. I appreciate the help with my post formatting and the tip you just provided. I should be able to figure it out from here, I was just lost as how to set it up. Thanks.
You're wellcome, good luck with that!
Topic archived. No new replies allowed.