/*
Algorithm findPassFail()
This algorithm reads in a text file containing information on a class and determins if students passed or failed.
It then prints the student number, name, both marks, the overall weighted average, and weither the student passed or failed.
pre:a data file formated as to have the student number (an integer), the student's lastname (a string up to 20
charachters long), the midterm grade (a float between 0 and 100) and the final grade (a float between 0 and 100).
post: It prints the student number, name, both marks, the overall weighted average, and weither the student passed or failed.
return: void
*/
#include <stdio.h>
#include <string.h>
int main (void) {
char fileName[16];
int studentNum = 0;
char studentName[12];
float studentMark1 = 0;
float studentMark2 = 0;
float studentWeightAv = 0;
char passOrFail[5] = "\0";
FILE *studentFile; // file where student data is kept
printf("Enter the file name \n");
scanf("%s", fileName);
studentFile = fopen(fileName, "r");
if(studentFile == NULL) { //this will be true if there was an
perror("failed to open"); //error such as file not existing
return -1;
}
while(!feof(studentFile)){
fscanf(studentFile, "%i%s%f%f", &studentNum, studentName, &studentMark1, &studentMark2);
studentWeightAv = ((studentMark1 * 0.40)+(studentMark2 * 0.60));
if(studentWeightAv > 50)
strcat(passOrFail, "pass\0");
else
strcat(passOrFail, "fail\0");
if(!feof(studentFile)){
printf("%i %s %.2f %.2f %.2f %s \n",
studentNum, studentName, studentMark1, studentMark2, studentWeightAv, passOrFail); // long line broken up
}
};
fclose(studentFile);
return 1;
}
what is going wrong here with
if(studentWeightAv > 50)
strcat(passOrFail, "pass\0");
else
strcat(passOrFail, "fail\0");
it prints "pass" on the first line from the file then "passpass" on the second then "passpasspass" then "passpasspassfail" then garbage.