I am trying to get my finalGrade formula to work for all of the students. I have moved it around several places but it doesn't seem to work anywhere. I will post where I have it now and maybe somebody could give me a push in the right direction.
Code:
#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; double finalGrade; 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;}; double getFinalGrade(){return finalGrade;}; }; 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()); 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); } 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; finalGrade=0.20*examOne+0.20*examTwo+0.35*homework+0.25*finalExam; for(i=0; i<gTable.size(); i++) { 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) <<gTable[i].getFinalGrade()<<endl; } for(i=0; i<Grades; i++) { outFile <<gTable[i].getName()<<" " <<gTable[i].getExamOne()<<" " <<gTable[i].getExamTwo()<<" " <<gTable[i].getHomework()<<" " <<gTable[i].getFinalExam()<<" " <<gTable[i].getFinalGrade()<<endl; } outFile.close(); cout << "The file " << filename << " has been successfully written." << endl; cin.ignore();cin.ignore(); return 0; }
Comment