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.
#include<iostream>
#include<cmath>
usingnamespace 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;
}
#include<iostream>
#include<cmath>
usingnamespace 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
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 ):
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.