Can't read second line from a file

I have a file that look's like this:
last_name first_name grade2 grade2 grade3 grade4 grade5 grade6 grade7 grade8 grade9 grade10
I have to make a output file exactly same except adding the average and the end of each line.
My program only reads the first line, what's wrong with it?

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void file_checking(ifstream& fin, ofstream& fout);
//Check the file is failing to open or not.
void get_grade(ifstream& fin, ofstream& fout);
//Get the value and calculate the aveage.
int main()
{
ifstream fin;
ofstream fout;
file_checking(fin,fout);
get_grade(fin,fout);
return 0;
}

void file_checking(ifstream& fin, ofstream& fout)
{
fin.open("before.txt");
if(fin.fail())
{
cout << "Input file opening fail.\n";
exit(1);
}
fout.open("after.txt");
if(fout.fail())
{
cout << "Output file opening fail.\n";
exit(1);
}
}

void get_grade(ifstream& fin, ofstream& fout)
{
int space,v1;
double ave,value;
char next;
space = 0;
ave = 0;
while(fin.get(next))
{
if(next == ' ')
{
space++;
fout << next;
}
else
{
fout << next;
}
if(space == 2)
{
fin >> v1;
for(int i=0;i<10;i++)
{
fout << v1;
fin >> v1;
fout << ' ';
value = v1;
ave += v1;
}
ave = ave / 10;
fout << ave;
}
else
ave = ave;
}
}


and

input file:
MNSD sjs 20 30 40 50 60 70 85 97 50
abcd efgh 10 20 30 40 50 60 70 80 90 10

out put file:
MNSD sjs 20 30 40 50 60 70 85 97 50 50 58.2
1
2
3
4
5
6
7
8
9
fin >> v1;
for(int i=0;i<10;i++)
{
fout << v1;
fin >> v1;
fout << ' ';
value = v1;
ave += v1;
}


Before the loop, you read in grade1. In the loop, you try to read 10 more numbers. But there are only 10 grades, not 11. You will get an error when you try to read in "abcd" as a number. That stops the outer loop since fin is no longer good.
Topic archived. No new replies allowed.