Need help displaying an average for each entry

I have to display an average for each student that is entered can someone please help with fixing my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void StudentData::displayAverageScore() {

double totalScore = 0;

double totalEx1 = 0, totalEx2 = 0, totalEx3 = 0, averageScore = 0;

for(int i = 0; i < entries.size(); i++) {

totalEx1 += entries[i].getEx1();// accumulate totals

totalEx2 += entries[i].getEx2();

totalEx3 += entries[i].getEx3();

 if(totalScore != 0) averageScore = (totalEx1 + totalEx2 + totalEx3) / 3; // don't divide by 0

 cout << "Average Score For Exams: " << setprecision(1) << fixed << averageScore << endl << endl;

}



}
In this code, totalscore is always 0 therefore the if statemenf will never be executed.

Aceix.
This ^
USe not accumulator, but current entry data: averageScore = (entries[i].getEx1() + /*...*/) / 3;
Thanks for the help getting the average to work , but I still need help getting it to average for each student entered. If the full code would help I can post it
Why not.

Aceix.
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iomanip>
using namespace std;

class Student { // keep this class simple and to a minimum

public:
	Student(string firstN, string lastN, double ex1, double ex2, double ex3);
	string getFirstN();
	string getLastN();
	double getEx1();
	double getEx2();
	double getEx3();
private:
	string firstN;
	string lastN;
	double ex1;
	double ex2;
	double ex3;

};

Student::Student(string fn, string ln, double e1, double e2, double e3) {

	firstN = fn;
	lastN = ln;
	ex1 = e1;
	ex2 = e2;
	ex3 = e3;

}

string Student::getFirstN() {

	return firstN;

}

string Student::getLastN() {

	return lastN;

}

double Student::getEx1() {

	return ex1;

}

double Student::getEx2() {

	return ex2;

}

double Student::getEx3() {

	return ex3;

}

class StudentData {
	public:
		StudentData();
		void loadDataFromFile();
		void addStudent();
		void deleteStudent();
		void displayStudentData();
		void displayAverageScore();
		void saveAndExit();

	private:
		vector<Student> entries;// vector of Student objects
};

StudentData::StudentData() {} // default constructor

	void StudentData::loadDataFromFile() {

	string firstN, lastN;
	double ex1, ex2, ex3;

	ifstream studentData;

	studentData.open("studentData.txt"); // open the file

	if(studentData.is_open()) 
	{

		while( studentData >> firstN >> lastN >> ex1 >> ex2 >> ex3 )
		{ 
		Student student(firstN, lastN, ex1, ex2, ex3); // create aStudent object
		entries.push_back(student);

		}

	}
	cout << entries.size() << " Total students entered "<< endl << endl;
	studentData.close();

}

void StudentData::addStudent() {

	string fn, ln;
	double s1, s2, s3;
	char ans;

		do

	{

		cout << " Enter First Name: ";
		cin >> fn;
		cout << " Enter Last Name: ";
		cin >> ln;
		cout << " Enter Score 1: ";
		cin >> s1;
		cout << " Enter Score 2: ";
		cin >> s2;
		cout << " Enter Score 3: ";
		cin >> s3;

		Student temp(fn,ln,s1,s2,s3);
		entries.push_back(temp);

		cout << " Would you like to enter another record? Y or N : ";
		cin >> ans;

		ans = tolower(ans);
		cout << endl << endl;

		}while(ans == 'y');

}

void StudentData::deleteStudent()

{

int index = 0;

cout << "There are " << entries.size() << " records at this time. " ;

if(entries.size() == 0)

{

cout << " No records can be removed. " << endl << endl;

return;

}

else

{

cout << "The names are: " << endl << endl;

for(int i = 0; i < entries.size(); i++)

cout << i + 1 << '\t' << entries[i].getFirstN() << " " << entries[i].getLastN() << endl;

cout << endl << " Enter the number of the file that you wish to delete: 1 through " << entries.size();

cin >> index;

if(index <= 0 || index > entries.size())

{

cout << " The number you entered is not within the proper range." << endl;

 

}

index = index - 1;

for(int i = index; i < entries.size() - 1; i++)

entries[i] = entries[i+1];

entries.pop_back();

}

cout << " There are now " << entries.size() << " records." << endl;

}

void StudentData::displayStudentData(){
	   cout << " There are " << entries.size() << " at this time. " << endl << endl;
    if(entries.size() > 0)
    {
        cout << " RecNum\tFName\tLName\tScore1\tScore2\tScore3\t" << endl;
        for(int i = 0; i < entries.size(); i++)
            cout << " " <<  (i + 1) << "." << '\t' << entries[i].getFirstN()
                                           << '\t' << entries[i].getLastN()
                                           << '\t' << entries[i].getEx1()
                                           << '\t' << entries[i].getEx2()
                                           << '\t' << entries[i].getEx3() << endl;
    }
    cout << endl;
}



void StudentData::displayAverageScore() {

double totalScore = 0;

double getEx1 = 0, getEx2 = 0, getEx3 = 0, averageScore = 0;

for(int i = 0; i < entries.size(); i++) {

getEx1 += entries[i].getEx1();// accumulate totals

getEx2 += entries[i].getEx2();

getEx3 += entries[i].getEx3();

 if(averageScore = (getEx1 + getEx2 + getEx3) / 3); // don't divide by 0

 cout << "Average Score For Exams: " << setprecision(1) << fixed << averageScore << endl << endl;

}



}

void StudentData::saveAndExit() {

ofstream outFile;

outFile.open("studentData.txt");

if(outFile.is_open())

{

for(int i = 0; i < entries.size(); i++)

outFile << entries[i].getFirstN() << '\t'

<< entries[i].getLastN() << '\t'

<< entries[i].getEx1() << '\t'

<< entries[i].getEx2() << '\t'

<< entries[i].getEx3() << endl;

cout << entries.size() << " The records were saved to studentData.txt" << endl << endl;

}

} 

int main() { // test program

StudentData entries; // 
int option;

do {

cout << "----------Menu Options--------------" << endl;

cout << " 1. Load a students records " << endl;

cout << " 2. Enter students name and scores " << endl;

cout << " 3. Display students information " << endl;

cout << " 4. Remove a students information " << endl;

cout << " 5. Display Exam Averages " << endl;

cout << " 6. Save and Exit " << endl;

cout << " Enter your choice 1, 2, 3, 4 ,5 " << endl;

cin >> option;

 

cin.clear();

cin.ignore(50, '\n');

cout << endl;

switch(option) {

case 1:

entries.loadDataFromFile(); // add an entry object to the log

break;

case 2:

entries.addStudent(); // add an entry object to the log

break;

case 3:

entries.displayStudentData(); // display all records

break;

case 4:

entries.deleteStudent();

break;

case 5:

entries.displayAverageScore(); // display average MPG

break;

case 6:

entries.saveAndExit(); // save to file and exit program

break;

default:

cout << "Please enter 1, 2, 3, 4, 5, or 6 : " << endl << endl;

}

} while(option!= 6);

cout << endl;

return 0;

}
These are the direction
1.a loadStudents method that loads a set of student records from a text file into a Student objects and adds each student object to the course vector. Record structure: lName fName ex1 ex1 ex3, separated by spaces or tabs.
2.an addStudent method to add a Student to the Course vector.
3.a displayStudents method that prints a list of all the students names and scores
4.a removeStudent method that removes a Student from the Course vector.
5.a displayAverages method that displays all of the Student's names and their exam averages.
6.a storeData method that stores each student record to a text file and exits the program
Topic archived. No new replies allowed.