All i want help with is changing the number value I enter in scanf such as 1 = January, 2 = February, etc. The code works fine as is and everything Just want the month name to display in the header as opposed to the number of the month. Code is below.
Code:
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>
int getdaycode(int month, int year);
void printheader(int month, int year);
int getndim(int month, int year);
int main(void)
{
int day, month, year, nummonths;
printf("Enter Month, Year, and number of Months: ");
if (scanf("%d %d %d", &month, &year, &nummonths) != 3
|| year < 0 || month < 1 || month > 12)
{
printf("invalid input\n");
return 1;
}
for (int i = 0; i < nummonths; i++)
{
printheader(month, year);
int numdays = getndim(month, year);
int daycode = getdaycode(month, year);
printf("%*s", daycode * 4, ""); /* print 4 spaces for each skipped day */
for (day = 1; day <= numdays; day++)
{
printf("%3d", day);
daycode = (daycode + 1) % 7;
if (daycode != 0)
printf(" ");
else
printf("\n");
}
if (daycode != 0)
printf("\n");
printf("\n");
month = month + 1;
if (month > 12)
{
month -= 12;
year += 1;
}
}
return 0;
}
int getdaycode(int month, int year)
{
int numdays;
{
numdays = ((year - 1) * 365 + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400)); // how many days including exceptions
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) //check if leapyear
{
if (month == 1) // January
numdays = numdays;
if (month == 2) // February
numdays = numdays + 31;
if (month == 3) // March
numdays = numdays + 28 + 31 + 1;
if (month == 4) // April
numdays = numdays + 31 + 28 + 31 + 1;
if (month == 5) // May
numdays = numdays + 30 + 31 + 28 + 31 + 1;
if (month == 6) // June
numdays = numdays + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 7) // July
numdays = numdays + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 8) // August
numdays = numdays + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 9) // September
numdays = numdays + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 10) // October
numdays = numdays + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 11) // November
numdays = numdays + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
if (month == 12) // December
numdays = numdays + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31 + 1;
}
else
{
if (month == 1) // January
numdays = numdays;
if (month == 2) // February
numdays = numdays + 31;
if (month == 3) // March
numdays = numdays + 28 + 31;
if (month == 4) // April
numdays = numdays + 31 + 28 + 31;
if (month == 5) // May
numdays = numdays + 30 + 31 + 28 + 31;
if (month == 6) // June
numdays = numdays + 31 + 30 + 31 + 28 + 31;
if (month == 7) // July
numdays = numdays + 30 + 31 + 30 + 31 + 28 + 31;
if (month == 8) // August
numdays = numdays + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month == 9) // September
numdays = numdays + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month == 10) // October
numdays = numdays + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month == 11) // November
numdays = numdays + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
if (month == 12) // December
numdays = numdays + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + 28 + 31;
}
}
int daycode = (numdays + 1) % 7;
return daycode;
}
void printheader(int month, int year)
{
printf("%14d %1d\n", month, year);
printf("Sun ");
printf("Mon ");
printf("Tue ");
printf("Wed ");
printf("Thu ");
printf("Fri ");
printf("Sat\n");
}
int getndim(int month, int year)
{
int numdays;
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) //check if leapyear
{
if (month == 1) // January
numdays = 31;
if (month == 2) // February
numdays = 29;
if (month == 3) // March
numdays = 31;
if (month == 4) // April
numdays = 30;
if (month == 5) // May
numdays = 31;
if (month == 6) // June
numdays = 30;
if (month == 7) // July
numdays = 31;
if (month == 8) // August
numdays = 31;
if (month == 9) // September
numdays = 30;
if (month == 10) // October
numdays = 31;
if (month == 11) // November
numdays = 30;
if (month == 12) // December
numdays = 31;
}
else
{
if (month == 1) // January
numdays = 31;
if (month == 2) // February
numdays = 28;
if (month == 3) // March
numdays = 31;
if (month == 4) // April
numdays = 30;
if (month == 5) // May
numdays = 31;
if (month == 6) // June
numdays = 30;
if (month == 7) // July
numdays = 31;
if (month == 8) // August
numdays = 31;
if (month == 9) // September
numdays = 30;
if (month == 10) // October
numdays = 31;
if (month == 11) // November
numdays = 30;
if (month == 12) // December
numdays = 31;
}
return numdays;
}