Reading/Writing Files - Is this correct?
Apr 21, 2015 at 3:33pm UTC
I need someone to run this code for me (or just read it if you're an expert) and tell me if it is syntactically correct and if it runs smoothly. If not, it would be MUCH appreciated if you could offer fixes. Thanks!
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>
#include <fstream>
#include <string>
using namespace std;
int main()
{
float grade, average, totalGrade = 0;
ifstream ins;
ofstream outs;
string first, last;
int count = 0;
ins.open("grades.txt" );
outs.open("result.txt" );
while (!ins.eof())
{
ins >> first >> last >> grade;
totalGrade = totalGrade + grade;
count++;
}
average = totalGrade/count;
ins.close();
cout << "The average grade is: " << average;
outs << "The average grade is: " << average;
outs.close();
system ("pause" );
return 0;
}
Last edited on Apr 21, 2015 at 5:26pm UTC
Apr 21, 2015 at 6:39pm UTC
You forgot to post your source file OP, how can we know if this is right if we don't even know the format the data is in?
Why didn't you just open the files when you constructed the stream objects?
You are over writing your variables 'first' and 'last'. So they will only have the last entry saved to them.
Topic archived. No new replies allowed.