Thread: calender problem

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    230

    calender problem [solved]

    hello,

    I have to make a calender in C.
    The user has to enter the number of days in that monts and which day the month starts.
    So I made this :
    Code:
    /*
     * =====================================================================================
     *
     *       Filename:  test.c
     *
     *    Description:  test
     *
     *        Version:  1.0
     *        Created:  05-06-11 21:07:38
     *       Revision:  none
     *       Compiler:  gcc
     *
     *         Author:  Dr. Fritz Mehner (mn), [email protected]
     *        Company:  FH Südwestfalen, Iserlohn
     *
     * =====================================================================================
     */
    
    
    #include        <stdio.h>
            int
    main ( int argc, char *argv[] )
    {
    
            int begin , dagen, teller, teller2, uitkomst ;
            begin = 0;
            printf ("Hoeveel dagen in een maand : ");
            scanf ("%d", &dagen);
            printf ("Op welke dag moet de maand starten (0 = Zondag, 7 = Zaterdag)");
            scanf ("%d", &begin);
            for (teller=1; teller <=dagen; teller++);
                    {
                            for (teller2=1 ; teller2 <=begin-1; teller2++)
                                    {
                                            printf ("\t");
                                    }
                            printf ("%d", teller);
                            if (teller > 7)
                                    {
                                            begin = 0 ;
                                    }
                            uitkomst = (teller + begin)%7 ;
                            if (uitkomst == 0)
                                    {
                                            printf ("\n");
                                    }
    
                    }
            return 0;
    }                               /* ----------  cnd of function main  ---------- */
    But now the only output I see is the number the user enters +1

    What am I doing wrong ?

    Roelof
    Last edited by roelof; 06-07-2011 at 01:40 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for (teller=1; teller <=dagen; teller++);
    The ; at the end of this line.
    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.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    230
    Thanks.

    Now I can try to solve the other problems.

    Roelof

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    Another thing I don't get.
    I have now this :
    Code:
    
    #include        <stdio.h>
            int
    main ( int argc, char *argv[] )
    {
    
            int begin , dagen, teller ;
            float  uitkomst ;
            begin = 0;
            uitkomst= 0 ;
            printf ("Hoeveel dagen in een maand : ");
            scanf ("%d", &dagen);
            printf ("Op welke dag moet de maand starten (0 = Zondag, 7 = Zaterdag)");
            scanf ("%d", &begin);
            for (teller=1; teller <=begin; teller++)
            {
                    printf ("\t");
            }
            for (teller=1; teller <=dagen; teller++)
                    {
                            if (teller > 7)
                                    {
                                            begin = 0 ;
                                    }
                            uitkomst = (teller + begin)%7 ;
                            printf ("uitkomst : %f", uitkomst);
                            printf ("%d \t", teller);
                            if (uitkomst == 0)
                                    {
                                            printf ("\n");
                                    }
    
                    }
            return 0;
    What I don't get it this :

    I have this uitkomst = (teller + begin) %7 so I can see if it can be divide by 7.
    Let's say teller = 1 and uitkomst =1 then it would be (1+1)%7 is 2/7 = 0.28
    But according to the print rule the outcome = 1.000007

    Can someone explain me why ?

    Roelof

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    printf ("uitkomst : %f", uitkomst);
    printf ("%d \t", teller);
    The first line prints 1.00000
    The second line prints 7

    Since there is no space between them, it looks like one number.
    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.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I have changed it and now getting this output:
    Code:
    	uitkomst : 2.000000 
    1 	uitkomst : 3.000000 
    2 	uitkomst : 4.000000 
    3 	uitkomst : 5.000000 
    4 	uitkomst : 6.000000 
    5 	uitkomst : 0.000000 
    6 	
    uitkomst : 1.000000 
    7 	uitkomst : 1.000000 
    8 	uitkomst : 2.000000 
    9 	uitkomst : 3.000000 
    10 	uitkomst : 4.000000 
    11 	uitkomst : 5.000000 
    12 	uitkomst : 6.000000 
    13 	uitkomst : 0.000000 
    14 	
    uitkomst : 1.000000 
    15 	uitkomst : 2.000000 
    16 	uitkomst : 3.000000 
    17 	uitkomst : 4.000000 
    18 	uitkomst : 5.000000 
    19 	uitkomst : 6.000000 
    20 	uitkomst : 0.000000 
    21 	
    uitkomst : 1.000000 
    22 	uitkomst : 2.000000 
    23 	uitkomst : 3.000000 
    24 	uitkomst : 4.000000 
    25 	uitkomst : 5.000000 
    26 	uitkomst : 6.000000 
    27 	uitkomst : 0.000000 
    28 	
    uitkomst : 1.000000 
    29 	uitkomst : 2.000000 
    30 	uitkomst : 3.000000
    The outcome still is not right. 1/7 is .14 and not 3 as the output of my programm is.

    Roelof
    Last edited by roelof; 06-06-2011 at 10:18 AM.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Code:
    printf ("uitkomst : %f", uitkomst);
    prints uitkomst which is given by:
    Code:
    uitkomst = (teller + begin)%7
    which is the remainder of (teller + begin) / 7
    which is always essentially an integer, hence the 1.00000, 2.00000 print statements. (Why do those not cause you concern?)

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    230
    I only have to know if the outcome will be 0 because then I need to do a newline.
    The other numbers are not important


    Roelof

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    What do you mean by:

    The outcome still is not right. 1/7 is .14
    Apparently you *do* care what it is since this is troubling you.

    Can you debug and watch what happens to your variables as you go through the loops?

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    When I know how to do that in vim I can do that.

    Roelof

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    I hinted to you before about modulus operation. Do you understand that the remainder is really an integer value, and NOT a float?

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    230
    I understand that

    Roelof

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    This looks suspicious:
    Code:
            if (teller > 7) {
                begin = 0 ;
            }
    You realize that begin will be reset to zero for all values of teller greater than 7? Not sure what you are doing with this...

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Quote Originally Posted by roelof View Post
    I understand that

    Roelof
    Then why would you expect 0.14 as an output?
    Last edited by mike65535; 06-06-2011 at 01:27 PM.

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Code:
    printf ("Op welke dag moet de maand starten (0 = Zondag, 7 = Zaterdag)");
    Saturday cannot be day '7' if Sunday is day '0'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calender in C?
    By follano in forum C Programming
    Replies: 3
    Last Post: 04-22-2011, 06:28 PM
  2. calender
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 03-02-2006, 03:34 AM
  3. Calender Problem!
    By CrackerJack in forum C Programming
    Replies: 16
    Last Post: 10-16-2003, 11:45 PM
  4. calender problem in C
    By pari in forum C Programming
    Replies: 6
    Last Post: 10-26-2002, 03:44 PM
  5. Calender
    By confused in forum C Programming
    Replies: 2
    Last Post: 11-28-2001, 05:44 AM