Moezzie: Yes Its reading all the data from a file. And inputing it into another so it will read from this file:
CODE
0.35 0.25 0.40
100 76 88 1014
0.35 0.25 0.40
100 100 25 9999
0.35 0.25 0.40
60 60 70 95 1337
and the output will look like this(in a file and the program window):
CODE
Student ID: 1014
Test Scores: 100 76 88
Weighted Average: 89.2
Grade: B
Student ID: 9999
Test Scores: 100 100 25
Weighted Average: 70
Grade: C
Student ID: 95
Test Scores: 60 60 70
Weighted Average: 64
Grade: D
salman: thank you for posting some code, but i cant seem to find your changes?
also I forgot to put in my posted code that I am trying to post the average outside of the loop. So where the } is the end of the loop. Then I would display the averageWeight in a cout.
CODE
}
averageWeight = sumAverage / totalStudents;
Ill just post the full code:
CODE
#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
using namespace std;
#define inFile "c:InFile.txt"
#define outFile "c:outFile.txt"
int main()
{
ifstream ins;
ofstream outs;
float weight1, weight2, weight3, score1, score2, score3, studentID, weightedAvg;
float lowScore = 100, highScore = 0, avgWeightAvg = 0, totalStudents = 0, sumAverage, averageWeight;
string studentGrade;
ins.open(inFile);
if (ins.fail())
{
cerr << "Error opening " << inFile << endl;
return EXIT_FAILURE;
}
outs.open(outFile);
if (outs.fail())
{
cerr << "Error opening " << inFile << endl;
return EXIT_FAILURE;
}
ins >> weight1 >> weight2 >> weight3 >> score1 >> score2 >> score3 >> studentID;
while (!ins.eof())
{
//////////////////////////////////////////////////////////
weightedAvg = (weight1 * score1) + (weight2 * score2) + (weight3 * score3);
//////////////////////////////////////////////////////////
if (weightedAvg >= 90)
studentGrade = "A";
if (weightedAvg >= 80 && weightedAvg < 90)
studentGrade = "B";
if (weightedAvg >= 70 && weightedAvg < 80)
studentGrade = "C";
if (weightedAvg >= 60 && weightedAvg < 70)
studentGrade = "D";
if (weightedAvg >= 50 && weightedAvg < 60)
studentGrade = "F";
if (weightedAvg < 50)
studentGrade = "F";
//////////////////////////////////////////////////////////////
if (weightedAvg <= lowScore)
lowScore = weightedAvg;
if (weightedAvg >= highScore)
highScore = weightedAvg;
//////////////////////////////////////////////////////////////
totalStudents++;
sumAverage += weightedAvg;
// averageWeight = sumAverage / totalStudents;
///////////////////////////////////////////////////////////////
outs << "Student ID: "<< studentID << endl << "Test Scores: " << score1 << " "
<< score2 << " " << score3 << endl << "Weighted Average: " << weightedAvg << endl << "Grade: " << studentGrade << endl << endl;
cout << "Student ID: "<< studentID << endl << "Test Scores: " << score1 << " "
<< score2 << " " << score3 << endl << "Weighted Average: " << weightedAvg << endl << "Grade: " << studentGrade << endl << endl;
ins >> weight1 >> weight2 >> weight3 >> score1 >> score2 >> score3 >> studentID;
}
averageWeight = sumAverage / totalStudents;
cout << "Your results have been written to: " << outFile << endl;
cout << "Low score: " << lowScore << " HighScore: " << highScore << endl;
cout << "Total Students: " << totalStudents << " Averages of the weights: " << avgWeightAvg << endl;
ins.close();
outs.close();
return 0;
}
I put a bunch of ////////// in there to break up the code. I like to make sure everything works in main before I split it up into functions..