Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > calendar problem

Reply
Thread Tools

calendar problem

 
 
Rob Somers
Guest
Posts: n/a
 
      10-15-2003
Hey people

I came across this calendar problem on another board, and so I tried
solving it myself, but to no avail. If you run the program as it is
now, you should see th problem of the spaces being in the wrong spot
on the first line of output after the 'days of the week' line.

here is the code:

#include <stdio.h>

int main(void)
{

int i, n, day;

printf( "Enter number of days in the month:" );
scanf( "%d",&n );
printf( "Enter starting day of the week (1=Sun, 7=Sat):" );
scanf( "%d",&day );

printf( "\n S M T W T F S \n\n" );

for( i = 1; i <= n ; i++ ){
printf( "%3d", i );

if( ( i + day ) % 7 == 0 ){
printf( "\n" );
}
}

printf( "\n\n" );
return 0;
}

I had wanted to solve it just for my own curiosity, but I think it is
beyond me.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      10-15-2003
Rob Somers wrote:
>
> Hey people
>
> I came across this calendar problem on another board, and so I tried
> solving it myself, but to no avail. If you run the program as it is
> now, you should see th problem of the spaces being in the wrong spot
> on the first line of output after the 'days of the week' line.
>
> here is the code:
>
> #include <stdio.h>
>
> int main(void)
> {
>
> int i, n, day;
>
> printf( "Enter number of days in the month:" );


See Question 12.4 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

for an explanation of why this might not always work
and of how you should fix it.

> scanf( "%d",&n );


scanf() can fail, and you should check whether it
succeeded before proceeding. See Questions 12.19 and
12.20 for some ideas of how to handle the obstreperous
or fat-fingered user who enters "e^H31". You should
probably also check for legitimate numeric input with
bogus values: a month of minus forty-two days, for
example.

> printf( "Enter starting day of the week (1=Sun, 7=Sat):" );
> scanf( "%d",&day );


Same remarks as above.

> printf( "\n S M T W T F S \n\n" );
>
> for( i = 1; i <= n ; i++ ){
> printf( "%3d", i );


Your problem occurs the first time you arrive at
this printf() call. The preceding printf() ended with
a pair of newlines, so the "output position" is at the
start of a fresh output line. Thus, the very first
number you print will occupy the first three spaces on
that line -- which is exactly where you want it for a
month beginning on Sunday, but is too far to the left
for any other weekday. (This is a new twist on the
old phrase about "a month of Sundays.")

There are at least two ways to fix this. The simplest
is to decide how many blank days begin the first week, and
to printf() enough space for them before entering the loop.
The tricky way (mostly useful for proving to yourself how
very clever you are) is to expand the width of the first
output field, changing it from a constant 3 to a variable
amount depending on how much offset is needed; a format
specifier like "%*d" and *two* argument values would do
the trick. Personally, I'd stick with the simple method.

> if( ( i + day ) % 7 == 0 ){
> printf( "\n" );
> }
> }
>
> printf( "\n\n" );
> return 0;
> }
>
> I had wanted to solve it just for my own curiosity, but I think it is
> beyond me.


Not so very far beyond, I'd say; you're fairly close.
Two glaring problems and one subtle error is a better-than-
average outcome in these parts.

For extra credit, modify the corrected program so it
allows the user to specify that the week runs from Sunday
through Saturday, as above, or from Monday through Sunday.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-15-2003

"Rob Somers" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> Hey people
>
> I came across this calendar problem on another board, and so I tried
> solving it myself, but to no avail. If you run the program as it is
> now, you should see th problem of the spaces being in the wrong spot
> on the first line of output after the 'days of the week' line.
>
> here is the code:
>
> #include <stdio.h>
>
> int main(void)
> {
>
> int i, n, day;
>
> printf( "Enter number of days in the month:" );
> scanf( "%d",&n );
> printf( "Enter starting day of the week (1=Sun, 7=Sat):" );
> scanf( "%d",&day );
>
> printf( "\n S M T W T F S \n\n" );
>
> for( i = 1; i <= n ; i++ ){
> printf( "%3d", i );
>
> if( ( i + day ) % 7 == 0 ){
> printf( "\n" );
> }
> }
>
> printf( "\n\n" );
> return 0;
> }
>
> I had wanted to solve it just for my own curiosity, but I think it is
> beyond me.


But it's not. When you can't figure out the behavior of
a program, watch it. This can be done with a debugger, or
in simple cases like this, just output variables' values at
strategic points.

I have made no changes to original other than
adding the 'debug' output:


#include <stdio.h>

/* MKW these three functions will be used for watching the code */
void eat(void) /* MKW */
{ /* MKW */
int c = 0; /* MKW */
while((c = getchar()) != '\n' && c !=EOF) /* MKW */
; /* MKW */
} /* MKW */

void show(const char *what, int arg) /* MKW */
{ /* MKW */
printf(" %s = %2d", what, arg); /* MKW */
} /* MKW */

void pause(void) /* MKW */
{ /* MKW */
printf("%s", " Enter to continue"); /* MKW */
getchar(); /* MKW */
} /* MKW */

