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;
}
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.