Code:
#include<stdio.h>
main()
{
FILE *inCanResults, *outCanResults;
inCanResults = fopen("data2a.txt", "r");
outCanResults = fopen("out2a.txt", "w");
int ctr, idNum, j1, j2, j3, total, count, totalScore, e;
float avg;
e=fscanf(inCanResults,"%d %d %d %d", &idNum, &j1, &j2, &j3);
printf(" Candidate Results\n");
printf("Id J1 J2 J3 totalScore\n");
fprintf(outCanResults," Candidate Results\n");
fprintf(outCanResults,"Id J1 J2 J3 totalScore\n");
ctr = 0;
total = 0;
while (e==4)
{
totalScore = j1 + j2 + j3;
printf("%d %5d %5d %5d %9d\n", idNum, j1, j2, j3, totalScore);
fprintf(outCanResults,"%d %5d %5d %5d %9d\n",
idNum, j1, j2, j3, totalScore);
ctr = ctr + 1;
count = count + 1;
total = totalScore + total;
e=fscanf(inCanResults,"%d %d %d %d", &idNum, &j1, &j2, &j3);
}
avg = total / ctr;
printf(" %d Total Candidates\n", count);
fprintf(outCanResults," %d Total Candidates\n", count);
printf(" Average total score is %.1f\n", avg);
fprintf(outCanResults," Average total score is %.1f\n", avg);
printf("Top candidate is number %d with score of %d\n",
idNum, totalScore);
fprintf(outCanResults,"Top candidate is number %d with score of %d\n",
idNum, totalScore);
printf("Bottom is number %d with score of %d\n", idNum, totalScore);
fprintf(outCanResults,"Bottom is number %d with score of %d/n",
idNum, totalScore);
fclose(inCanResults);
fclose(outCanResults);
system("pause");
}
The book we are using doesn't explain this very well so I would appreciate some help. Thank you!!