c++ - Functions using structure read from file return no output? -
i supposed declare structure holds name , grades of unknown number of students. here how did it:
struct student { char name[30]; int grade1, grade2, grade3; };
then, have text file containing information each student on different lines. read way.
student info[20]; int nr_students,i=0,k=0; fstream infile; infile.open("in.txt"); fstream outfile; outfile.open("out.txt"); while(!eof) { infile>>info[i].name; infile>>info[i].grade1; infile>>info[i].grade2; infile>>info[i].grade3; i++; } nr_students=i; avg_promovability(outfile,info, nr_students);
i not think works properly, since nr_students
remains 0, apparently. also, first function:
void avg_promovability(fstream &outfile,student *info, int nr_students) { int i; float sum=0,j=0,avg; (i=0;i<nr_students;i++) if(info[i].grade1 >=5 && info[i].grade2>=5 && info[i].grade3>=5 ) { sum=sum + info[i].grade1 + info[i].grade2 + info[i].grade3; j++; } avg=sum/(3*j); outfile<<avg; }
it's supposed calculate average grade of students have passed (that is, grades >=5). doesn't it, out.txt file empty. suggestions i'm doing wrong?
that not way check end of file. try
while (infile>>info[i].name) { infile>>info[i].grade1; infile>>info[i].grade2; infile>>info[i].grade3; i++; }
Comments
Post a Comment