YingKang 0 Light Poster

The main function looks so messy. I am wondering if there is a way I can move some of the code from the main function to Student.cpp.

Could anybody help please? Thanks.

//main
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include "Student.h"

using namespace std;

int main(int argc, char *argv[])
{
        
              string id;
              string fName;
              string lName;
              int score[SCORE];
              double average;
              char grade;            
              double testAverage[SCORE];
              
             Student student[SIZE];
             int j = 0;
             while(j < SIZE  && !cin.eof())
                  {   
                    cout << "Enter student " << j+1 << " id, enter ctrl-z to quit: ";                   
                    getline(cin, id);   
                    if(!cin.eof())                                      
                    {                  
                       cout << "Enter first name: ";
                       getline(cin, fName);
                       cout << "Enter last name: ";
                       getline(cin, lName);
                      
                       cout << "Enter " << SCORE <<" test score: "; 
                       for (int i = 0; i < SCORE; i++)
                       {
                         cin >> score[i];
                       }                     
                       cin.ignore();                                                
                      }
                      system("cls");
                      student[j].setStudent(id, fName, lName, score);                       
                      j++;                                                                
                   }  
                                                  
                  cout << "ID   Student Name       1       2       3       4       5     Avg       GRD" << endl;    
                  for(int i=0; i < Student :: getNumOfStu()-1; i++)
                  {
                  // student[i].setStudent(id, fName, lName, score);                     
                   student[i].calculate();
                   student[i].print(); 
                                                                 
                    }
                    
                    
                        for(int i = 0; i < SCORE; i++)
                     {
                             double totalInEachTest = 0;
                             
                             for(int j = 0; j < Student :: getNumOfStu(); j++)
                             {
                                  totalInEachTest += student[j].score[i];                   
                             }
                             testAverage[i] = totalInEachTest / Student :: getNumOfStu();
                     }
                   double totalScore = 0;
                   for(int i = 0; i < SCORE; i++)
                     {
                             totalScore += testAverage[i];             
                     }
                    average = totalScore /SCORE;
                    cout << "     " << left << setw(12) << "Average:" ;
                    cout << fixed << noshowpoint << setprecision(0);
                   for (int i = 0; i < SCORE; i++)
                   {
                          cout  << right << setw(8) << testAverage[i] ;
                   }
                    cout << fixed << showpoint << setprecision(1);      
                    cout << setw(8) << average << endl;       
    system("PAUSE");
    return EXIT_SUCCESS;
}
 
//Student.cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

#include "Student.h"

int Student :: numOfStu = 0;

Student :: Student()
{
     id = " ";
     fName = " ";
     lName = " ";
     for(int i = 0; i < SCORE; i++)
     {
             score[i] = 0;
     }  
      //numOfStu++;                             
}
Student :: Student(string idIn, string fNameIn, string lNameIn,
                    int scoreIn[])           
{
        setStudent(idIn, fNameIn, lNameIn, scoreIn); 
        //numOfStu++;                   
}         
          
void Student :: setStudent(string idIn, string fNameIn, string lNameIn,
                    int scoreIn[])
{
     id = idIn;
     fName = fNameIn;
     lName = lNameIn;
     for(int i = 0; i < SCORE; i++)
     {
             score[i] = scoreIn[i];
     }  
    numOfStu++;                               
}             

             
void Student :: calculate()
{
     int totalScore = 0;
    
     for(int i = 0; i < SCORE; i++)
         totalScore += score[i];
     
     average = totalScore / SCORE; 
     
     if(average >= 90)
       grade = 'A';
     else if(average >= 80)
        grade = 'B';
     else if(average >= 70)
        grade = 'C';
     else if(average >= 60)
        grade = 'D';
     else 
        grade = 'F';   
 }
    
int Student :: getNumOfStu()
{
       return numOfStu;
}

         
      
void Student :: print() const
{   
    
    cout <<  left << setw(2) << id << "   " << setw(7) << fName <<  setw(8) << lName
        << right <<  setw(5);
    for(int i = 0; i < SCORE; i++)
         { 
          cout << score[i] << setw(8);
         }    
    cout << fixed << showpoint << setprecision(1);    
         
         cout << average << setw(8) << grade << endl;      
 }
//Student.h
#ifndef STUDENT
#define STUDENT
#include <string>
using namespace std;

const int SIZE = 25, SCORE = 5;
class Student
{
      private:
              string id;
              string fName;
              string lName;             
              double average;
              char grade;
              static int numOfStu;
      public:
             int score[SCORE];
             Student();
             Student(string idIn, string fNameIn, string lNameIn,
                    int scoreIn[]);           
             void setStudent(string idIn, string fNameIn, string lNameIn,
                    int scoreIn[]);
             
             string getID() {return id;}
             string getFName() {return fName;}
             string getLName() {return lName;}
             int getScore() {return score[SCORE];}
             
             void calculate();
             
             static int getNumOfStu();
             
             void print() const;
};
#endif
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.