# Thread: Can someone please help me with my program? I am new to C programming.

1. ## Can someone please help me with my program? I am new to C programming.

Hello,
I was wondering if someone can help me with file input and output.
I basically have golf.txt document that contains data from 8 golfers' scores while playing a game of golf. I used the data in the golf.txt document to create a program that read values from the .txt document and calculates the total score of each golfer. However, now i need to take it a step further and create a program that compares each golfers score to the par score and outputs a value for hole by hole.

The program should output 18 different values for each golfer, where each value will contain (the number of tries- par score= score for that hole) and there are 18 holes in a game of golf so it make sense

Each letter represents each golfers score on each hole. (A for hole 1, B for hole 2..... R for hole 18)

It should look something like this for each golfer:
Golfer #1’s score: A B C D E F G.....R
Golfer #2.................
Golfer #3..................

This should not be too hard because I have it all the data I need in my golf.txt file:

8 // This is the number of golfers
4 4 3 5 4 3 4 5 6 5 4 4 4 5 5 4 3 3 // these are the par scores
4 4 3 6 7 4 4 5 4 5 3 5 3 5 7 5 3 2 // for here down is each of the eight golfers score
4 5 3 4 6 5 5 6 3 5 4 6 3 3 5 4 5 3
3 4 5 6 4 4 4 5 5 5 4 5 4 3 3 5 4 3
2 5 5 6 7 6 5 5 4 6 4 5 6 7 5 5 5 5
4 5 6 3 3 5 4 4 3 3 2 5 6 6 5 5 3 3
2 5 3 5 5 5 6 5 4 3 4 5 5 4 4 6 5 5
6 5 4 5 5 3 5 6 5 4 6 5 6 4 4 6 5 4
4 4 5 5 3 3 6 5 4 3 3 5 6 5 3 6 5 4

MY QUESTIONS:

1) Would arrays be the best way to go about completing this program?
2) Would I need a 1d or 2d array to complete this program?
3) If so, I am completely new to arrays! can anyone help me understand how I would set it up? I know that the basic format is something like int numbers [rows][columns]
4) How would I add/ subtract array's positions from each other (for example row 2 column 3 + row 2 column 4) ?

Thank You,
Matt H.

2. 1. Arrays would definitely be useful.
2. I would use a 1d array for the par scores and either a 2d array or an array of structs for the golfer scores. Since you're new to this, I would go with the 2d array for golfer scores.
3. I'd make it an int[# golfers][18]
4. You would use a loop to iterate through each element and add the elements just like any other variable. For example, part of your program might look like this:

Code:
```for (i=0; i<18; ++i)
{
score[n][i] = tries[n][i] - par[i];
}```

3. Thank you for the response,

For some reason my array program is not even compiling correctly...
I am not even sure if I am on the right track... Can someone help me out and take a look?

This is what i got so far:

Code:
```#include <stdio.h>

int main() {

FILE *ifp;
int ttl_golfers, counter, totalscore, ok, j, i, z, n;
int tries [9][18];
int score [9][18];
int parscore[18];

ifp = fopen("golf.txt","rt");

fscanf(ifp, "%d", &ttl_golfers);
printf("\n%d golfers played on this golf course:\n", ttl_golfers);

for (z=0; z < 18; z++)  {
fscanf(ifp, "%d", &parscore);
parscore[18] = parscore[z];
}

for(i = 0; i < 18; i++) {
fscanf(ifp, "%d", &score);
score[n][i] = tries[n][i]- parscore[i];
}
for(counter=0; counter < ttl_golfers; counter++) {
}

printf("\nGolfer #%d's Score: %d\n", counter, score-parscore);

}

fclose(ifp);

system("PAUSE");
return 0;

}```

4. Code:
`fscanf(ifp, "%d", &parscore);`
you are missing [z] after parscore

5. This is my new code and it is still not compiling our outputting any values:/

Any ideas?

Code:
```#include <stdio.h>

int main() {

FILE *ifp;
int ttl_golfers, counter, totalscore, ok, j, i, z, n, p, o;
int tries [9][18];
int score [9][18];
int parscore[18];

ifp = fopen("golf.txt","rt");

fscanf(ifp, "%d", &ttl_golfers);
printf("\n%d golfers played on this golf course:\n", ttl_golfers);

for (z=0; z < 18; z++)  {
fscanf(ifp, "%d", &parscore[z]);
parscore[z] = parscore[z];
}

for(i = 0; i < 18; i++) {
fscanf(ifp, "%d", &score[n][i]);
score[n][i] = tries[n][i]- parscore[z];
}
for(counter=0; counter < ttl_golfers; counter++) {
}

printf("\nGolfer #%d's Score: %d\n", counter, score[n][i]-parscore[z]);

}

fclose(ifp);

system("PAUSE");
return 0;

}```

