Thread: Calender Problem!

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    49

    Calender Problem!

    OK I got this far, but the problems I am having are getting the date to start on the correct day and allowing the days to correcty follow that.

    Any ideas?

    Code:
    #include <stdio.h>
    
    main()
    {
    
    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("  S  M  T  W  T  F  S \n");
    
    for (i=1; i <= n ; i++)
    {
    	printf("%3d", i);
    
    	if (i % 7 == 0)
    	{
    		printf("\n");
    	}
    }
    
    }
    I did try the search button, but still have not figured it out!

    Thanks in advance!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( (i+day) % 7 == 0 )
    Enjoy.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by quzah
    Code:
    if( (i+day) % 7 == 0 )
    Enjoy.

    Quzah.
    Well that creates the spaces for me, however it puts them at the end of the line. So, it makes a month that starts on Tues. end on Fri.

    Is there another statement I would before that if?

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by CrackerJack
    Well that creates the spaces for me, however it puts them at the end of the line. So, it makes a month that starts on Tues. end on Fri.

    Is there another statement I would before that if?
    Yes, before the loop that displays the month you need one more loop to print out the number of spaces.

    If your month starts on Tues, you need a spacing loop that will go from 0 (or 1, you figure that out) to day, then start printing the month
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Ahhh - I was hoping someone would give a hint. I got a solution using some if statements before the month is displayed.

    I should try a loop solution too.

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    OK...Thanks so far guys! But I still gotta problem. I got that loop function to work, but the only problem is it seems to add too many spaces.

    I 'm gonna try some other stuff, but any more hints would be apprecitive.

    Code:
     #include <stdio.h>
    
    main()
    {
    
    int i;
    int n;
    int day;
    int blank;
    
    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("  S  M  T  W  T  F  S \n");
    
    for ( blank = 0; blank <= day; blank++ )
    	{
    	printf("   ");
    	}
    
    
    for (i=1; i <= n ; i++)
    	{
    		if ( (i + day) % 7 == 0)
    		{
    		printf("\n");
    		}
    		
    	printf("%3d", i);
    
    	}
    
    }

  7. #7
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    for ( blank = 0; blank <= day; blank++ )

    You're on the right track, but your logic around this loops appears to be slightly off. Count in your head how many times this loop will execute (ie how make blanks will be printed) if you were to pick say 4 (Wed) for the day, is that how many blanks you want? If not adjust the logic (where the loop starts, how high it counts before it stops, should your counter be incremented before or after printing the blank), you may need to adjust more than one of these in order for it to work correctly and for it to make logical sense.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by Azuth
    for ( blank = 0; blank <= day; blank++ )

    You're on the right track, but your logic around this loops appears to be slightly off. Count in your head how many times this loop will execute (ie how make blanks will be printed) if you were to pick say 4 (Wed) for the day, is that how many blanks you want? If not adjust the logic (where the loop starts, how high it counts before it stops, should your counter be incremented before or after printing the blank), you may need to adjust more than one of these in order for it to work correctly and for it to make logical sense.
    OK OK..I see your point about the counter location! The logic is killing me though. Using this code will get me the correct amount of spaces, but then just like before i get blank spaces at the end too.

    I appreciate your help man...I have been at this for 3 days! I am missing something cuz it should not be this hard.

    Code:
    for ( blank = 1; day > blank;)
    	{
    	printf("   ");
    	blank++;
    	}

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use pesudo-code and work the problem out in your mind. Then make it work.
    Code:
    display callender header
    display spaces
        number of spaces is start day minus one
    for x is the first day, x less than days in month, x equals x plus one
        display this day
        if this day is saturday, go to the next line
    Enjoy.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Code:
    #include <stdio.h>
    
    int main()
    {
    
    int i;
    int n;
    int day;
    int blank;
    
    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("  S  M  T  W  T  F  S \n");
    
    for ( blank = 1; blank < day; blank++)
    
    	{
    	printf("   ");
    	}
    
    for (i=0; i < n ; i++)
    
    	{
    	printf("%3d", (i+1));
    
    	if ( (i + day) % 7 == 0)
    	        {
    		printf("\n");
    		}
    
    	}
    
    return 0;
    }
    Normally I'd stick with quzah and give hints, not code, but I'm going to trust that you'll work out why I've made the changes to your program and what they do.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    And one other suggestion....

    Instead of working this stuff out in your head, work it out on paper. That way it's easier to backtrack.

    I remember when I was learning (and even today), papertesting is essential during development.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  12. #12
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by Azuth
    Code:
    for (i=0; i < n ; i++)
    
    	{
    	printf("%3d", (i+1));
    OK...This part still gets me. OK...It will start off at zero so that I am not forced to a have a month w/ just 1 day if the user would enter zero for 'n'. Then it loops while 'n' is greater then 'i' incrementing it by one.

    I am still confused on the logic of that printf. My guess would be since I start at 0...it allows me to us the first 'for' startement. Then it adds the one to 'i' directly after that in the second 'for' statement to allow the numbers to contine after the blanks are printed.

  13. #13
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Here is what I had figured out today while I was at work - I hate that when I can't get to a computer to test it out - And I didn't really have any time to work it out on paper anyway.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
    int i, j, 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" );
    
     if( day <= 7 ){
          day = ( day - 1 );          /* properly align the spaces */
          for( j = 1; j <= day; j++ ) /* print the spaces */
    	   printf( "   " );
     }
    
     for( i = 1; i <= n ; i++ ){
         printf( "%3d", i );
    
        	if( ( i + day ) % 7 == 0 ){
    	       printf( "\n" );
    	}
     }
     
     printf( "\n\n" );
     return 0;
    }
    BTW, when I was working on this I tried using:

    Code:
     day = day--
    It worked, but gcc sqawked at me -
    [kermit@localhost misc]$ gcc -Wall -ggdb -o days_2 days_2.c
    days_2.c: In function `main':
    days_2.c:16: warning: operation on `day' may be undefined
    Can anyone explain this to me? Did I do something wrong?
    Last edited by kermit; 10-16-2003 at 06:25 PM.

  14. #14
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    Originally posted by kermit
    Here is what I had figured out today while I was at work - I hate that when I can't get to a computer to test it out - And I didn't really have any time to work it out on paper anyway.

    I see that Azimuth's code works, but I have not had time to really check it out.
    Yeah, both work. I appreciate you guys helping me understand this stuff better. After looking at the way kermit did his code I figured it out why you need that extra 1. Came up w/ this after error checking:

    Code:
    #include <stdio.h>
    
    main()
    {
    
    int i=0;           /* Intialize i, n, day, & blank */
    int n=0;
    int day=0;
    int blank=0;
                       /* Ask user for amount of days */ 
    printf("Enter number of days in the month:");
    
                       /* Checks if valid amount of days */
    while ( scanf("%d", &n) != n > 28 || n > 31)
    {
    	printf("Not a correct number of days! Please try again!");
    }
    
    				   /* Ask user for start of week */
    printf("Enter starting day of the week (1=Sun, 7=Sat):");
    
                       /* Checks if valid day        */
    while ( scanf("%d", &day) != day > 1 || day > 7)
    {
    	printf("Not a correct choice of start days! Please try again!");
    }
    
    				   /* Print out headers of days */
    printf("  S  M  T  W  T  F  S \n\n");
                     
                       /* Blank equals one. If blank is > then day,   */
                       /* print spaces. Increment blank, print spaces */
    				   /* until day is < blank                        */
    for ( blank = 1; day > blank; blank++ )
    	{
    		printf("   ");
    	}
    
                       /* Start off at zero, if i is < then amount of */
    	               /* days increment by one. Until > then the days*/
    for (i=0; i < n ; i++)
    	{              /* Print out i's looped, then add one          */
    		printf("%3d", (i+1));
    		
    		           /* When mod. equals zero, end line & start new */ 
    		if ( (i + day) % 7 == 0)
    		{
    			printf("\n");
    		}
    
    	}
    
    }
    Look ok to you guys?

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >BTW, when I was working on this I tried using:
    >day = day--
    >It worked, but gcc sqawked at me -
    >Can anyone explain this to me? Did I do something wrong?

    It's pretty much the same as this.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM