How can I get my finalGrade formula to work for all students?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gator6688
    New Member
    • Sep 2007
    • 63

    How can I get my finalGrade formula to work for all students?

    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;
    }
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    why don't you try moving your final grade forumla into your final grade method?

    Comment

    • gator6688
      New Member
      • Sep 2007
      • 63

      #3
      I have tried moving it all kinds of places but it does not work anywhere I put it.

      Comment

      • Cucumber
        New Member
        • Sep 2007
        • 90

        #4
        Just put the formula inside setGrade() dude.
        Im talking about

        finalGrade=0.20 *examOne+0.20*e xamTwo+0.35*hom ework +0.25*finalExam

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          Originally posted by Cucumber
          Just put the formula inside setGrade() dude.
          Im talking about

          finalGrade=0.20 *examOne+0.20*e xamTwo+0.35*hom ework +0.25*finalExam
          From the looks of your code it doesn't seem like you understand how main methods and classes work.

          In object oriented programming we try to design software so that individual software parts only need to know about themelseves or what interface they present to the world.

          Your class does nothing but keep state of a few things, all the calculations are done in your main method, which doesn't make any sense. You main method is there to drive your class, give you a way to get input from the user and to call classes.

          you main method should consist of...

          [CODE=cpp]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(fi lename.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>>home work>>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<<e ndl;

          g=Grade(name, examOne, examTwo, homework, finalExam);
          gTable.push_bac k(g);
          }//end for

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

          //File junk here
          ...
          //
          cin.ignore();ci n.ignore();

          return 0;[/CODE]

          Now your main method only uses the class it does not have to have any knowledge of how it works or what it does to calculate stuff. Now you need to figure out how to make all your getters and setter function properly.
          Last edited by RedSon; Nov 7 '07, 03:51 PM. Reason: Adding code

          Comment

          Working...