Everything is working on this program except for the output to the table at the end. It will only display the second student I enter but not the first.
#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;
Grade g;
vector<Grade> gTable;
outFile.open(filename.c_str());
if(outFile.fail())
{
cout << "The file was not successfully opened."<<endl;
exit(1);
}
outFile << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);
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;
cout <<"\nFor the student the following data has been written to the file: \n";
cout <<name<<" "<<examOne<<" "<<examTwo<<" "<<homework<<" "<<finalExam<<endl;
g=Grade(name, examOne, examTwo, homework, finalExam);
gTable.push_back(g);
}
for(i=0; i<Grades; i++)
{
outFile <<gTable[i].getName()<<" "
<<gTable[i].getExamOne()<<" "
<<gTable[i].getExamTwo()<<" "
<<gTable[i].getHomework()<<" "
<<gTable[i].getFinalExam()<<endl;
}
cout << fixed << setprecision(2) << endl;
cout << "Student Exam 1 Exam 2 Homework Final Exam"<<endl;
cout << "Name Grade Grade Average Grade"<<endl;
cout << "------- ------ ------ -------- ----------"<<endl;
for(i=0; i<gTable.size(); i++)
{
cout <<setw(8)<<name<<" "<<setw(6)<<examOne<<" "<<setw(6)<<examTwo<<" "<<
setw(8)<<homework<<" "<<setw(6)<<finalExam<<endl;
}
outFile.close();
cout << "The file " << filename
<< " has been successfully written." << endl;
cin.ignore();cin.ignore();
return 0;
}