Need help with getting file output to console

My project is to create a file that calculates the average of a students grade from a file. I have the correct file output, but I am also supposed to get an output on the console.

Sample Output: Console (given provided input file)
Damian Hess 6.5
Andrew Nielsen 6.2
Alfred Yankovic 6.9
Inga Scharf 7.6

Sample Output: “lab08_out.txt” (given provided input file)
Hess Damian 5 10 7 5 9 2 8 5 6 8 6.5
Nielsen Andrew 7 9 8 7 4 7 3 4 9 4 6.2
Yankovic Alfred 6 5 8 10 9 6 5 7 3 10 6.9
Scharf Inga 9 2 7 10 7 8 8 10 6 9 7.6

I am not sure how to do this. I tried putting cout <<firstname; places but it only puts out one name(Igna.



#include <iostream>
#include <fstream>
using namespace std;
void calcAvg(ifstream& in, ofstream& out);
int main()

{
ifstream in;
ofstream out;
in.open("lab08_in.txt");
if (in.fail())
{
cout << "File could not be found";
return 1;
}

out.open("lab08_out.txt");
calcAvg(in,out);
out.close();
in.close();

return 0;
}
void calcAvg(ifstream& in, ofstream& out)

{
int i,sum,a[10];
string firstname,lastname;
double average;
in>>firstname;
while(in)
{sum=0;
in>>lastname;
for(i=0;i<10;i++)
{in>>a[i];
sum+=a[i];
}
out<<firstname<<" "<<lastname<<" ";
for(i=0;i<10;i++)
out<<a[i]<<" ";
average=sum/10.0;
out<<average<<endl;
in>>firstname;
}

}

@doodle123

You have one array of 10, which will hold only one students grades, which, when the program finishes, will be the last student, 'Inga'. You have one string variable for first name, and one for last. Again, when program finishes, will contain the last student, "Inga Scharf'.
Topic archived. No new replies allowed.