GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Nov-2006, 22:02
icondor icondor is offline
New Member
 
Join Date: Oct 2006
Posts: 13
icondor is on a distinguished road

PrintCalendar Program [HELP PLEASE!!!]


This program creat a calendar for a year
The program reads the year from the keyboard.
It then calculates which day of the week (SUN, MON....SAT) is the first day of the year and prints the calendar for that year. After printing the year, it should ask id the user wants to continue. If the answer is yes, it will print the calendar for another year until the user is done.

CPP / C++ / C Code:
#include <stdio.h>

int  getYear      ( void );
int  calcJanFirst ( int year );
void printCalendar( int year, int JanFirst );

int  chooseDays   ( int year, int month );
int  isLeap       ( int year );

void printHeader  ( int year, int month );
int  printMonth   ( int firstDay, int days );

int main( void )
{
    int year;
    int JanFirst;
    int month;
    
    year = getYear();
    JanFirst = calcJanFirst(year);
    printCalendar(year, JanFirst);
        
    return 0;

}

/* ======================================================= */
/* This function prompts the user to enter a year: if it is 
   within the range 1000 to 3000 it is considered a valid year;
   otherwise, it prompts the user to enter another value, until 
   it is valid.
    PRE : nothing
    POST: year is returned
*/
int  getYear      ( void )
{
     int year;
     do
       {
        printf("Please enter the year within range 1000 3000: ");
        scanf("%d", &year);
             if (year < 1000 || year > 3000)
                printf("It is outside the range 1000 3000.\n");
                printf("Please input it again.\n\n");   
      } while (year < 1000 || year > 3000);
     return year;
}

/* ======================================================= */
/* This function determines the day of the week for 
   January first: 0 - Sunday, 1 - Monday, ... 6 - Saturday.
			- see formula at page 388
    PRE : year 
    POST: day of the week for Jan 1st returned
	  
*/
int  calcJanFirst ( int year )
{
     int FirstDay;
     
     FirstDay = (((year-1)*365)+((year-1)/4)-((year-1)/100)+((year-1)/400)+1)%7;
     return FirstDay; 
}

/* ======================================================= */
/*  This function has prints the calendar for a given year,
    using three sub-functions.
    PRE : year 
          JanFirst 
    POST: calendar printed
*/
void printCalendar( int year, int JanFirst )
{
     int days;
     int month;
     int NextFirst;
     
     // Print January first
     month = 1;
     days = chooseDays(month, year);
     printHeader(year, month);
     printMonth(JanFirst, days);
     NextFirst = printMonth(JanFirst, days);
     
     // Print the rest
     for (month = 2; month <= 12; month++)
         { 
           days = chooseDays(month, year);
           printHeader(year, month);
           printMonth(NextFirst, days);
           NextFirst = printMonth(NextFirst, days);
           }
     return;
}

/* ======================================================= */
/*  This function determines the number of days in a month.
    PRE : month - a number between 1 and 12 	      
 		  year 
    POST: the number of days in the month is returned
*/
int chooseDays( int month, int year )
{
    int days;

    switch( month )
    {
    case 1: 
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12: 
        days = 31;
        break;
    case 4:
    case 6:
    case 9:
    case 11: 
        days = 30;
        break;
    case 2: 
		days = 28;
        if( isLeap( year ) )
            days = 29;      // leap year 
        break;
    }

    return days;
}

/* ======================================================= */
/* A year is a leap year if it is divisible by 4 but not by 100,
   except that years divisible by 400. This function determines 
   if a year is a leap year or not.
    PRE : year 
    POST: True/False returned      
*/
int isLeap( int year )
{
    return year % 4 == 0 && year %100 != 0 || year %400 == 0;
}

/* ======================================================= */
/* This function prints the header of a month 
   (name + year + name of the days in a week) 
      - see example at page 388
    PRE : month - a number between 1 and 12 	      
 		  year 
    POST: header printed
*/
void printHeader  ( int year, int month )
{
     switch(month)
       {
        case 1: printf("January                 %4d", year);
        case 2: printf("February                %4d", year);
        case 3: printf("March                   %4d", year);
        case 4: printf("April                   %4d", year);
        case 5: printf("May                     %4d", year);
        case 6: printf("June                    %4d", year);
        case 7: printf("July                    %4d", year);
        case 8: printf("August                  %4d", year);
        case 9: printf("September               %4d", year);
        case 10: printf("October                %4d", year);
        case 11: printf("November               %4d", year);
        case 12: printf("December               %4d", year);
        }
     return;
}

