1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help with calendar in C

Discussion in 'C' started by tastyTreeHUGGER, Nov 11, 2010.

  1. tastyTreeHUGGER

    tastyTreeHUGGER New Member

    Joined:
    Nov 11, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I am writing a calendar in C. I'm still very new to the language. I think i wrote most of the functions/algorithms correctly (it would be nice if you guys could check for any errors too). Now I'm having a tough time printing the calendar out In a correct format. For example, if I enter this data: 12/x/2010 to getMonthYear(). I am trying to get my calendar to start in the correct day, so the 1st is on Wednesday, 2nd Thursday, 3rd Friday... I should be able to enter any month and year and get the day to correspond with the correct weekday. All of this is going to have to be in the prototype printCalendar(). Any help is much appreciated.

    Code:
    #include <stdio.h>
    #define MAX 31
    #define MID 30
    #define MIN 28
    #define FOUR 4
    #define ONEHUN 100
    #define FOHUN 400
    #define BEGIN 1900
    #define YEAR 365
    
    //Prototypes:
    void getMonthYear(int *month, int *year);
    int toJulian(int month, int day, int year);
    int dayInMonth(int month, int year);
    int leapYear(int year);
    long yearsToDays(int year);
    void printCalendar(int startDay, int numDays);
    void printHeader();
    
    void main(void)
    {
    	int day, month, year, startDay, numDays;
    	
    	startDay = 1;
    	numDays = 0;
    	day = 0;
    
    	getMonthYear(&month, &year);
    	toJulian(month, day, year);
    	yearsToDays(year);
    	printCalendar(startDay, numDays);
    }
    //Ask user to imput data
    void getMonthYear(int *month, int *year)
    {
    	printf("Enter Month: ");
    	scanf_s("%d", month);
    	printf("Enter Year: ");
    	scanf_s("%d", year);
    	printf("\n\n");
    }
    //Takes a calendar date and calculates its julian day within that year
    int toJulian(int month, int day, int year)
    {
    	while(month != 0)
    	{
    		--month;
    		day += daysInMonth(month, year);
    	}
    	
    	return day;
    }
    //Takes a month and year, calculates how many days are in this particular month
    int daysInMonth(int month, int year)
    {
    	switch (month)
    	{
    		case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 :
    			return MAX;
    			break;
    		case 4 : case 6 : case 9 : case 11 :
    			return MID;
    			break;
    		case 2 :
    			return MIN + leapYear(year);
    			break;
    	}
    }
    //Takes a year, returns 1 if this is a leap year, 0 otherwise
    int leapYear(int year)
    {
    	if (year % FOUR == 0 && (year % ONEHUN != 0 || year % FOHUN == 0))
    		return 1;
    	else 
    		return 0;
    }
    //Takes a year, returns the number of days from 1/1/1900 to the end of the previous year
    long yearsToDays(int year)
    {
    	long days = 0;
    
    	while(year != BEGIN)
    	{
    		--year;
    		days += YEAR + leapYear(year);
    	}
    
    	return days;
    }
    //Outputs the calendar to the screen.
    void printCalendar(int startDay, int numDays)
    {
    	int i, col = 0;
    
    	for(i = 0, i < 5; ++i)
    		printf("");
    }
    void printHeader()
    {
    	printf("%4s%4s%4s%4s%4s%4s%4s \n", "Su", "M", "Tu", "W", "Th", "F", "Sa");
    } 
     

Share This Page