Thread: i can't find what's wrong with this code

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    40

    i can't find what's wrong with this code

    Code:
    #include <stdio.h>
    
    main()
    {
      int day_code; /* day_code = 0 means the month starts on Sun
                          day_code = 1 means the month starts on Mon
                          day_code = 2 means the month starts on
                          Tues, etc.  */
      int days_in_month;     /* number of days in month currently
                              being printed */
      int leap_year; /* 1 means leap year; 0 means no leap year */
      int day;       /* counter for day of month */
      int month;     /* month = 1 is Jan, month = 2 is Feb, etc. */
    
      int year = 0, f = 0, d = 0, c = 0;
    
      do {
        printf( "Enter year: " );
        scanf( "%d", &year );
        printf ( "%d", &year );
      }while( year < 1776 );
    
      while ((year % 400 == 0) && (year % 4 == 0) && (year % 100 > 0))
        {
          leap_year = 1;
        }
    
      c = year / 100;
      printf ( "%d", &c );
      d = year % 100;
      printf ( "%d", &d );
      f = 1 + (( 13 * 11 - 1 ) / 5) + d + ( d / 4 ) + ( c / 4 ) - 2*c;
    
      printf ( "%d", &f );
    
      return 0;
    
    }
    here's whats comes out:

    Enter year: 1990
    -1073743504-1073743516-1073743512-1073743508



    now can anyone please help me and tell me why its giving me all these wacked out numbers?

  2. #2
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Code:
    printf ( "%d", &year );
    ...
    You're using the adress of operator & in the printf() function. Thereby you are printing the adress of the variable e.g. year.
    Remove the "&" in all printf() function calls, recompile and look at the output.

  3. #3
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    first of all
    Code:
     printf("%d", &f ); //wrong
    What you are trying to do is print the address ???

    Code:
    printf( "%d", f );  //this is right
    Check to code for you leap year ..if it is a leap year then it should print it out where does it do that ....
    varaible day_code, month etc... are they just there for fancy ??
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     while ((year % 400 == 0) && (year % 4 == 0) && (year % 100 > 0))
        {
          leap_year = 1;
        }
    Why are you using a loop here? It's a simple check, so why not just use an 'if' statement?

    Code:
    printf ( "%d", &c );
      d = year % 100;
      printf ( "%d", &d );
      f = 1 + (( 13 * 11 - 1 ) / 5) + d + ( d / 4 ) + ( c / 4 ) - 2*c;
    
      printf ( "%d", &f );
    You're using printf wrong. You're printing addresses, not values. Don't use the & when using printf.

    [edit]Curses! Foiled again.[/edit]
    [edit]On a side note, why are you not using hard coded values?

    f = 1 + (( 13 * 11 - 1 ) / 5) + d + ( d / 4 ) + ( c / 4 ) - 2*c;

    Which is...

    f = 1 + ((142)/5) + d + (d / 4) + (c/4) - 2 * c;

    Which is...

    f = 1 + (28) + d + (d / 4) + (c/4) - 2 * c;

    Which is....

    f = 29 + d + (d / 4) + (c/4) - 2 * c;
    [/edit]

    Quzah.
    Last edited by quzah; 11-12-2002 at 03:35 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  2. What's wrong with this code?
    By Finchie_88 in forum Networking/Device Communication
    Replies: 10
    Last Post: 05-27-2005, 09:46 AM
  3. What's wrong with my Win32 Wrapper code?
    By miica in forum Windows Programming
    Replies: 9
    Last Post: 02-22-2005, 08:55 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. what's wrong with this qsort code?
    By Sargnagel in forum C Programming
    Replies: 2
    Last Post: 01-12-2003, 06:58 AM