I need to assign each student a letter grade. The way I have it set up now it assigns the letter grade of the second student to both students. How can I get it to assign each student their own letter grade? Also, I would rather output the letter grade to the table I have set up. How can I do that? Any help is appreciated!
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
class Grade
{
private:
string name;
int examOne, examTwo, homework, finalExam;
public:
Grade(){setGrade("Tom", 94, 92, 87 ,85);};
Grade(string nm, int exOne, int exTwo, int hw, int final){setGrade(nm, exOne, exTwo, hw, final);};
void setGrade(string nm, int exOne, int exTwo, int hw, int final){name=nm; examOne=exOne; examTwo=exTwo; homework=hw; finalExam=final;};
string getName(){return name;};
int getExamOne(){return examOne;};
int getExamTwo(){return examTwo;};
int getHomework(){return homework;};
int getFinalExam(){return finalExam;};
};
int _tmain(int argc, _TCHAR* argv[])
{
const int Grades = 2;
string filename = "grades.dat";
string name;
ofstream outFile;
int i, examOne, examTwo, homework, finalExam;
double finalGrade;
Grade g;
vector<Grade> gTable;
outFile.open(filename.c_str());
for(i=0; i<Grades; i++)
{
cout <<"\nEnter students name, 1st exam grade, 2nd exam grade, homework average,"
<< "and final exam grade (type done to exit): \n";
cin >>name>>examOne>>examTwo>>homework>>finalExam;
if(name=="done")
break;
g=Grade(name, examOne, examTwo, homework, finalExam);
gTable.push_back(g);
}
cout << fixed << setprecision(2) << endl;
cout << "Student Exam 1 Exam 2 Homework Final Exam Final Letter"<<endl;
cout << "Name Grade Grade Average Grade Grade Grade"<<endl;
cout << "------- ------ ------ -------- ---------- ----- -----"<<endl;
for(i=0; i<Grades; i++)
{
finalGrade=0.20*gTable[i].getExamOne()+0.20*gTable[i].getExamTwo()+0.35*gTable[i].getHomework()+0.25*gTable[i].getFinalExam();
cout <<setw(8)<<gTable[i].getName()<<" "<<setw(6)<<gTable[i].getExamOne()<<" "
<<setw(6)<<gTable[i].getExamTwo()<<" "<<setw(8)<<gTable[i].getHomework()
<<" "<<setw(6)<<gTable[i].getFinalExam()<<" "<<setw(5)
<<finalGrade<<endl<<endl;;
}
for(i=0; i<gTable.size(); i++)
{
if (finalGrade>90)
cout<<gTable[i].getName()<<"'s final grade is an A.\n";
else if (finalGrade>80)
cout<<gTable[i].getName()<<"'s final grade is a B.\n";
else if (finalGrade>70)
cout<<gTable[i].getName()<<"'s final grade is a C.\n";
else if (finalGrade>59)
cout<<gTable[i].getName()<<"'s final grade is a D.\n";
else
cout<<gTable[i].getName()<<"'s final grade is a F.\n";
}
for(i=0; i<Grades; i++)
{
outFile <<gTable[i].getName()<<" "
<<gTable[i].getExamOne()<<" "
<<gTable[i].getExamTwo()<<" "
<<gTable[i].getHomework()<<" "
<<gTable[i].getFinalExam()<<endl;
}
outFile.close();
cout << "The file " << filename
<< " has been successfully written." << endl;
cin.ignore();cin.ignore();
return 0;
}