Thread: Headdesk/facepalm

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    2

    Headdesk/facepalm

    I've been searching teh internets and this forum for an answer for quite some time now, so forgive me if this post is as ridiculous as I think it is.

    I'm writing a very simple, very belts and braces program for a golf score card for my local course. It should use nothing more than int, plus, scanf and printf.

    I hit compile, the DOS window pops up and asks the user to input a score for each hole. After the 9 holes have been entered (yes, it's only a small course, but I like it there) there's a sarcastic comment and the final score should show up. What I get though is the final score equaling 2293540... Even I'm not that bad.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int hole_1;
        int hole_2;
        int hole_3;
        int hole_4;
        int hole_5;
        int hole_6;
        int hole_7;
        int hole_8;
        int hole_9;
        int final_score;
        final_score = 0;
        printf("Welcome to the Himley GC Score Sheet\n");
        printf("Please enter your scores for each hole\n");
        printf("Press any key to continue\n");
        getchar();
        printf("Hole 1 is 330 Yards, Par 4 and Stroke Index 9\n");
        printf("What did you get?\n");
        scanf("%d", &hole_1);
        final_score = final_score + hole_1;
        if ( hole_1 < 4 ) {
         printf ("Shooting Tex!\n" );
         }
      else if ( hole_1 == 4 ) {
         printf( "Nice Par.\n" );
      }
      else if ( hole_1 == 5 ) {
        printf( "Unlucky dude.\n" );
      }
      else printf("Get it together!\n");
        printf("Hole 2 is 325 Yards, Par 4 and Stroke Index 11\n");
    And so on until hole 9:
    [...]
    Code:
      else printf("Get it together!\n");
        printf("Dude, really?\n");
        printf("You're not very good at this game are you?\n");
        printf("\n");
        final_score = final_score + hole_9;
        printf("Your final score was %d", &final_score);
        getchar()
        return 0;
    }
    Now, I fully realise that this is the most basic and laborious way of doing this job. I fully intend to split out the data entry into subroutines, include look up tables for different course's pars and stroke indexes and have the course selected at the start. Like I said, it's basic. I'm a bit concerned that I can't seem to perform this very simple running total. Am I actually updating the final_score or not printing final_score correctly or both?

    Again, I've really searched for an answer but everyone seems to get past page 1 of "Hello World!" without problem.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Your cascade of 9 hole? ints probably should be converted to an array...

    Calculate the score on the fly..
    Code:
    int hole[9] = {0}
    int next = 0;
    int score = 0;
    
    
    // get individual hole scores.
    // input score to  hole[next]
    // increment next for next hole
    
    
    // calculate game score
    score = 0;
    while ( next-- )
      score += hole[next];
    For the bizarro output you got ....
    Code:
    printf("Your final score was %d", &final_score);
    Lose the &.
    Last edited by CommonTater; 06-02-2011 at 11:06 AM.

  3. #3
    Registered User Inanna's Avatar
    Join Date
    May 2011
    Posts
    69
    printf("Your final score was %d", &final_score);
    You have an address operator on final_score. That makes printf print the address of final_score instead of the value of final_score.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2
    Quote Originally Posted by CommonTater View Post
    Your cascade of 9 hole? ints probably should be converted to an array...

    Calculate the score on the fly..
    Thank you for that. Like I said, I will be doing it properly as I learn more but this first effort is purely to build a bid of confidence and to participate in the age old sport of Running before I can Crawl.

    Quote Originally Posted by Inanna View Post
    You have an address operator on final_score. That makes printf print the address of final_score instead of the value of final_score.
    Brilliant. That's exactly what I was after. Thank you. It now works like a charm. A long winded charm but a charm all the same.

    Did I say "Thank you" yet?

Popular pages Recent additions subscribe to a feed