Thread: Is something wrong with this program?

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    8

    Is something wrong with this program?

    Code:
    #include<stdio.h>#include<conio.h>
    #include<stdlib.h>
    #include<dos.h>
    int getkey();
    void main()
    {
        long normaldays,firstday,totaldays;
        int leapdays,year,month,sum=0,col,row,i,ch,d;
        int days[]={31,0,31,30,31,30,31,31,30,31,30,31};
        char *months[]={"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
    
    
        clrscr();
    
    
        printf("Enter Month & Year:");
        scanf("%d%d",&month,&year);
    
    
        while(1)
        {
            normaldays=(year-1)*365l;
            leapdays=(year-1)/4-(year-1)/100+(year-1)/400;
            totaldays=normaldays+leapdays;
    
    
            if((year%400==0)||(year%100==0 && year%4==0))
                days[1]=29;
            else
                days[1]=28;
    
    
            for(i=0;i<(month-1);i++)
                sum=sum+days[i];
    
    
            totaldays+=sum;
            firstday=totaldays%7;
    
    
            clrscr();
    
    
            gotoxy(32,4);    printf("%s %d",months[month-1],year);
            gotoxy(18,5);    printf("---------------------------------------");
            gotoxy(18,6);    printf("MON   TUE   WED   THU   FRI   SAT   SUN");
            gotoxy(18,7);    printf("---------------------------------------");
    
    
            col=18+firstday*6;
            row=8;
            for(d=1;d<=days[month-1];d++)
            {
                gotoxy(col,row);
                printf("%d",d);    col+=6;
                if(col>54)
                {
                    col=18;    row++;
                }
            }
            printf("\n\t\t----------------------------------------");
    
    
            gotoxy(16,16);    printf("(*) Press Right arrow key to see next month.");
            gotoxy(16,17);    printf("(*) Press Left arrow key to see previous month.");
            gotoxy(16,18);    printf("(*) Press Up arrow key to see same month of next year.");
            gotoxy(16,19);    printf("(*) Press Down arrow key to see same month of previous year.");
            gotoxy(16,20);    printf("(*) Press ESC to escape.");
    
    
            ch=getkey();
            switch(ch)
            {
                case 77:
                    month++;
                    if(month>12)
                    {
                        month=1;    year++;
                    }
                    break;
    
    
                case 75:
                    month--;
                    if(month<1)
                    {
                        month=12;    year--;
                    }
                    break;
    
    
                case 72:
                    year++;
                    break;
    
    
                case 80:
                    year--;
                    break;
    
    
                case 1:
                    exit(1);
    
    
                default:
                    printf("\a");
            }
        }
    }
    int getkey()
    {
        union REGS i,o;
        while(!kbhit())
            ;
        i.h.ah = 0;
        int86(22,&i,&o);
        return (o.h.ah);
    }

    When i execute this program it output correctly.But the problem occur when i use arrow keys for next month,previous month,etc.whatever.,then it doesn't show correct output.And when i return to the same month again it shows that month wrongly.Why this is happening.Is something wrong with the switch or anything else.Please help me.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If I understand you correctly the problem is a formatting one, in which case I think you need a saner approach. If you imagine a calendar page, then there are a couple of things that we notice.

    One is that the first week of the month can be short, because the first may fall on a day other than Sunday. The last week may be short because the month ends before Saturday. All of the other weeks are full weeks.

    The second thing is that the calendar is a grid. On the screen, you simply want the number of the days to be spaced out evenly on each line. You can use printf() to make the spacing neat.

    So all you need to know is when the first and the last falls on a given month. I think it would be easier to use some functions from the C Time library instead of trying to figure it out yourself. Look here for some hints.

    You now have enough information to try again, but just to make it clear that the approach works, I will give you a gift. This is a little program that prints out the given month:
    Code:
    #include <stdio.h>
    
    #define CELLSPACE 8
    #define WEEKDAYS  7
    
    void getCalendarHeadings(void);
    
    /*Returns the calender day that starts the next week.*/
    int printWeek(int init_weekday, /*Day of the week this week started*/
        int firstday, /*First calender day of the week, e.g. 8*/ 
        int lastday) 
    {
        int loop;
        if (firstday == 1) /*might have some odd spacing...*/
            printf("%*d", (1 + init_weekday) * CELLSPACE, firstday);
        else
            printf("%*d", CELLSPACE, firstday);
            
        for (loop = firstday + 1; loop < lastday; loop++)
            printf("%*d", CELLSPACE, loop);
            
        printf("\n");
        return lastday;
    }
    
    int main()
    {
        int day = 1;
        const int EndOfSeptember = 30;
        getCalendarHeadings();
        
        /*The first week may not be a full week. Use the first arg as an offset*/
        day = printWeek(1 /*mon*/, day, day + WEEKDAYS - 1);
        
        while (day <= EndOfSeptember)
        {
            /*In the case where the last week is not a full week, use the last day of the month.*/
            if (day + WEEKDAYS > EndOfSeptember)
                day = printWeek(0, day, EndOfSeptember + 1);
            else
                day = printWeek(0, day, day + WEEKDAYS);
        }
        
        return 0;
    }
    
    void getCalendarHeadings()
    {
        char formatbuffer[1000];
        char *labels[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
        int loop;
        sprintf(formatbuffer, "%%%ds", CELLSPACE);
        for (loop = 0; loop < WEEKDAYS; loop++)
            printf(formatbuffer, labels[loop]);
            
        printf("\n");
    }
    
    /* my output:
         Sun     Mon     Tue     Wed     Thu     Fri     Sat
                   1       2       3       4       5       6
           7       8       9      10      11      12      13
          14      15      16      17      18      19      20
          21      22      23      24      25      26      27
          28      29      30
    */
    Feel free to extend it for your purposes.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for(i=0;i<(month-1);i++)
    > sum=sum+days[i];
    Does sum start at zero the 2nd time around your while loop?

    Listen to whiteflags. If you had more functions to perform specific actions, you wouldn't have a morass of variables and code in a single large function trying to do everything.
    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. Something is wrong with my box program :C
    By Rukris in forum C Programming
    Replies: 10
    Last Post: 10-25-2011, 09:26 PM
  2. What's wrong with this program?
    By lijr07 in forum C Programming
    Replies: 4
    Last Post: 06-24-2011, 07:08 AM
  3. what's wrong with this program?
    By fares jajeh in forum C Programming
    Replies: 5
    Last Post: 05-05-2011, 10:39 AM
  4. [Help] What's wrong in my program?
    By kingofdcp in forum C Programming
    Replies: 2
    Last Post: 05-19-2009, 09:40 PM
  5. What's wrong with my program?
    By Infuriate in forum C Programming
    Replies: 7
    Last Post: 12-03-2005, 04:43 PM

Tags for this Thread