Tech Support banner

Status
Not open for further replies.
1 - 2 of 2 Posts

·
Registered
Joined
·
5 Posts
Discussion Starter #1
hi guys... well basically this is the code i have: It retreives information from a text file>.... this txt file looks like:

Balto 85 83 77 91 76 -999
Mickey 80 90 95 93 48 -999
Minnie 78 81 11 90 73 -999
Doc 92 83 30 69 87 -999
Goofy 23 45 96 38 59 -999
Duckey 60 85 45 39 67 -999
Grumpy 27 31 52 74 83 -999
Sunny 93 94 89 77 97 -999
Piggy 79 85 28 93 82 -999
Pluto 85 72 49 75 63 -999

Now if you run the following code... it displays everything like i would like it to...but im having trouble displaying the "grades" column... for example... if the "average" for a student is more then 90...he would ge an A.. if its between 80-90 - B and so on... i just cannot get to display it... i need to use a function (calculateGrade) and it needs to return the grade.. i also cannot use global variables. :( please if someone could help>...

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

void calculateAverage(ifstream& inp, int score1, int score2, int score3, int score4, int score5, double& courseAvg, double& classAvg);
//char calculateGrade(char grade, double avg2);

int main()
{
	cout << setprecision(1);
	cout << fixed << showpoint;

	int score1;
	int score2;
	int score3;
	int score4;
	int score5;
	string name;
	//double grade;
	int numberOfStudents;
	double avg;
	char grade;
	char final;
    double avg2;
	int sum = 0;
	double classAvg;


	
	

	ifstream infile;
	
	infile.open("C:\\Documents and Settings\\Magas\\Desktop\\SCHOOL\\C++Assign2\\TestScores.txt");
	

	if(!infile)
	{
		cout << "Cannot open the input file." << endl;
		cout << "Program will terminate!" << endl;
		return 0;
	}

	numberOfStudents = 0;

	cout << "Student" << "   " << "Test1" << "   " << "Test2" << "   " << "Test3" << "   " << "Test4" << "   " << "Test5" << "   " << "Average" << "   " << "Grade";
	cout << endl;

	infile >> name >> score1 >> score2 >> score3 >> score4 >> score5;
	
	while(infile)
	{

		//Calls the "calculateAverage" function to calculate the averages
		calculateAverage(infile, score1, score2, score3, score4, score5, avg, classAvg);

		//Calls the "calculateGrade" function to calculate the final letter grades
		//calculateGrade(final, avg);

		cout << setfill(' ') << left << setw(12) << name << setw(8) << score1 << setw(8) << score2 << setw(8) << score3 << setw(8) << score4 << setw(8) << score5 << setw(8) << avg/5 << setw(8) << final;
		
		cout << endl;
		

		infile >> name >> score1 >> score2 >> score3 >> score4 >> score5;


		}
	
	cout << endl << endl;
	cout << "Class Average = " << (classAvg/5)/10 << endl << endl;
}



void calculateAverage(ifstream& inp, int score1, int score2, int score3, int score4, int score5, double& courseAvg, double& classAvg)
{
	int score;
	inp >> score;
	while(score != -999)
	{	
		inp >> score;
		
	}

	courseAvg = (score1 + score2 + score3 + score4 + score5);
	classAvg = (classAvg + courseAvg);
	
}
 
1 - 2 of 2 Posts
Status
Not open for further replies.
Top