I have a project that needs to be modified to omit invalid data from a function that averages grades. It also needs to print "Invalid data" in place of the data. I am burnt out from finals and I can't think straight. Any help would be greatly appreciated
//Purpose:
//1 to read information from a file (name, IDnumber, test scores,1,2,3)
//2 determine if the test scores are valid (score>=0 && <=100)
//3 if invalid, display"invalid data" in place of data
//4 omit invalid data from all calculations
//5 calculate averages of scores
//6 assign a letter grade value to score
//7calculate class average
#include <stdio.h>
#include <string.h> // a string will be used for name input
int main () {
char firstname [40];
char lastname [40];
char fullname [80];
int score1, score2, score3,idnum;
float averageclass, sum;
int c;
int students; // name of file
int valid (int,int,int);
float average;
char gradef (float);
FILE * datafile;
datafile = fopen("D://students.txt","r");
if (datafile == NULL)
{
printf("Could not open data file\n");
return 0;
}//end if
sum = 0;
fscanf(datafile, "%d", &students);
printf("\n %d Students will be read from file\n\n",students);
for(c = 1 ;c <= students; c++)
{
fscanf(datafile,"%s", firstname);
fscanf(datafile,"%s", lastname);
fscanf(datafile,"%d", &idnum);
fscanf(datafile,"%d",&score1);
fscanf(datafile,"%d", &score2);
fscanf(datafile,"%d", &score3);
if (valid(score1, score2, score3))
{// begin if
average = (score1 + score2 + score3) / 3.0; //calculates average of scores
sum = sum + average;
strcpy(fullname, lastname);
strcat(fullname,", ");
strcat(fullname, firstname); // string to store name first-last and display last name first
printf(" %s \t %d ", fullname, idnum);
}// end if
else
printf(" Invalid data");// this needs to be printed instead of (test scores1,2,3 and average)
// also, I must exclude this invalid information from the class average
printf("\t %d \t %d \t%d", score1, score2, score3);
printf("\t%.2f \t %c \t\n\n", average, gradef(average));
} //end of loop
fclose(datafile);
averageclass = (sum / students);
printf("\n\nThe class average is: ");
printf(" %.2f\n\n", averageclass);
}//end of main
int valid (int score1, int score2, int score3) //valid function
{// start of valid
int good;
return( (score1 >= 0) && (score1 <= 100) && (score2 >= 0) && (score2 <= 100) && (score3 >= 0) && (score3 <= 100));
}// end of valid
char gradef (float average)//grade function
{
char grade;
if (average <= 60) grade ='F';
if (average >= 60 && average < 70) grade = 'D'; // assigns letter grade to numerical value if (average >= 70 && average < 80) grade = 'C';
if (average >= 70 && average < 80) grade = 'C';
if (average >= 80 && average < 90) grade = 'B';
if (average >= 90 && average < 100) grade ='A';
return grade;
}
Current display
8 Students will be read from file
Clinton, Bill 1111 92 64 91 82.33 B
Bush, George 2222 66 42 75 61.00 D
Reagan, Ronald 3333 50 50 56 52.00 F
Invalid data 82 -5 91 52.00 F
Invalid data 118 64 88 52.00 F
Nixon, Richard 6666 90 90 93 91.00 A
Johnson, Lyndon 7777 78 90 68 78.67 C
Kennedy, John 8888 52 96 75 74.33 C
The class average is: 54.92
Press any key to continue