/* ======================================================= */
/* This function prints the days in a month as a table with 
   7 columns(one for each day in a week, beginning with Sunday).
    - see Program 6-12, page 330
    PRE : firstDay - a number between 0 and 6 (the day of the 
					 week for the first day in the current month).
		  days  - the number of days in the current month
    POST: the day of the week for the first day of the next month
          is returned.
*/
int  printMonth   ( int firstDay, int days )
{
     int weekDay;
     
     printf("Sun Mon Tue Wed Thu Fri Sat\n");
     
     for (weekDay = 0; weekDay < firstDay; 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 weekDay;
}

The program print out many garbage which is not expected.
I have struggled it for a long time already, but I cannot firgure out the problem.

I am using Dev-C++ compiler
2nd question:
Why the compiler don't allow me to use this statment in this program ::
CPP / C++ / C Code:
 
system("pause");

I cannot see the result if I don't use this statment.

Please help me......
  #2  
Old 17-Nov-2006, 23:41
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: PrintCalendar Program [HELP PLEASE!!!]


First, you need a break after every case in a switch statement, or execution will "fall through" to the next case:
CPP / C++ / C Code:
switch (variable)
{
    case 0:
         printf("it was 0"); //whoops -- there is no break, so if variable == 0 then the program will print "it was 0it was 1"
    case 1:
         printf("it was 1");
         break; //stop here
     case 2:
         printf("it was 2");
         break; //not strictly necessary since it's the end of the switch anyways, but good practice
}

What else do you mean by "garbage"? What was the expected output, and what was the actual output?


The system() statement is in stdlib.h. However, use getchar() instead as it is much more portable, and faster.
  #3  
Old 18-Nov-2006, 00:58
icondor icondor is offline
New Member
 
Join Date: Oct 2006
Posts: 13
icondor is on a distinguished road

Re: PrintCalendar Program [HELP PLEASE!!!]


How can I use getchar()??
  #4  
Old 18-Nov-2006, 03:06
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,281
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: PrintCalendar Program [HELP PLEASE!!!]


What book are you using? Doesn't it explain getchar()?
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #5  
Old 18-Nov-2006, 03:38
mathematician mathematician is offline
Member
 
Join Date: Nov 2006
Location: Shrewsbury Uk
Posts: 131
mathematician will become famous soon enough

Re: PrintCalendar Program [HELP PLEASE!!!]


Quote:
Originally Posted by icondor
How can I use getchar()??


He may have meant putchar( '\n' ) instead of printf( "\n" ); The former is quicker, but not, IMHO more portable.

=============================================

There are a few other things I can see, although the list is not necessarily exhaustive.

1.) First your week appears to have only six days in it, if you start counting from 1
CPP / C++ / C Code:
if (weekDay > 6)
             {
              printf("\n");
              weekDay = 1;
             } //if

2.) Since the two printf's are part of the same compound statement, you need braces round them.
CPP / C++ / C Code:
if (year < 1000 || year > 3000)
               {                                                                  //HERE
                printf("It is outside the range 1000 3000.\n");
                printf("Please input it again.\n\n");   
               }                                                                  // AND HERE

3.) I may be missing something, but why do you call printMonth twice, when you are going round the loop eleven times.
CPP / C++ / C Code:
 for (month = 2; month <= 12; month++)
         { 
           days = chooseDays(month, year);
           printHeader(year, month);
           printMonth(NextFirst, days);
           NextFirst = printMonth(NextFirst, days);
         }

If you want to get two months on the same row I fear you may have a fair bit of rewriting to do. For example, Instead of

CPP / C++ / C Code:
printf("Sun Mon Tue Wed Thu Fri Sat\n");

you would need

printf("Sun Mon Tue Wed Thu Fri Sat          Sun Mon Tue Wed Thu Fri Sat\n");

Similarly you would need to get the first (and subsequent) weeks in Jan/Feb or Mar/April onto the same line. You would also need to take account of the fact that Feb might only have four lines in it, but January might have six lines in it.
Last edited by LuciWiz : 18-Nov-2006 at 13:58. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #6  
Old 18-Nov-2006, 04:00
mathematician mathematician is offline
Member
 
Join Date: Nov 2006
Location: Shrewsbury Uk
Posts: 131
mathematician will become famous soon enough

Re: PrintCalendar Program [HELP PLEASE!!!]


Actually, if you only want to display the calendar on the screen, rather than print it out, and you want to get two months on the same, you could simplify things a little bit if you could find a function to move the cursor to the right of January before printing the lines for February. I'm sure there would be such a function buried away amongst your library functions somewhere (although it is not part of the standard C library).
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
BOOKEEPING program, HELP!! yabud C Programming Language 10 17-Nov-2006 04:48
Pipeline freeze simulation darklightred C++ Forum 6 27-Jul-2006 20:37
How to read particular memory location ? realnapster C Programming Language 10 10-May-2006 10:11
Type casts ? kai85 C++ Forum 12 23-Jun-2005 13:04
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:48.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.