1

I do non know how to call my getdaycode function correctly into my main function. Also, when that isn't the problem, the day showing up below header and it messes with the ordering of the days messing up the calendar. Those are my two big problems at this point, I will enjoy working through this with someone... I am really confused.. I am brand new at programming with no background. (This makes it very difficult). For any help Appreciated!

#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()
{
    int  day, month, year, nummonths;
    {
        printf("Enter Month, Year, and Number of Months");
        scanf("%d %d %d", &month, &year, &nummonths);

        for (nummonths; nummonths < 13; nummonths++)
            if (nummonths > 13)
                year = year + 1;

        printheader(month, year);
        int numdays = getndim(month, year);
        int daycode = getdaycode(month,  year);

        for (day = 1; day <= 1 + daycode * 4; day++)
            printf(" ");

        for (day = 1; day <= numdays; day++) 

        {
            printf("%2d", day);
            if ((day + daycode) % 7 > 0)

                printf("   ");
            else
                printf("\n");
        }
        daycode = (daycode + numdays) % 7;
    }
}

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 % 7;
        switch (daycode)
        {
        case 0:
            printf("Sunday\n");
            break;

        case 1:
            printf("Monday\n");
            break;

        case 2:
            printf("Tuesday\n");
            break;

        case 3:
            printf("Wednesday\n");
            break;

        case 4:
            printf("Thursday\n");
            break;

        case 5:
            printf("Friday\n");
            break;

        case 6:
            printf("Saturday\n");
            break;

        default: printf("unexpected error (daycode case) daycode = %d", daycode);
            break;
        }
        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;
}
2
Contributors
1
Reply
31
Views
2 Months
Discussion Span
Last Post by Reverend Jim
0

Before we get to your problem, let's address a more fundamental problem. A block of code like

       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;

simply shouts out to be replaced by something like

numdays = MappingArray(month)

where the entries in MappingArray are the individual sums from each if block. There are several places in your code where this should be done.

getdaycode should (must?) have a comment or two above it that says what it does/returns. It needs a much more descriptive name. It also has a hidden purpose which is to print out a day name. Make that a separate function. Likewise for the leap year determination. It will be useful elsewhere. Make it a separate function.

getndim needs a header/comment and a more descriptive name.

Edited by Reverend Jim

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.