int main(void)
{
int i, n, day;
printf( "Enter number of days in the month:" );
scanf( "%d",&n );
printf( "Enter starting day of the week (1=Sun, 7=Sat):" );
scanf( "%d",&day );

eat(); /* MKW 'eat' any chars left over from scanf() */
printf( "\n S M T W T F S \n\n" );

for( i = 1; i <= n ; i++ ){
/* printf( "%3d", i ); */ /* MKW removed so won't interfere */
/* with our 'debug' output */

show("i", i); /* MKW */
putchar(' '); /* MKW */
show("i + day", i + day); /* MKW */
putchar(' '); /* MKW */
show("i + day % 7", i + day % 7); /* MKW */
pause(); /* MKW */

if( ( i + day ) % 7 == 0 ){
printf( "\n" );
}
}
printf( "\n\n" );
return 0;
}

-Mike


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-15-2003

"Mike Wahler" <(E-Mail Removed)> wrote in message
news:3pjjb.2335$(E-Mail Removed) nk.net...

correction:

> void pause(void) /* MKW */
> { /* MKW */
> printf("%s", " Enter to continue"); /* MKW */
> getchar(); /* MKW */


Change to:
eat(); /* MKW */

> }


This will prevent corruption of the output if any
characters are typed before the newline, e.g.

Enter to continue ABC<enter>


-Mike


 
Reply With Quote
 
Rob Somers
Guest
Posts: n/a
 
      10-17-2003
"Mike Wahler" <(E-Mail Removed)> wrote in message news:<3pjjb.2335$(E-Mail Removed) ink.net>...
> "Rob Somers" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) m...
> > Hey people
> >
> > I came across this calendar problem on another board, and so I tried
> > solving it myself, but to no avail. If you run the program as it is
> > now, you should see th problem of the spaces being in the wrong spot
> > on the first line of output after the 'days of the week' line.
> >
> > here is the code:
> >
> > #include <stdio.h>
> >
> > int main(void)
> > {
> >
> > int i, n, day;
> >
> > printf( "Enter number of days in the month:" );
> > scanf( "%d",&n );
> > printf( "Enter starting day of the week (1=Sun, 7=Sat):" );
> > scanf( "%d",&day );
> >
> > printf( "\n S M T W T F S \n\n" );
> >
> > for( i = 1; i <= n ; i++ ){
> > printf( "%3d", i );
> >
> > if( ( i + day ) % 7 == 0 ){
> > printf( "\n" );
> > }
> > }
> >
> > printf( "\n\n" );
> > return 0;
> > }
> >
> > I had wanted to solve it just for my own curiosity, but I think it is
> > beyond me.

>
> But it's not. When you can't figure out the behavior of
> a program, watch it. This can be done with a debugger, or
> in simple cases like this, just output variables' values at
> strategic points.
>
> I have made no changes to original other than
> adding the 'debug' output:
>
>
> #include <stdio.h>
>
> /* MKW these three functions will be used for watching the code */
> void eat(void) /* MKW */
> { /* MKW */
> int c = 0; /* MKW */
> while((c = getchar()) != '\n' && c !=EOF) /* MKW */
> ; /* MKW */
> } /* MKW */
>
> void show(const char *what, int arg) /* MKW */
> { /* MKW */
> printf(" %s = %2d", what, arg); /* MKW */
> } /* MKW */
>
> void pause(void) /* MKW */
> { /* MKW */
> printf("%s", " Enter to continue"); /* MKW */
> getchar(); /* MKW */
> } /* MKW */
>
> int main(void)
> {
> int i, n, day;
> printf( "Enter number of days in the month:" );
> scanf( "%d",&n );
> printf( "Enter starting day of the week (1=Sun, 7=Sat):" );
> scanf( "%d",&day );
>
> eat(); /* MKW 'eat' any chars left over from scanf() */
> printf( "\n S M T W T F S \n\n" );
>
> for( i = 1; i <= n ; i++ ){
> /* printf( "%3d", i ); */ /* MKW removed so won't interfere */
> /* with our 'debug' output */
>
> show("i", i); /* MKW */
> putchar(' '); /* MKW */
> show("i + day", i + day); /* MKW */
> putchar(' '); /* MKW */
> show("i + day % 7", i + day % 7); /* MKW */
> pause(); /* MKW */
>
> if( ( i + day ) % 7 == 0 ){
> printf( "\n" );
> }
> }
> printf( "\n\n" );
> return 0;
> }
>
> -Mike



Hey people

Thanks for your help. As it turned out, someone on the board, that I
initially found this code on, gave a little hint as to what direction
to take that got me started on about 3 different 'solutions' though I
did not get an answer that worked properly until it came to me while I
was working today - heh - my job is completely unrelated to computers
so I had no chance to try my code. Your answers have indeed shown me
that there is a lot more that I need to learn about programming. What
I mean is that sometimes you cannot learn if you don't know what you
don't know.. For example, the whole debugging thing is not
something I am up to speed on at all.

So thanks again for your help.

Rob
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calendar GadGets does not show calendar iccsi Windows 64bit 4 08-14-2011 01:52 PM
How to Synchronise Hotmail Calendar with Standard Outlook Calendar using Outlook Connector ?? Synapse Syndrome Computer Support 0 12-02-2007 04:19 AM
Thunderbird Calendar with Exchange 2003 Calendar and Public FoldersCalendar jincmcse Firefox 1 09-03-2005 02:46 AM
Calendar Control - Programatically set the calendar to a date range Shevek ASP .Net 3 06-23-2004 01:41 PM
Calendar Control - Programatically set the calendar to a date range Shevek ASP .Net Web Controls 0 06-23-2004 12:06 PM



Advertisments