I have to write a program that can input up to 20 students name, exam 1 and 2 grades, homework average, and final exam grade. Then it says I have to calculate a final grade using the formula finalGrade=0.20*examOne+0.20*examTwo+0.35*homework +0.25*finalExam; I got the first part of the program to work but no matter what I try the final grade will only output the last student I entered instead of both students. Can anyone help me out, Please?
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; 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*examOne+0.20*examTwo+0.35*homework+0.25*finalExam; 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()<<finalGrade<<endl; } 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; }



LinkBack URL
About LinkBacks