6. Code:
```for(i = 0; i < 18; i++) {
fscanf(ifp, "%d", &score[n][i]);
score[n][i] = tries[n][i]- parscore[z];
}```
where n is initialized?

7. Hm... Im not sure what u meant by "Where n is initialized?" but I guess u mean that I forgot to include n=0? right? Sorry, im not very good with programming yet, im working on it

This is my new code (which I am still not able to compile:

Code:
```#include <stdio.h>

int main() {

FILE *ifp;
int ttl_golfers, counter, totalscore, ok, j, i, z, n=0, p, o;
int tries [9][18];
int score [9][18];
int parscore[18];

ifp = fopen("golf.txt","rt");

fscanf(ifp, "%d", &ttl_golfers);
printf("\n%d golfers played on this golf course:\n", ttl_golfers);

for (z=0; z < 18; z++)  {
fscanf(ifp, "%d", &parscore[z]);
parscore[z] = parscore[z];
}

for(i = 0; i < 18; i++) {
fscanf(ifp, "%d", &score[n][i]);
score[n][i] = tries[n][i]- parscore[z];
}
for(counter=0; counter < ttl_golfers; counter++) {
}

printf("\nGolfer #%d's Score: %d\n", counter, score[n][i]-parscore[z]);

}

fclose(ifp);

system("PAUSE");
return 0;

}```

8. Code:
```}

fclose(ifp);

system("PAUSE");
return 0;

}```
this is in its own stament block and is all messed up, delete the topmost } in this code snippet above^^

9. Code:
```   for(i = 0; i < 18; i++) {
fscanf(ifp, "%d", &score[n][i]);
score[n][i] = tries[n][i]- parscore[z];
}
for(counter=0; counter < ttl_golfers; counter++) {
}

printf("\nGolfer #%d's Score: %d\n", counter, score[n][i]-parscore[z]);```
the bolded code above should be inside the for loop above (its an empty for loop) at this point if you fix that, it does in fact compile....however it segfaults immediately upon running, ill leave that up to you to find out why

10. Thank you! My program is finally compiling

I am unsure what u meant by "the bolded code" because it did not show up in bold lettering. I might of misunderstood you:/

11. My program is now compiling correctly, however it is running completely wrong...

Any ideas or advice? .... I think my logic for my loops are incorrect but it makes sense to me

Code:
```#include <stdio.h>

int main() {

FILE *ifp;
int ttl_golfers, counter, totalscore, ok, j, i, z, n=0, p, o;
int tries [9][18];
int score [9][18];
int parscore[18];

ifp = fopen("golf.txt","rt");

fscanf(ifp, "%d", &ttl_golfers);
printf("\n%d golfers played on this golf course:\n", ttl_golfers);

for (z=0; z < 18; z++)  {
fscanf(ifp, "%d", &parscore[z]);
parscore[z] = parscore[z];
}

for(counter=0; counter < ttl_golfers; counter++) {
for(i = 0; i < 18; i++) {
for(n = 0; n < 8; n++)
fscanf(ifp, "%d", &tries[n][i]);
score[n][i] = tries[n][i]- parscore[z];
printf("\nGolfer #%d's Score: %d\n", counter, tries[n][i]-parscore[z]);

}
}

fclose(ifp);

system("PAUSE");
return 0;

}```

12. Code:
```   for(counter=0; counter < ttl_golfers; counter++) {
}

printf("\nGolfer #%d's Score: %d\n", parscore[z]);```
As I said prior the printf above sould go into the for loop just above it, right now that for loop does absolutely nothing.

Beyond that however your problems are mainly due to excessive looping and using an inferior method of reading in your records.

Whenever reading data from a file (a flat text file, not binary) you should be using fgets and an appropriate buffer and you should verify the line was read in correctly before attempting to use it or split it up into scores/arrays

13. Can you please view my "newer" code because I adjusted my code after the advice you gave me.

Look at post #11 (two responses up)

Thank you for your help

14. Originally Posted by nonpuz
Beyond that however your problems are mainly due to excessive looping and using an inferior method of reading in your records.

Whenever reading data from a file (a flat text file, not binary) you should be using fgets and an appropriate buffer and you should verify the line was read in correctly before attempting to use it or split it up into scores/arrays
refer to this ^^

15. Oh okay...

What exactly is a buffer?

See I tried to make sure that my code is getting read in correctly but it is not, which is not surprising, but why is this? Even when it is as simple as reading in the first value in the document its reading it as 46 instead of 8

Popular pages Recent additions