Hey,
I am trying to write a program that will take the grades from an input file and average them and print them to an output file along with the names and grades.
I know i am missing something simple (but ive been staring at this for 6 hours). Here are my problems...
1. The grades are outputted correctly when i remove the last names from the input file but not when i leave them in. When left in the program outputs all 0 for grades and the same first name every time.
2. The last set of grades is repeated 5 times more than needed.
Code and input below.
Thanks
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
void CalculateAverage(ifstream& inp,ofstream& outp, double& courseAvg);
int main()
{
int counter;
string name;
double classAvg = 0;
double courseAvg;
ifstream infile;
ofstream outfile;
infile.open ("grades.txt"); // Open Input File
outfile.open ("grades_averages.txt"); // Open Output File
outfile<<fixed<<showpoint; // Show Decimal Point
outfile<<setprecision(2); // Set Two Decimals
outfile<<"Student Test1 Test2 Test3 Test4 Test5 Test6 Test7 Test8 Test9 Test10 Average "<<endl; //Header for Columns
for (counter=1;counter<=10;counter++)
{
infile>>name;
outfile<<left<<setw(10)<<name;
CalculateAverage(infile,outfile,courseAvg);
outfile<<setw(10)<<courseAvg<<endl;
}
infile.close(); // Close Input File
outfile.close(); // Close Output File
return 0;
}
void CalculateAverage(ifstream& inp,ofstream& outp,double& courseAvg)
{
int score(0);
int sum(0);
int count(0);
for( count=1; count<=10; count++)
{
inp >> score;
outp << left << left << setw(10)<< score;
sum = sum + score;
}
courseAvg=sum/10;
}
Input File information
test_ line_ 10 20 30 40 50 60 70 80 90 100
Price Betty 40 50 60 70 60 50 40 30 60 90
Goodman James 60 70 80 90 50 60 90 90 100 90
Smith Charles 70 80 90 60 70 60 80 90 90 90
Jones Warren 70 70 80 90 70 80 90 80 70 60