I've been assigned a program requiring loops. The program's objective is to read and tally certain letters of baseball player record. The txt file looks like this:
12 HOOPSTOHOPST
4 OSPSHOOSOPSPPSH
7 OOHOSPHPPSHHOSPW
The numbers are the player numbers.
The assignment requires a program that calculated average batting score, which are just the sum of Hs and Ps divided by the total number of letters.
So I've been trying to make a loop that simply reads and displays the player's number and record
Code:
# include <stdio.h> 2
3 int main (int argc, char *argv[] )
4
5 {
6 int player;
7 char at_bat;
8 FILE *fp;
9
10 fp = fopen( argv[1], "r" );
11 fscanf(fp, "%c", &at_bat);
12
13 while ((fscanf(fp, "%d", &player)) != EOF)
14 {
15 printf("Player number is %d.\n", player);
16 fscanf(fp, "%c", &at_bat);
17 printf("_%c_", at_bat);
18 while ((fscanf(fp, "%c", &at_bat)) != '\n')
19 {
20 fscanf(fp, "%c", &at_bat);
21 printf("%c", at_bat);
22 }
23 }
24 return(0);
25 }
My program cycles endlessly and prints no text. How can I get this loop to just print out the txt file using a loop? I'm sure if I could get fimiliar with the format of while, I could handle the math. Am I using the EOF correctly? Please help me. - Thanks for your time. - Kurt