File Handling Problem

I'm new to C++. And I got stuck. What we have to do is input the marks for 3 students, store it in a file, retrieve said file, calculate the average marks and also find out the maximum marks received in maths and who received it. The following URL gives the code in it's entirety:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream.h>
#include <fstream.h>
#include <conio.h>

int main ()
{ int p,c, m, sumP =0, sumC = 0, sumM = 0, M=0;
  char name[80], str [80], ch;
  
  ofstream fileout ("marks.txt", ios::out);
  for (int i=1; i<4; i++)
    { cout << "\nEnter name of student " << i << ": "; fflush (stdin); gets (name);
      fileout << name<<"\n";
      
      cout << "Enter marks for Physics, Chemistry, Maths :"; 
      cin >> p >> c >> m;
      fileout << "Physics: " << p << "\nChemistry: " << c << "\nMaths: " << m <<"\n";
    }
     
 fileout.close ();
 
 ifstream filein ("marks.txt", ios::in);
 
 while (filein)
  {filein >> p >> c >> m; 
   sumP += p; sumC+=c; sumM+=m;
   
  
   if (m>M) {M=m; filein.getline (str, 80 ,'\n');}
   
 }
 filein.close ();
 
 cout << "Average = " << sumP/3 << "  " << sumC/3 << "  " << sumM/3;
 cout << "Maximum marks" << M << "received by "; puts (str);
 
 getch ();
 return 0;
}



My problem is that the file is created fine. it saves everything I checked. But while reading it only the last student's data is shown. I have no clue why that is.
Last edited on
I don't think it reads anything at all from the file. The last student's data seem to be correct is probably because the read operations on line 24 failed so the variables kept their old values.
Oh shit. You're right.
Any idea as to how I can fix it?
Last edited on
Well, it would be easier if you simplified the file format. If you only write p, c and m separated with whitespace (like space or newline) to the file ...
 
fileout << p << ' ' << c << ' ' << m << "\n";
... you can read the file the way you have it now.

Well, that would almost work. The way you have it now the second loop will run one time too many. The reason is that the loop condition (line 23) will not be false until one of the read operations has failed. A better way is to put what you have on line 24 as the loop condition instead. That way it will read the data and check that it was read successfully before each iteration.
 
while (filein >> p >> c >> m)
Last edited on
I did the correction. Good news, it's reading it now, I think. Bad news, I have no clue what's happening.

1
2
3
4
5
6
7
8
9
10
while (filein)
  {filein.getline (name, 80 ,'\n');
       
   filein >> p >> c >>  m ;
   cout << p << m <<c; getch (); //to check the output

   sumP += p; sumC+=c; sumM+=m;
   
  
   if (m>M) {M=m; strcpy (str, name); }


Enter name of student 1: a 
Enter marks for Physics, Chemistry, Maths :3 1 2 

Enter name of student 2: b 
Enter marks for Physics, Chemistry, Maths :1 2 3 

Enter name of student 3: c 
Enter marks for Physics, Chemistry, Maths :2 3 1 
321321 Average = 2 0 1 Maximum marks 2 received by a 

Topic archived. No new replies allowed.