I am supposed to create a program that reads from an input file of names and grade scores, calculate the average and assign a total grade:
The input file looks like:
Michael Jackson
88 90 93 89 94
Tony Gwynn
78 93 92 84 80
...and so on, with a name on one line followed by scores on the next
My program works perfectly as far as calculation goes, but I am supposed to get all the data to print out on one line. I get this when I execute:
Michael Jackson
Average = 90.8 Grade = A
But I am supposed to have:
Michael Jackson, Average = 90.8, Grade =A
What am I doing wrong? My code so far:
Code:
{
FILE *infile, *outfile;
char text[81], ch_grade;
double average;
int lines, k, score [5], i_ave;
infile = fopen("score.dat", "r");
if(infile == NULL){
printf("File Opening Error!\n");
exit(1);
}
lines=0;
while(fgets(text,81,infile)!=NULL){
lines++;
if (lines%2 !=0){
printf("Student %s", text);
}else {
sscanf(text, "%d %d %d %d %d",
score, score+1, score+2, score+3, score+4);
for(k=0, average=0.0; k<5; k++)
average += (double)score[k];
average /= 5.0;
i_ave = (int)(average + 0.5);
if(i_ave >=85) ch_grade = 'A';
else if(i_ave >= 75) ch_grade = 'B';
else if(i_ave >= 60) ch_grade = 'C';
else if(i_ave >= 40) ch_grade = 'D';
else ch_grade = 'F';
printf("Average =%5.1f;", average);
printf(" Grade = %c.\n", ch_grade);
}
}
}