Like Tree9Likes

Golfgame

This is a discussion on Golfgame within the C Programming forums, part of the General Programming Boards category; Hello, This should be very easy for some people but I just cant get a grasp on it The score ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49

    Post Golfgame

    Hello,

    This should be very easy for some people but I just cant get a grasp on it

    The score is calculated by subtracting each user’s score from the par. For example, if the user needed 100 strokes to finish 18 holes, and the total par for the 18 holes is 90, their score is 10. Likewise, if the user only needed 80 and the total par was 90, their score is -10. In golf, lower numbers indicate better scores.

    Up to 4 users can play a single game of golf. Their scores for each individual hole will be recorded in a file along with the par for each hole. This file will be the input for your scoring system. Your scoring system should calculate the final score for each of the players and output this value to the screen along with the player’s score breakdown.
    1. The input will be in a file called input.txt
    The first line of the file will contain a single positive integer, n (1 <= n <= 4), specifying the number of players.

    The second line of the file will contain 18 positive integers that represent the par for each of the holes, from hole 1 to hole 18.

    The following n lines will contain 18 positive integers, representing each user’s individual scores for each hole.

    For each of the n players, output a line with the following format:

    Player X scored Y.
    Breakdown: A B C D E F G H I J K L M N O P Q R

    X represents the player’s number: 1, 2, 3, or 4. Y represents their final score. Y may be negative.
    The other letters represent the score for each hole. (A for hole 1, B for hole 2, etc.)

    At the end, output which of the players in the winner in the following format:

    Player X is the winner!

    I have created a input.txt document that should store all of what I got into my document

    2//Players
    4 3 3 4 4 5 4 3 4 4 3 4 4 3 5 3 4 4
    2 3 4 4 4 5 5 3 3 4 4 4 4 4 4 3 3 4
    4 4 5 4 6 4 5 6 3 4 4 4 5 4 3 3 5 4


    Last edited by Bsmooth01; 03-28-2012 at 09:17 AM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Are you expecting someone to do this for you?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49
    No not at all. I mean I have a little bit of code so far but Im just stuck at how to display the final scores after I do my loops

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well post it here then.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49
    insert
    Code:
     #include <stdio.h>
    
    
    int main() {
    
    
      FILE *ifp;
      int golfer, counter, score, total=0, i;
    
    
      ifp = fopen("input.txt","r");
    
    
    
    
        fscanf(ifp, "%d", &golfer);
        printf("Player %d scored %d\n\n", golfer);
    
    
        for(counter = 1; counter < golfer; counter++) {
    
    
            for(i = 0; i < 18; i++) {
            fscanf(ifp, "%d", &score);
            total += score;
          }
          printf("Player %d Score: %d\n\n", golfer, total);
    
    
    }
    
    
    return 0;
    
    
    }

    For some reason when I run it It doesnt have the right input than when I ran it yesterday. Previously when I ran it and altered some things I seemed to be on the right track but Idk what happened.

    Heres a sample

    Player 1 scored 7.
    Breakdown: 1 0 1 0 2 -1 0 3 0 0 0 0 1 0 -2 0 2 0

    Player 2 scored -3.
    Breakdown: -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0

    Player 2 is the winner!

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Did you change the input file? What exactly is the problem? What are you getting as output and what are you expecting to get as output?

    Also remember to close the file after you are done reading from it with fclose(ifp).
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49
    No the input..txt is still the same. But everytime I run it I get Player 2 scored 2686732 and Player 2 core:68

    But im supposed to get something along the lines of
    Player 1 scored 7.
    Breakdown: 1 0 1 0 2 -1 0 3 0 0 0 0 1 0 -2 0 2 0

    Player 2 scored -3.
    Breakdown: -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0

    Player 2 is the winner!

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well yes because this line:
    printf("Player %d scored %d\n\n", golfer);

    has two %d, the second one refers to?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    And where the hell are you printing the word "Breakdown" that you expect to see. Is this code yours?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49
    Break down is an example, You can use any word for example to see the 18 scores you can use score or counter which I used in mine. It was my instructors example code.

  11. #11
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49
    The second %d is suppose to refer to the sum of the number. As you see 1 0 1 0 2 -1 0 3 0 0 0 0 1 0 -2 0 2 0 = 7

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok well still, that first printf is wrong, you are trying to print two integers but are only supplying one. Remove the second %d.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  13. #13
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49
    Ok I removed it. Im trying to find a shorter way to input all 18 number without actually using some huge formula in my code and before today I had everything going just right but unfortunately when You let people borrow a laptop they mess around :/ ..

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, so everytime you change something repost the entire code again, I am not a version tracking software and can't remember all the changes you made. Then say what you are getting as output. Better yet, copy and post it here.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  15. #15
    Registered User
    Join Date
    Mar 2012
    Location
    US
    Posts
    49
    int
    Code:
    #include <stdio.h>
    
    int main() {
    
    
      FILE *ifp;
      int golfer, players, counter, score, total=0, i;
    
    
      //Open Input File
      ifp = fopen("input.txt","r");
    
    
        fscanf(ifp, "%d", &golfer);
        printf("Player %d scored %d\n\n", golfer, total);
    
    
        total += score;
    
    
        for(counter = 1; counter < golfer; counter++) {
    
    
            for(i = 0; i < 18; i++) {
            fscanf(ifp, "%d", &score);
            total += score;
          }
          printf("Player %d Scored: %d\n\n", golfer, total);
    
    
    }
    fclose(ifp);
    
    
    return 0;
    
    
    }
    My out put is Player 2 scored 0 and Player 2 scored:400237
    douglas481 likes this.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21