Designning classes

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//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

I created another function within the main, and moved the loops in the new function. now the main looks neater : )
Topic archived. No new replies allowed.