Thread: Need help with calendar in C

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Need help with calendar in C

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

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    I believe what you're looking for is Zeller's congruence. Using that, you will be able to calculate the day of the week for any arbitrary date.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is the THIRD forum you've made this request in, and I'm still in the dark about why you didn't use the pseudo code I gave you.

    Please choose one forum, and stick with it. We don't like it when poster's just waste our time with cross posting the same question, on different forums.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Eg. Need help with calendar in C - Dev Shed

    Stay with the one Adak has actually helped you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a calendar c prog
    By nhoda911 in forum C Programming
    Replies: 5
    Last Post: 05-13-2009, 11:30 PM
  2. Writing a program to make a calendar
    By Northstar in forum C Programming
    Replies: 17
    Last Post: 11-07-2007, 11:34 AM
  3. how to convert from solar calendar to lunar calendar??
    By zaracattle in forum C++ Programming
    Replies: 7
    Last Post: 11-30-2006, 07:17 AM
  4. Calendar Program. What am I doing wrong?
    By Sektor in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2004, 11:39 PM
  5. writing a calendar program help please!!!
    By Newbie2006 in forum C Programming
    Replies: 7
    Last Post: 11-20-2002, 07:36 PM