I am writing a program that get month
and year
from the user, and then print out the calendar. But the calendar is only correct in January (every two years).
How do I make the other months correct? What am I doing wrong?
#include "stdafx.h"
void printMonth (int* startDay, int* days);
int _tmain(int argc, _TCHAR* argv[])
{
int startDay, days, year, month, a;
printf("enter year: ");
scanf("%d", &year);
printf("enter month: ");
scanf("%d", &month);
a = year - 1;
startDay = ((a * 365) + (a / 4) - (a / 100) + (a / 400) + 1) % 7;
switch (month)
{
case 1: days = 31;
break;
case 2: days = 28;
break;
case 3: days = 31;
break;
case 4: days = 30;
break;
case 5: days = 31;
break;
case 6: days = 30;
break;
case 7: days = 31;
break;
case 8: days = 31;
break;
case 9: days = 30;
break;
case 10: days = 31;
break;
case 11: days = 30;
break;
case 12: days = 31;
break;
}
printMonth (&startDay, &days);
printf("\nstart day: %d\n", startDay);
printf("\na: %d\n", a);
return 0;
}
void printMonth (int* startDay, int* days)
{
int weekDay;
printf("Sun Mon Tue Wed Thu Fri Sat\n");
printf("--- --- --- --- --- --- ---\n");
for (weekDay = 0; weekDay < *startDay; weekDay++)
printf(" ");
for (int dayCount = 1; dayCount <= *days; dayCount++)
{
if (weekDay > 6)
{
printf("\n");
weekDay = 1;
}
else
weekDay++;
printf("%3d ", dayCount);
}
printf("\n--- --- --- --- --- --- ---\n");
return;
}