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

The problem is that you calculate the final grade when printing the table, but don't calculate the letter grade until the next loop. At that point the final grade is set to whatever the final grade of the last student was. You need to merge those two loops:

cout<<"Student    Exam 1     Exam 2     Homework     Final Exam     Final     Letter\n";
cout<<"Name       Grade      Grade      Average      Grade          Grade     Grade \n";
cout<<"-------    ------     ------     --------     ----------     -----     ------\n";

for ( i = 0; i < Grades; i++ ) {
  double finalGrade =
      0.20 * gTable[i].getExamOne()
    + 0.20 * gTable[i].getExamTwo()
    + 0.35 * gTable[i].getHomework()
    + 0.25 * gTable[i].getFinalExam();

  cout<< setw(7) << gTable[i].getName()
      << setw(10) << gTable[i].getExamOne()
      << setw(11) << gTable[i].getExamTwo()
      << setw(13) << gTable[i].getHomework()
      << setw(15) << gTable[i].getFinalExam()
      << setw(10) << finalGrade;

  if ( finalGrade > 90 )
    cout<< setw( 11 ) <<"A";   
  else if ( finalGrade > 80 )
    cout<< setw( 11 ) <<"B";   
  else if ( finalGrade > 70 )
    cout<< setw( 11 ) <<"C";    
  else if ( finalGrade > 59 )
    cout<< setw( 11 ) <<"D";   
  else
    cout<< setw( 11 ) <<"F"; 

  cout<<'\n'<<endl;
}

How do I get the finalGrade and letter grade to save to the file when its not part of the vector?

>How do I get the finalGrade and letter grade to
>save to the file when its not part of the vector?
Repeat the previous solution: merge the file output code into the loop that calculates the finalGrade and letter grade.

Without actually naming letter grade how will it know to save it as that?

>Without actually naming letter grade how will it know to save it as that?
Dude, are you really this helpless? When you print the letter grade to cout, print it to outFile too. I completely fail to see how that's not the most obvious solution in the world.

>Without actually naming letter grade how will it know to save it as that?
Dude, are you really this helpless? When you print the letter grade to cout, print it to outFile too. I completely fail to see how that's not the most obvious solution in the world.

Hmmmmm...

... the shallow end of the programmer pool

:icon_question:

I have sat here for hours trying to learn this out of a textbook that really doesn't show much. Believe me, I wish I could just sit here and be done in 10 mins like you probably could.

>Believe me, I wish I could just sit here and be done in 10 mins like you probably could.
I can empathize. Nobody is born knowing how to do this stuff. In fact, programming is very hard, and it takes a lot of dedication to become good at it. But still... ;)

Even though you tell me print it to cout and outfile my book says nothing about that so I sit here and try some things then I google it and see if I can find anything out and if I can't I ask another question. This exercise was due last night at midnight and I have spent hours on it and still am not finished.

>Even though you tell me print it to cout and outfile my book says nothing about
This is what I was thinking of for your table printing loop:

outFile <<gTable[i].getName()<<" "
  <<gTable[i].getExamOne()<<" "
  <<gTable[i].getExamTwo()<<" "
  <<gTable[i].getHomework()<<" "
  <<gTable[i].getFinalExam()<<" ";

if ( finalGrade > 90 ) {
  cout<< setw( 11 ) <<"A";   
  outFile <<"A"<<endl;   
}
else if ( finalGrade > 80 ) {
  cout<< setw( 11 ) <<"B";
  outFile <<"B"<<endl;
}   
else if ( finalGrade > 70 ) {
  cout<< setw( 11 ) <<"C";
  outFile <<"C"<<endl;
}    
else if ( finalGrade > 59 ) {
  cout<< setw( 11 ) <<"D";
  outFile <<"D"<<endl;
}   
else {
  cout<< setw( 11 ) <<"F"; 
  outFile <<"F"<<endl; 
}

Usually it is something easy that I miss. I had all that info in a for loop together before you told me that but I did something wrong and it did not work right. I do appreciate the people on here that take the time to help out. I really am not looking for answers but a boost in the right direction.

This is what my outfile looks like.

A
bruce 100 100 100 100 -9.25596e+061
F
tom 50 50 50 50 -9.25596e+061

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