Thread: (C PROGRAM IN VISUAL STUDIO) Calendar Program. 1 small addition.

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    9

    (C PROGRAM IN VISUAL STUDIO) Calendar Program. 1 small addition.

    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;
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You can create an array of strings (each value corresponding the month name).

    The indexes will then serve as a representation for the month that you pass in to your header function.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    9
    Do you think something like this could work maybe in my printheader function?
    Code:
    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;
            }
    But with months

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Try it and see.

    And cross-posting is considered poor etiquette.

    How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    9
    Ok, I didn't know what thanks for letting me know.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-15-2016, 06:35 AM
  2. how to run a c program on visual studio command prompt
    By David_Baratheon in forum C Programming
    Replies: 8
    Last Post: 02-07-2012, 05:01 PM
  3. Executing C Program in Visual Studio 2008
    By rory-uk in forum C Programming
    Replies: 29
    Last Post: 07-10-2009, 01:53 PM
  4. Trouble running a program Visual Studio
    By Swerve in forum C++ Programming
    Replies: 8
    Last Post: 03-03-2008, 11:15 AM
  5. C++ console program using Ms Visual Studio .NET
    By matheo917 in forum C++ Programming
    Replies: 4
    Last Post: 09-05-2002, 11:16 PM

Tags for this Thread