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;
}

>It will only display the second student I enter but not the first.
It will only ever display the last record entered. You're using the temporary variables to print your table instead of the contents of the vector.

>How do I use the contents of the vector?
I highly recommend engaging your brain before shooting off a question. You'll seem much more intelligent that way. How do you use the contents of the vector? Look at the code (code you supposedly wrote) that prints the contents of the vector to outFile.

Who else would have wrote it? By the way I figured it out before you answered the question!

>Who else would have wrote it?
If you don't know how to do something that already exists in the code, it suggests you didn't write it completely.

>By the way I figured it out before you answered the question!
Good for you. Want a cookie?

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