Hi guys , i m doing a programme that can read a text files that contains number, student's ID,1st exam and final exam marks and it can calculate the total of 1st and final exam in order to grade the students. the programme then prints out the calculated total marks and grades into another output text file. here's what i tried:

#include <stdio.h>



int getStu (FILE* spStu,int* number,int* stuID, int* exam1, int*final);
int writeStu (FILE* spGrades,int number,int stuID,int total,char grade);
void calcGrade (int exam1,int final,int* total,char*grade );

int main (void)
{
FILE* spStu;
FILE* spGrades;

int number;
int stuID;
int exam1;
int final;	
int total;

char grade;

printf ("Begin Student grading\n");
spStu = fopen ("studentsmark.txt","r");

spGrades = fopen ("studentgrade.txt","w");
	

while (getStu(spStu,&number,&stuID,&exam1,&final) );

{
	calcGrade (exam1,final,&total,&grade);
	writeStu (spGrades,number,stuID,total,grade)!=EOF;
}

fclose (spStu);
fclose (spGrades);

printf ("End student grading\n");
return 0;
}

int getStu (FILE* spStu,int* number,int *stuID, int *exam1,int *final)

{
	int ioresult;

	ioresult = fscanf (spStu,"%d%d%d%d",number,stuID,exam1,final);

	if (ioresult == EOF)
		return 0;
	else if (ioresult !=4)
	{printf ("\aError in reading data\n");
	return 0;
	}
	else
	
	

		return 1;
	
}

void calcGrade (int exam1,int final,int* total,char* grade)
{
	*total = exam1 + final;

	if (*total >=80)
		*grade ='A';

	else
	*grade = 'F';

	return ;
}
int writeStu (FILE* spGrades,int number,int stuID,int total,char grade)
{
	fprintf (spGrades,"%d  %04d  %d  %c\n",number,stuID,total,grade);
	return 0;
}

input file example:
1 0050 30 70
2 0030 20 30
3 0020 10 10

output file example
1 0050 100 A
2 0030 50 F
3 0020 20 F

however my output file is just
3 0020 20 F

so any corrections i should make to the code to do what's it suppose to do instead of printing only the last line ?

Please, take a look here at how to properly tag your code, so it can keep the format for C language.
That makes it easier to read.

To start:

while ((getStu(spStu,&number,&stuID,&exam1,&final)) != 0 );
{
    calcGrade (exam1,final,&total,&grade);
    writeStu (spGrades,number,stuID,total,grade)!=EOF;
}

Remove what's in red. Add what's in green.

This article has been dead for over six months. Start a new discussion instead.