Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;
void grades (ifstream& in, ofstream& out);
double avg (ifstream& in, ofstream& out);
int main ()
{
char next;
double average;
ifstream fin;
ofstream fout;
fin.open ("F:\\CS 2\\scores.txt");
if (fin.fail())
{
cout << "File failed to open.\n";
system ("pause");
exit (1);
}
fout.open ("F:\\CS 2\\grades.txt");
if (fout.fail())
{
cout << "File failed to open.\n";
system ("pause");
exit (1);
}
grades (fin, fout);
fin.close ();
fout.close ();
system ("pause>nul");
}
void grades(ifstream& in, ofstream& out)
{
char next;
double average;
while (! in.eof())
{
in.get(next);
while (next != '\n')
{
cout << next;
out << next;
in.get (next);
}
average = avg (in, out);
cout << ' ' << average;
out << ' ' << average;
}
}
double avg (ifstream& in, ofstream& out)
{
string last, first;
double score [10], average = 0;
int i;
out.setf(ios::fixed);
out.setf(ios::showpoint);
out.precision(2);
in >> last >> first;
for (i = 0; i < 10; i++)
in >> score[i];
average=(score[0]+score[1]+score[2]+score[3]+score[4]+score[5]+score[6]+score[7]+score[8]+score[9])/10;
return (average);
}
It keeps returning the average as 43.90, which is the average of the scores for the next student. I don't know why though. Can somebody help me please?
Here is the input:
Lo Ma 1 2 3 4 5 6 7 8 9 10
George Michelle 12 23 34 54 65 7 98 23 100 23
Frat Brat 12 12 12 12 12 12 12 12 12 12
Bore More 23 4 56 6 34 4 2 6 8 9
The out put is:
Lo Ma 1 2 3 4 5 6 7 8 9 10 43.90 12.00 15.20