-
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
-
Are you expecting someone to do this for you?
-
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
-
-
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!
-
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).
-
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!
-
Well yes because this line:
printf("Player %d scored %d\n\n", golfer);
has two %d, the second one refers to?
-
And where the hell are you printing the word "Breakdown" that you expect to see. Is this code yours?
-
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.
-
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
-
Ok well still, that first printf is wrong, you are trying to print two integers but are only supplying one. Remove the second %d.
-
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 :/ ..
-
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.
-
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