Thread: Writing a program to make a calendar

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    34

    Writing a program to make a calendar

    So I need to write a program that makes a calendar. Basically, if I input a year, the output is supposed to be a calendar for the whole year (12 months).

    The program is also supposed to ask the user if the user wants to continue, and if the user answers yes, then it will print the calendar for another year.

    I don't really know where to start on this, except that I know I need to use another program that I wrote for what day any random date in time is (enter 12-20-1255 and it gives you the day of the week). I posted that program below. Could someone help me get started on converting this program to make a calendar?

    Code:
    #include <stdio.h>
    
    int main()
    {
    	
    	int month, day, year, prevyear, numdays;
    	
    	printf("Please enter a date for month-day-year: ");
    	scanf("%d-%d-%d", &month, &day, &year);
    	
    	prevyear = ((year - 1) * 365 + ((year - 1)/4) - ((year - 1)/100) + ((year - 1)/400)) % 7;
    
    	numdays = 0;
    
    switch(month)
    {
    	case 12:
    		numdays += 30;
    		break;
    	case 11:
    		numdays += 31;
    		break;
    	case 10:
    		numdays += 30;
    		break;
    	case 9:
    		numdays += 31;
    		break;
    	case 8:
    		numdays += 31;
    		break;
    	case 7:
    		numdays += 30;
    		break;
    	case 6:
    		numdays += 31;
    		break;
    	case 5:
    		numdays += 30;
    		break;
    	case 4:
    		numdays += 31;
    		break;
    	case 3:
    		if ((!(year % 4) && (year % 100)) || ! (year % 400))
    			numdays += 29;
    		else numdays += 28;
    		break;
    	case 2:
    		numdays += 31;
    		break;
    
    
    }
    
    switch(day)
    {
    	case 6:
    		printf("The day was Saturday");
    break;
    	case 5:
    		printf("The day was Friday");
    break;
    	case 4:
    		printf("The day was Thursday");
    break;
    	case 3:
    		printf("The day was Wedensday");
    break;
    	case 2:
    		printf("The day was Tuesday");
    break;
    	case 1:
    		printf("The day was Monday");
    break;
    	case 0:
    		printf("The day was Sunday");
    break;
    }
    	return(0);
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    * Work out the start day for each month (ie the week and day, eg Week 1 day 2)
    * If the year is a leap year
    * The days in each month

    I assume you have to print it 'as a calendar'?

    Code:
                Jan
    Sun Mon Tue Wed Thu Fri Sat
             01  02  03  04  05
     06  07
    /* so on */
    
    /* find out the day jan starts on, 2008 it's tuesday */
    Basically you have to count [0, 365], with respect to leap years and the amount of days in each month.

    Perhaps start with:
    Code:
    int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int m = 0;
    
    /* adjust for leap years etc */
    
    for(m = 0; m < 12; m++)
    {
        /* start day of month is... */
        /* print the days from 0 to months[m] */
        
    }
    Last edited by zacs7; 11-07-2007 at 12:17 AM.

  3. #3
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    there are 2 things you need to do (I assume printing formatted is your headache)

    1. calculate if it is a leap year
    2. calculate the day of the first date of a month

    Code:
    int days_in_month[]={31,28,....31};
    int days_in_year (int year)
    {
        /*calculate if its a leap year*/
        if( (year divisible by 4 AND not 100) OR (year divisible by 400))
        {
             /*this will be a leap year.. number of days will be 366. update the days_in_months[] array accordingly*/
        }
        else 
        {
            /*days will be 365. update the days_in_months[] array accordingly*/
        }
        return days;
    }
    
    int day_of_month(int month, int year)
    {
        /* 1-1-1 was a monday */
        for(number of years)
        {
            day =  (day + days_in_year(year) )% 7;
        }
        for(number of months)
        {
            day = (day + days_in_month[month])%7;
            /* days_in_month array is updated according to the current 'year' variable in days_in_year()*/
        }
        return day;
    }
    TAKE CARE OF 'OFF-BY-ONE' ERRORS. I JUST TYPED IT WITHOUT MUCH THOUGHT
    now you got the day of the month its not difficult to print the month

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    73

    Wink source code...... it works fine...

    this code works fine
    small problem is the months r printed in order
    not by tabs
    hope u understand
    so to completely view ouput
    rediect output



    Code:
    #include<stdio.h>
    #define p printf
    main()
    {
    int d,y,no_lp,n,i=1,j,nod[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    
    p("enter yr:");
    scanf("&#37;d",&y);
    
    no_lp=(y-2001)/4;
    
    /*no of leap yrs betwen 2001 and the yr*/
    
    d=(y-2001+no_lp)%7;
    
    /*the day of 1st month*/
    
    n=d;
    
    if(y%4==0)
    nod[2]=29;
    
    for(j=1;j<=12;j++)
    {
    
    p("\nm  t  w  t  f  s  s\n");
    
    while(d--!=0)
    p("   ");
    
    while(i<=nod[j])
    
    {
    
    if(i<10)
    {p("%d  ",i++);}/*formattng for dates with 2 digits*/
    
    else{p("%d ",i++);}
    n++;
    
    if(n==7)
    {n=0;p("\n");}
    
    }
    
    d=n;i=1;/*n will be the 1st day of next month*/
    
    }
    
    getch();
    
    }
    Last edited by Salem; 11-07-2007 at 07:22 AM. Reason: Remove font/style abuse.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    that was my 1st one.....
    my indentin is a bit bad
    plsssss xcuse me......
    n [b] any queries leave a mesage[\b]

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > #define p printf
    Please don't use the pre-processor to rewrite the language. Making the program readable is of far more importance than making it cryptic with lots of single letter identifiers.

    And on the same subject, learn how to indent your code as well.
    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.

  7. #7
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    First Thing
    HOMEWORK POLICY: LET THEM DO THEIR OWN HOME WORK
    Second
    Dont use those bold/CAPS/big sized letters until necessary

    naa
    it dosent even compile too :P
    untill you get ncurses/curses included
    Quote Originally Posted by ElemenT.usha View Post

    Code:
    getch();
    your intention might be to write getchar() or getc(stdin)

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Admittedly, it's about 92 years until it is a problem, and 392 years until there is another issue once you fix that, but:
    Code:
    if(y%4==0)
    nod[2]=29;
    is not complete.

    Using getch() instead of getchar() is probably a bad idea, especialy without including the header file "conio.h" [or whatever it requires].

    Code:
    if(i<10)
    {p("%d  ",i++);}/*formattng for dates with 2 digits*/
    
    else{p("%d ",i++);}
    Why not use "%2d" [or "%-2d"] instead?

    I would say this:
    Code:
    while(i<=nod[j])
    and related i=1 and i++ should be
    Code:
    for(i = 1; i <= nod[j]; i++)
    instead.

    You don't return anything in main().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    it compiles
    i got an output........

    actually getch() is a function which echos the output on the input screen itself.......
    it is included in stdio.h or conio.h

    ok for GIBOSMAT i dunno wat do u mean by curses ????

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ElemenT.usha View Post
    it compiles
    i got an output........

    actually getch() is a function which echos the output on the input screen itself.......
    it is included in stdio.h or conio.h

    ok for GIBOSMAT i dunno wat do u mean by curses ????
    "It compiles, I get an output" may not be quite the same thing as "works correctly", which is generally the acceptance criteria for software.
    Code:
    int main() 
    {
       int i;
       printf("November\n";
       for(i = 1; i < 31; i++)
         printf("%2d ", i);
      return 0;
    }
    also compiles and produces an output, which works for this month. But you could hardly call it a "calender program".

    getch() is a function that doesn't exist in standard C - it's in Turbo C and some versions of MS C, but not available in for example Linux.

    curses/ncurses is a set of library functions that allow more flexible use of text input/output and screen management. It is often used in editors and such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    thanxxxx matsp ya
    while with for is a good idea!!!!!

    and i dunno why u guys have a prob with getch()

    it works fine fr me ..... i use a TC2.0 ......
    and comin to returnin values in main() ....
    im sorry replace main() with void main()

    and with the issue of 92 n 392 .... i xpected him to complete the code......
    that was pretty easy......


    > #define p printf
    Please don't use the pre-processor to rewrite the language. Making the program readable is of far more importance than making it cryptic with lots of single letter identifiers.
    and SALEM ill remember to indent and remove preprocessor ... thanxxxx

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    It compiles, I get an output" may not be quite the same thing as "works correctly", which is generally the acceptance criteria for software.

    ya got the point..... ok i never knew getch() wasn,t present......

    i use TC...... n it does work..... tell me its flas... if u v found any????

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    flaws of the prg i mean......... and remove getch()......
    matsp tell me an ideal compiler......
    i wana do c as c not as turbo c....
    reply

    and for the code check it!!!!!!!!!!!!!!!!!

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    i use a TC2.0
    That compiler was introduced in 1989 according to http://en.wikipedia.org/wiki/Turbo_C

    Perhaps it's time to move up a bit?

    Edit: And I'm sorry if you have some sort of handicap that makes it hard to type, but could you please try to type full words, "why" instead of "y", "want to" instead of "wana".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    Quote Originally Posted by matsp View Post
    That compiler was introduced in 1989 according to http://en.wikipedia.org/wiki/Turbo_C

    Perhaps it's time to move up a bit?

    Edit: And I'm sorry if you have some sort of handicap that makes it hard to type, but could you please try to type full words, "why" instead of "y", "want to" instead of "wana".

    --
    Mats

    ok .... mats....... ill type full words..... by the way i am not a handicap...
    thank you for your concern......

    and could you suggest me something better for compiling please.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Writing code for a program in C
    By Sure in forum C Programming
    Replies: 7
    Last Post: 06-11-2005, 01:33 PM
  3. Writing a checklist program for MTGO?
    By CrYpTiC in forum Game Programming
    Replies: 2
    Last Post: 04-08-2005, 09:46 PM
  4. make a program stay....
    By Nirav in forum C Programming
    Replies: 6
    Last Post: 10-12-2003, 07:01 PM