I am trying to have the final scores print our on a file from the highest score
ID: 3547041 • Letter: I
Question
I am trying to have the final scores print our on a file from the highest score to the lowest, but I can not seem to get it to work.
#include <stdio.h>
#include <stdlib.h>
void printData(char name[][50], double dScore[], double eScore[], int max);
void sumScore(char names[][50], double dScore[], double eScore[], double finalScore[], int max);
int main()
{
FILE *myFile;
int i;
double dScore[3], eScore[3], finalScores[3];
char names[3][50];
myFile = fopen("scores.txt","w"); // w for write
if(myFile == NULL){
printf("Can't open file! ");
return 0;
}
printf("This program will print out the resluts of the gymnast. ");
for(i=0;i<3;i++){
printf("Enter the Last Name, D-Score and E-Score of the competitor. ");
scanf("%s %lg %lg",names[i],&dScore[i],&eScore[i]);
if(dScore[i] < 0){break;}
}
printData(names, dScore, eScore, i);
fprintf(myFile,"The Final Results are: ");
fclose(myFile);
sumScore(names, dScore, eScore, finalScores, i);
}
void printData(char names[][50], double dScore[], double eScore[], int max)
{
int j;
for(j=0;j<max;j++){
printf("Name: %s D-Score: %g E-Score; %g ", names[j],dScore[j],eScore[j]);
}
}
void sumScore(char names[][50], double dScore[], double eScore[], double finalScore[], int max)
{
int i,j,swap;
for(i=0;i<max;i++){
finalScore[i] = dScore[i] + eScore[i];
fprintf(myFile,"Name: %s Final Score: %g ", names[i],finalScore[i]);
}
for(i=0;i<max;i++){
for(j=0;j<max-i-1;j++){
if(finalScore[j]>finalScore[j+1]){
swap=finalScore[j];
finalScore[j]=finalScore[j+1];
finalScore[j+1]=swap;
}
}
}
}
Explanation / Answer
Try changing your code as this.
fprintf(myFile,"The Final Results are: ");
sumScore(names, dScore, eScore, finalScores, i);
fclose(myFile);
You are closing the file before you are printing the results to the file.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.