Thread: Calendar Program

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    22

    Calendar Program

    task is to create a program that asks for year and prints calendar for entire year. this is by the far the largest program i have written so far. i am trying to take it in small pieces. i think the first step is to create a functioning calendar month and then a year and then inserting formulas for leap year and finding first day of the year. and then ridding of bugs. so far, i have written program for a calendar month but cannot get it to compile. it says that 'for' initial loop is only allowed in C99 mode and im not sure what that is. any idea?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    // Prototype Declarations
    void printMonth (int startDay, int days);
    
    
    int main(void)
    {
    // Statements
        printMonth (2,29);
        return 0;
    }
    
    
    void printMonth (int startDay, int days)
    {
    
    
    //Local Declarations
        int weekDay;
    
    
    //Statements
        //print day header
        printf("Sun Mon Tue Wed Thu Fri Sat\n");
        printf("--- --- --- --- --- --- ---\n");
    
    
        //position first day
        for (weekDay = 0; weekDay < startDay; weekDay++)
            printf("    ");
    
    
    
    
        for (int dayCount = 1; dayCount <= days; dayCount++)
            {
            if (weekDay > 6)
                {
                printf("\n");
                weekDay = 1;
                } //if
            else
                weekDay++;
            printf("%3d ", dayCount);
            } //for
        printf("\n--- --- --- --- --- --- ---\n");
        return;
    } // printMonth
    error is for this line:

    Code:
    for (int dayCount = 1; dayCount <= days; dayCount++)

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    That means you either have to declare the int outside the loop:
    Code:
    int dayCount;
    for (dayCount = 1; dayCount <= days; dayCount++)
    or compile as c99:
    Code:
    gcc -std=c99 main.c
    Edit: this is because declaring variables inside the initialisation section of a for loop was illegal before C99 standard.

  3. #3
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    I did something like this years ago. Here's a tip. Calendars cycle every 28 years. This is because every year they are off by one day. If there were no leap years then it would cycle every 7 years. But with leap years its 28 years. So march 4th 2012 is sunday, and so march 4th 2040 will also be a sunday. Good luck.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by javaeyes View Post
    Calendars cycle every 28 years. This is because every year they are off by one day. If there were no leap years then it would cycle every 7 years. But with leap years its 28 years.
    Every 28 years must be something of a simplification since leap years are not EVERY 4 years. Leap years are every 4 years, except every 100 years, except every 400 years. I.e.:
    Code:
    int leapYear = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    how do i make this repeat? my way is not working...
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    // Global Declarations
    int MonthDays[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    char *months[]=
    {
    	" ",
    	"\n\n\nJanuary",
    	"\n\n\nFebruary",
    	"\n\n\nMarch",
    	"\n\n\nApril",
    	"\n\n\nMay",
    	"\n\n\nJune",
    	"\n\n\nJuly",
    	"\n\n\nAugust",
    	"\n\n\nSeptember",
    	"\n\n\nOctober",
    	"\n\n\nNovember",
    	"\n\n\nDecember"
    };
    
    
    int yearUsed(void)
    {
    	int year;
    
    
    	printf("Please enter a year : ");
    	scanf("%d", &year);
    	return year;
    }
    
    
    int firstDay(int year)
    {
    	int findDay;
    	int a;
    	int b;
    	int c;
    
    
    	a = (year - 1.)/ 4.0;
    	b = (year - 1.)/ 100.;
    	c = (year - 1.)/ 400.;
    	findDay = (year + a - b + c) %7;
    	return findDay;
    }
    
    
    int Leapyear(int year)
    {
    	if(year% 4 == 0 && year%100 != 0 || year%400 == 0)
    	{
        MonthDays[2] = 29;
        return 1;
    	}
    	else
    	{
        MonthDays[2] = 28;
        return 0;
    	}
    }
    
    
    void calendar(int year, int findDay)
    {
    	int month, day;
    	for ( month = 1; month <= 12; month++ )
    	{
        printf("%s %d", months[month], year);
        printf("\n\nSun  Mon  Tue  Wed  Thu  Fri  Sat\n" );
    
    
        // First Day
        for ( day = 1; day <= 1 + findDay * 5; day++ )
        {
            printf(" ");
        }
    
    
        // Print Month
        for ( day = 1; day <= MonthDays[month]; day++ )
        {
            printf("%2d", day );
    
    
        // If day before Sat or start Sun
        if ( ( day + findDay ) % 7 > 0 )
            printf("   " );
        else
            printf("\n " );
        }
        // Align month
        findDay = ( findDay + MonthDays[month] ) % 7;
    	}
    }
    
    
    int main(void)
    {
    //Local Declarations
        char ans;
    	int year, findDay, leapyear;
    
    
        do{
    
    
    	year = yearUsed();
    	findDay = firstDay(year);
    	Leapyear(year);
    	calendar(year, findDay);
    	printf("\n");
    
    
        printf("y to continue\n");
        scanf("%s", ans);
    }   while( ans[0] == 'y' || ans[0] == 'Y' );
    
    
    
    
    } // Main

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I think it works OK. But I had to change ans to ans[10] so that it is an array since you check for ans[0].

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    HMM.... doesnt work for me. it says subscribted value is neither array nor pointer for that line. i am running code blocks with gnuc compiler...

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    ah i figured out my problem. works fine now. thanks for the tip!

  9. #9
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    Hmm didn't realize the 400 year leap year exception.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Program for Calendar
    By Karyumi in forum C Programming
    Replies: 1
    Last Post: 03-03-2012, 10:47 PM
  2. 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
  3. Yet another Calendar Program.
    By duffmckagan in forum C Programming
    Replies: 9
    Last Post: 07-14-2006, 10:29 PM
  4. Help with calendar program please
    By blindleaf in forum C Programming
    Replies: 1
    Last Post: 10-31-2002, 11:11 AM
  5. calendar program
    By [email protected] in forum C Programming
    Replies: 1
    Last Post: 02-11-2002, 10:44 AM