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

infile >> name only takes in the first name not both first and last because like cin, your infile will stop at a '\n' new line character or a space character.

infile >> name only takes in the first name not both first and last because like cin, your infile will stop at a '\n' new line character or a space character.

wow thanks cant believe i overlooked that.

Any ideas on why its repeating the last person 6 times though?

thanks, new code below

#include<fstream>
#include<iomanip>
#include<string>

using namespace std;


void CalculateAverage(ifstream& inp,ofstream& outp, double& courseAvg);



int main()
{
	int counter;
	string firstname;
	string lastname;
	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 >> lastname >> firstname;
			outfile << left << setw(10) << lastname << firstname;
			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 << setw(10)<< score;

	sum = sum + score;

	}

courseAvg=sum/10;

}

input file

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

Current output

Student Test1 Test2 Test3 Test4 Test5 Test6 Test7 Test8 Test9 Test10 Average 
test_     line_10        20        30        40        50        60        70        80        90        100       55.00     
Price     Betty40        50        60        70        60        50        40        30        60        90        55.00     
Goodman   James60        70        80        90        50        60        90        90        100       90        78.00     
Smith     Charles70        80        90        60        70        60        80        90        90        90        78.00     
Jones     Warren70        70        80        90        70        80        90        80        70        60        76.00     
Jones     Warren0         0         0         0         0         0         0         0         0         0         0.00      
Jones     Warren0         0         0         0         0         0         0         0         0         0         0.00      
Jones     Warren0         0         0         0         0         0         0         0         0         0         0.00      
Jones     Warren0         0         0         0         0         0         0         0         0         0         0.00      
Jones     Warren0         0         0         0         0         0         0         0         0         0         0.00

You are forcing it to write...

for (counter=1;counter<=10;counter++)

A preferred method of reading a file:

while(infile >> lastname >> firstname)
{
     .... 
     .....
     ......  
      etc. etc. etc.
}

So i tried the below and my code snippet is

while(infile >> lastname >> firstname)
{
		
			infile >> lastname >> firstname;
			outfile << left << setw(10) << lastname << firstname << " ";
			CalculateAverage(infile, outfile, courseAvg);
			outfile << setw(10) << courseAvg <<endl;
			
		}

But this isnt working for me. I saw where I went wrong in the counter (must have been thinking about the number of tests not people for some reason)

You are forcing it to write...

for (counter=1;counter<=10;counter++)

A preferred method of reading a file:

while(infile >> lastname >> firstname)
{
     .... 
     .....
     ......  
      etc. etc. etc.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.