I need to complete a simple program that takes an input file with the following information:

Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 83 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 75 63

and adds a grade average and assigns a letter grade for each student. The program needs to have input/output variables ( i.e. int&). I know that I am missing alot of stuff, but right now, I can't get it to read the input file.....

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

double calculateAverage(ifstream& inFile, double& test1, double& test2, double& test3, double& test4, double& test5);

void main()
{
	ifstream inFile;
	ofstream outFile;

	double test1, test2, test3, test4, test5, courseAvg, avg1;
	int numberoftests;
	string Name;

	inFile.open("tests.txt");

	numberoftests = 0;
	courseAvg = 0.0;
	avg1 = calculateAverage(inFile, test1, test2, test3, test4, test5);

	if (!inFile)
	{
		cout << "Cannot open the input file." << endl;
	}
	else
	{
		outFile.open("finalgrades.txt");
		outFile << fixed << showpoint;
		outFile << setprecision(1);

		outFile << "Name       Test1      Test2      Test3      Test4     Test5"
			<< endl;
		calculateAverage(inFile, test1, test2, test3, test4, test5);
	}
}//End of Main

double calculateAverage(ifstream& inFile, double& test1, double& test2, double& test3, double& test4, double& test5)
{
	double totalscore = 0.0;
	double courseAvg;
	int numberoftests = 0;
	int score;

	inFile >> score;
	while (numberoftests <=5)
	{
		totalscore = totalscore + score;
		numberoftests++;
		inFile >> score;
	} 

	courseAvg = totalscore / numberoftests;

	return score;
} //End of calculateAvg

Any ideas?

This is your input stream:

Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
....

So this is name (string) followed by scores (int-s). Now, looks like first place, where you attempt to read from this stream is line #48:

inFile >> score;

Since score is integer type, what you're trying to do is to put string into int, which can't be successfull. First you need to read name (string), than you can read scores (int).

Edited 7 Years Ago by pecet: n/a

This article has been dead for over six months. Start a new discussion instead.