Need help with struct of vectors

I'm sorry not quite sure how this site works yet so please don't scold me to bad on my post. I am writing a program that will get students names and score average then save on prompt any input would be appreciated. Thanks in advance

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

struct Student {
string lastName;
string firstName;
double exam1;
double exam2;
double exam3;
};

void newRecord(vector<string> &, vector<string> &, vector<string> &, vector<string> &, vector<string> &);
void displayRecord(vector<string> , vector<string> , vector<string> , vector<string> , vector<string>);
void deleteRecord(vector<string> &, vector<string> &, vector<string> &, vector<string> &, vector<string> &);
void scoreAverage(vector<string> &, vector<string> &, vector<string> &, vector<string> &, vector<string> &);
void saveAndExit(vector<string> , vector<string> , vector<string>, vector<string>, vector<string>);

int main()
{
vector<string> firstName, lastName, score1, score2, score3;
int option = 0;

do
{
cout << "----------Menu Options--------------" << endl;
cout << " 1. Enter students name and scores " << endl;
cout << " 2. Display students information " << endl;
cout << " 3. Remove a students information " << endl;
cout << " 4. Display Exam Averages " << endl;
cout << " 5. Save and Exit " << endl;
cout << " Enter your choice 1, 2, 3, 4 ,5 " << endl;
cin >> option;

cout << endl << endl;


switch(option)
{
case 1:
newRecord(firstName, lastName, score1, score2, score3);
break;

case 2:
displayRecord(firstName, lastName, score1, score2, score3);
break;

case 3:
deleteRecord(firstName, lastName, score1, score2, score3);
break;

case 4:
scoreAverage(firstName, lastName, score1, score2, score3);
break;

case 5:
saveAndExit(firstName, lastName, score1, score2, score3);
return 0;
break;

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

}

}while(option != 4);
}

void newRecord(vector<string> &fName, vector<string> &lName, vector<string> &score1, vector<string> &score2, vector<string> &score3)
{
string fn, ln, s1, s2, s3;
char ans = 'n';

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;
fName.push_back(fn);
lName.push_back(ln);
score1.push_back(s1);
score2.push_back(s2);
score3.push_back(s3);

cout << " Would you like to enter another record? Y or N : ";
cin >> ans;
ans = tolower(ans);
cout << endl << endl;
}while(ans == 'y');

}

void displayRecord(vector<string> fName, vector<string> lName, vector<string> score1, vector<string> score2, vector<string> score3)
{

cout << " There are " << fName.size() << " at this time. " << endl << endl;
if(fName.size() > 0)
{
cout << " RecNum" << '\t' << "FName" << '\t' << "LName" << '\t' << "Score1" << '\t' << "Score2" << '\t' << "Score3" << '\t' << endl;
for(int i = 0; i < fName.size(); i++)
{
cout << " " << (i + 1) << "." << '\t' << fName[i] << '\t' << lName[i] << '\t' << score1[i] << '\t' << score2[i] << '\t' << score3[i] << endl;
}
}
cout << endl;
}

void deleteRecord(vector<string> &fName, vector<string> &lName, vector<string> &score1, vector<string> &score2, vector<string> &score3)
{
int index = 0;

cout << "There are " << fName.size() << " records at this time. " ;
if(fName.size() == 0)
{
cout << " No records can be removed. " << endl << endl;
return;
}
else
{
cout << "The names are: " << endl << endl;
for(int i = 0; i < fName.size(); i++)
{
cout << i + 1 << '\t' << fName[i] << " " << lName[i] << endl;
}
cout << endl << " Enter the number of the file that you wish to delete: 1 through " << fName.size();
cin >> index;

if(index <= 0 || index > fName.size())
{
cout << " The number you entered is not within the proper range." << endl;
return;
}

index = index - 1;

for(int i = index; i < fName.size() - 1; i++)
{
fName[i] = fName[i+1];
lName[i] = lName[i+1];
score1[i] = score1[i+1];
score2[i] = score2[i+1];
score3[i] = score3[i+1];
}

fName.pop_back();
lName.pop_back();
score1.pop_back();
score2.pop_back();
score3.pop_back();
}

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

void scoreAverage(vector<string> &fName, vector<string> &lName, vector<string> &score1, vector<string> &score2, vector<string> &score3)
{
(score1 + score2 + score3 = total

void saveAndExit(vector<string> fName, vector<string> lName, vector<string> score1, vector<string> score2, vector<string> score3)
{
ofstream outFile;
outFile.open("studentScores.txt");
if(outFile.is_open())
{
for(int i = 0; i < fName.size(); i++)
{
outFile << fName[i] << '\t' << lName[i] << '\t' << score1[i] << '\t' << score2[i] << '\t'<< score3[i] << endl;
}

cout << fName.size() << " The records were saved to studentScores.txt" << endl << endl;
}
}
Please use code tags
http://www.cplusplus.com/articles/z13hAqkS/

Why is your saveAndExit function inside your scoreAverage function? my compiler wont allow this.

Also this line (score1 + score2 + score3 = total I think you wan't this (score1 + score2 + score3) = total; but total isn't defined anywhere.

also you should close outFile
First of all, what is your problem? Secondly, how do you want to calculate the average where all the scores are strings? What are you trying to do?
In scoreAverage(), you didn't declare total. Since you did score1 + score2 + score3 and they are string, so you are trying to concatenate string not actually calculate the average.
The scoreAverage should call a function so that for each record in the vector a student's exam average is calculated, then the name and exam average for that student is displayed. So if there are 10 students, you should display the 10 names with the proper exam average. After this, the user should be returned to the main menu.

I was just getting frustrated and just stopped writing the code that's why the scoreAverage was not finished. The following are the directions to my program and I am having problems with the whole structure part of the program.

This program will improve on the approach used in program 4. As before we will use student records that have the following fields: last name, first name, exam1 score, exam2 score, and exam3 score.

We will improve on the previous design by abandoning the parallel vectors approach and use a vector of structures. Each structure will hold a student's data in 5 member variables.

You will need to declare a single vector in the main() function that will hold all of the student structures.
I have changed my code some everything works except the average

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

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <math.h>
using namespace std;

struct Student 
{
     string lastName;
     string firstName;
     double exam1;
     double exam2;
     double exam3;
       
 
	Student(string ln, string fn, double s1, double s2, double s3)
	{
		lastName = ln;
		firstName = fn;
		exam1 = s1;
		exam2 = s2;
		exam3 = s3;
	}
};
void newRecord(vector<string> &, vector<string> &, vector<string> &, vector<string> &, vector<string> &);
void displayRecord(vector<string> , vector<string> , vector<string> , vector<string> , vector<string>);
void deleteRecord(vector<string> &, vector<string> &, vector<string> &, vector<string> &, vector<string> &);
void scoreAverage(vector<string> &, vector<string> &, vector<string> &, vector<string> &, vector<string> &);
void saveAndExit(vector<string> , vector<string> , vector<string>, vector<string>, vector<string>);

int main()
{
	vector<string> firstName, lastName, score1, score2, score3;
	int option = 0;

	do
	{
		cout << "----------Menu Options--------------" << endl;
		cout << "  1. Enter students name and scores " << endl;
		cout << "  2. Display students information   " << endl;
		cout << "  3. Remove a students information  " << endl;
		cout << "  4. Display Exam Averages          " << endl;
		cout << "  5. Save and Exit                  " << endl;
		cout << "  Enter your choice 1, 2, 3, 4 ,5   " << endl;
		cin >> option;

		cout << endl << endl;


		switch(option)
		{
		case 1: 
			newRecord(firstName, lastName, score1, score2, score3);
			break;

		case 2:
			displayRecord(firstName, lastName, score1, score2, score3);
			break;

		case 3:
			deleteRecord(firstName, lastName, score1, score2, score3);
			break;

		case 4: 
			scoreAverage(firstName, lastName, score1, score2, score3);
			break;

		case 5:
			saveAndExit(firstName, lastName, score1, score2, score3);
			return 0;
			break;

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

		}

	}while(option != 4);
}

void newRecord(vector<string> &fName,  vector<string> &lName, vector<string> &score1, vector<string> &score2, vector<string> &score3)
{
	string fn, ln, s1, s2, s3;
	char ans = 'n';

	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;
		fName.push_back(fn);
		lName.push_back(ln);
		score1.push_back(s1);
		score2.push_back(s2);
		score3.push_back(s3);

		cout << " Would you like to enter another record? Y or N :   ";
		cin >> ans;
		ans = tolower(ans);
		cout << endl << endl;
	}while(ans ==  'y');

}

void displayRecord(vector<string> fName,  vector<string> lName, vector<string> score1, vector<string> score2, vector<string> score3)
{

	cout << " There are " << fName.size() << " at this time. " << endl << endl;
	if(fName.size() > 0)
	{
		cout << " RecNum" << '\t' << "FName" << '\t' << "LName" << '\t' << "Score1" << '\t' << "Score2" << '\t' << "Score3" << '\t' << endl;
		for(int i = 0; i < fName.size(); i++)
		{
			cout << " " <<  (i + 1) << "." << '\t' << fName[i] << '\t' << lName[i] << '\t' << score1[i] << '\t'  << score2[i] << '\t' << score3[i] << endl;
		}
	}
	cout << endl;
}

void deleteRecord(vector<string> &fName,  vector<string> &lName, vector<string> &score1, vector<string> &score2, vector<string> &score3)
{
	int index = 0;

	cout << "There are " << fName.size() << " records at this time. " ;
	if(fName.size() == 0) 
	{
		cout << " No records can be removed. " << endl << endl;
		return;
	}
	else
	{
		cout << "The names are: " << endl << endl;
		for(int i = 0; i < fName.size(); i++)
		{
			cout << i + 1 << '\t' << fName[i] << " " << lName[i] << endl;
		}
		cout << endl << " Enter the number of the file that you wish to delete: 1 through " << fName.size();
		cin >> index;

		if(index <= 0 || index > fName.size()) 
		{
			cout << " The number you entered is not within the proper range." << endl;
			return;
		}

		index = index - 1;

		for(int i = index; i < fName.size() - 1; i++)
		{
			fName[i] = fName[i+1];
			lName[i] = lName[i+1];
			score1[i] = score1[i+1];
			score2[i] = score2[i+1];
			score3[i] = score3[i+1];
		}

		fName.pop_back();
		lName.pop_back();
		score1.pop_back();
		score2.pop_back();
		score3.pop_back();
	}
	
	cout << " There are now " << fName.size() << " records." << endl;
}

void scoreAverage(vector<string> &fName,  vector<string> &lName, vector<string> &score1, vector<string> &score2, vector<string> &score3)
{
	double total;
	double average;
	average = total / 3;
}

void saveAndExit(vector<string> fName,  vector<string> lName, vector<string> score1, vector<string> score2, vector<string> score3)
{
	ofstream outFile;
	outFile.open("studentScores.txt");
	if(outFile.is_open())
	{
		for(int i = 0; i < fName.size(); i++)
		{
			outFile << fName[i] << '\t' << lName[i] << '\t' << score1[i] << '\t' << score2[i] << '\t'<< score3[i] << endl;
		}

		cout << fName.size() << " The records were saved to studentScores.txt" << endl << endl;
	}
}
Here's the easiest way to complete your void scoreAverage():
1
2
3
4
5
6
7
8
9
void scoreAverage(vector<string> &fName,  vector<string> &lName, vector<string> &score1, vector<string> &score2, vector<string> &score3)
{
	double total = 0;
	for (int i = 0; i < score1.size() && i < score2.size() && i < score3.size())
		total += score1[i] + score2[i] + score3[i];
	double average = total / 3;

	std::cout << average;
}


However, looking at this thread's title, and looking at the fact that you have 5 functions with the same complicated parameter lists tells me that you've got a problem in general.

The idea behind a struct is that you can keep all of the data associated with one person together. You've defined your struct, but you have never used it!

Instead of using 5 parallel vector<string> arrays and trying to keep them organized, make a single vector<Student>. Otherwise it's easy to get into problems. For example, what would happen if score1 is shorter than lName? Since we are only checking the size of lName, we would go out of bounds in score1. Structures help us to avoid problems like this.

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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <math.h>
using namespace std;

struct Student
{
     string lastName;
     string firstName;
     double exam1;
     double exam2;
     double exam3;


	Student(string ln, string fn, double s1, double s2, double s3)
	{
		lastName = ln;
		firstName = fn;
		exam1 = s1;
		exam2 = s2;
		exam3 = s3;
	}
};
void newRecord(vector<Student>& );
void displayRecord(vector<Student>& );
void deleteRecord(vector<Student>& );
void scoreAverage(vector<Student>&);
void saveAndExit(vector<Student>&);

int main()
{
	vector<Student> students;
	int option = 0;

	do
	{
		cout << "----------Menu Options--------------" << endl;
		cout << "  1. Enter students name and scores " << endl;
		cout << "  2. Display students information   " << endl;
		cout << "  3. Remove a students information  " << endl;
		cout << "  4. Display Exam Averages          " << endl;
		cout << "  5. Save and Exit                  " << endl;
		cout << "  Enter your choice 1, 2, 3, 4 ,5   " << endl;
		cin >> option;

		cout << endl << endl;


		switch(option)
		{
		case 1:
			newRecord(students);
			break;

		case 2:
			displayRecord(students);
			break;

		case 3:
			deleteRecord(students);
			break;

		case 4:
			scoreAverage(students);
			break;

		case 5:
			saveAndExit(students);
			return 0;
			break;

		default:
			cout << "Please enter 1, 2, 3, 4, or 5" << endl << endl;
			break;
		}

	}while(option != 4);
}

void newRecord(vector<Student>& students)
{
	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);

		students.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 displayRecord(vector<Student>& students)
{

	cout << " There are " << students.size() << " at this time. " << endl << endl;
	if(students.size() > 0)
	{
		cout << " RecNum" << '\t' << "FName" << '\t' << "LName" << '\t' << "Score1" << '\t' << "Score2" << '\t' << "Score3" << '\t' << endl;
		for(int i = 0; i < students.size(); i++)
		{
			cout << " " <<  (i + 1) << "." << '\t' << students[i].firstName
			                               << '\t' << students[i].lastName
			                               << '\t' << students[i].exam1
			                               << '\t' << students[i].exam2
			                               << '\t' << students[i].exam3 << endl;
		}
	}
	cout << endl;
}

void deleteRecord(vector<Student>& students)
{
	int index = 0;

	cout << "There are " << students.size() << " records at this time. " ;
	if(students.size() == 0)
	{
		cout << " No records can be removed. " << endl << endl;
		return;
	}
	else
	{
		cout << "The names are: " << endl << endl;
		for(int i = 0; i < students.size(); i++)
		{
			cout << i + 1 << '\t' << students[i].firstName << " " << students[i].lastName << endl;
		}
		cout << endl << " Enter the number of the file that you wish to delete: 1 through " << students.size();
		cin >> index;

		if(index <= 0 || index > students.size())
		{
			cout << " The number you entered is not within the proper range." << endl;
			return;
		}

		index = index - 1;

		for(int i = index; i < students.size() - 1; i++)
		{
			students[i] = students[i+1];
		}

		students.pop_back();
	}

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

void scoreAverage(vector<Student> &students)
{
	double total = 0;
	for (int i = 0; i < students.size(); i++)
	{
	    total += students[i].exam1 + students[i].exam2 + students[i].exam3;
	}
	double average = total / ( 3.0 * students.size() );

	cout << average << endl;
}

void saveAndExit(vector<Student>& students)
{
	ofstream outFile;
	outFile.open("studentScores.txt");
	if(outFile.is_open())
	{
		for(int i = 0; i < students.size(); i++)
		{
			outFile << students[i].firstName << '\t' << students[i].lastName << '\t' << students[i].exam1 << '\t' << students[i].exam2 << '\t'<< students[i].exam3 << endl;
		}

		cout << students.size() << " The records were saved to studentScores.txt" << endl << endl;
	}
}

And here it is with some beautifying:
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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

struct Student
{
     string lastName;
     string firstName;
     double exam1;
     double exam2;
     double exam3;


    Student(string ln, string fn, double s1, double s2, double s3)
    {
        lastName = ln;
        firstName = fn;
        exam1 = s1;
        exam2 = s2;
        exam3 = s3;
    }
};
void newRecord(vector<Student>& );
void displayRecord(vector<Student>& );
void deleteRecord(vector<Student>& );
void scoreAverage(vector<Student>&);
void saveAndExit(vector<Student>&);

int main()
{
    vector<Student> students;
    int option = 0;

    do
    {
        cout << "----------Menu Options--------------" << endl;
        cout << "  1. Enter students name and scores " << endl;
        cout << "  2. Display students information   " << endl;
        cout << "  3. Remove a students information  " << endl;
        cout << "  4. Display Exam Averages          " << endl;
        cout << "  5. Save and Exit                  " << endl;
        cout << "  Enter your choice 1, 2, 3, 4 ,5   " << endl;
        cin >> option;

        cout << endl << endl;


        switch(option)
        {
        case 1:
            newRecord(students);
            break;

        case 2:
            displayRecord(students);
            break;

        case 3:
            deleteRecord(students);
            break;

        case 4:
            scoreAverage(students);
            break;

        case 5:
            saveAndExit(students);
            return 0;

        default:
            cout << "Please enter 1, 2, 3, 4, or 5" << endl << endl;
            break;
        }
    }while(option != 4);
}

void newRecord(vector<Student>& students)
{
    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);

        students.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 displayRecord(vector<Student>& students)
{

    cout << " There are " << students.size() << " at this time. " << endl << endl;
    if(students.size() > 0)
    {
        cout << " RecNum\tFName\tLName\tScore1\tScore2\tScore3\t" << endl;
        for(int i = 0; i < students.size(); i++)
            cout << " " <<  (i + 1) << "." << '\t' << students[i].firstName
                                           << '\t' << students[i].lastName
                                           << '\t' << students[i].exam1
                                           << '\t' << students[i].exam2
                                           << '\t' << students[i].exam3 << endl;
    }
    cout << endl;
}

void deleteRecord(vector<Student>& students)
{
    int index = 0;

    cout << "There are " << students.size() << " records at this time. " ;
    if(students.size() == 0)
    {
        cout << " No records can be removed. " << endl << endl;
        return;
    }
    else
    {
        cout << "The names are: " << endl << endl;
        for(int i = 0; i < students.size(); i++)
            cout << i + 1 << '\t' << students[i].firstName << " " << students[i].lastName << endl;

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

        if(index <= 0 || index > students.size())
        {
            cout << " The number you entered is not within the proper range." << endl;
            return;
        }

        index = index - 1;

        for(int i = index; i < students.size() - 1; i++)
            students[i] = students[i+1];

        students.pop_back();
    }

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

void scoreAverage(vector<Student> &students)
{
    double total = 0;
    for (int i = 0; i < students.size(); i++)
        total += (students[i].exam1 + students[i].exam2 + students[i].exam3)/3.0;

    cout << total / students.size() << endl;
}

void saveAndExit(vector<Student>& students)
{
    ofstream outFile;
    outFile.open("studentScores.txt");
    if(outFile.is_open())
    {
        for(int i = 0; i < students.size(); i++)
            outFile << students[i].firstName << '\t'
                    << students[i].lastName << '\t'
                    << students[i].exam1 << '\t'
                    << students[i].exam2 << '\t'
                    << students[i].exam3 << endl;

        cout << students.size() << " The records were saved to studentScores.txt" << endl << endl;
    }
}
Last edited on
Then change:
1
2
3
4
5
6
7
8
void scoreAverage(vector<Student> &students)
{
    double total = 0;
    for (int i = 0; i < students.size(); i++)
        total += (students[i].exam1 + students[i].exam2 + students[i].exam3)/3.0;

    cout << total / students.size() << endl;
}

to
1
2
3
4
5
6
7
8
9
10
11
12
13
void scoreAverage(vector<Student> &students)
{
    double total = 0;
    for (int i = 0; i < students.size(); i++)
    {
        double personalAverage = (students[i].exam1 + students[i].exam2 + students[i].exam3) / 3.0;
        total += personalAverage;
        
        cout << students[i].firstName << '\t' << personalAverage << endl;
    }

    cout << "Class average: " << total / students.size()  << endl;
}
Last edited on
Thanks guys!! The average part is still not working properly because it is suppose to average and display a averaged score for each name entered. Can someone show me how to do that part.. My instructions read as followed
Option 4 should call a function so that for each record in the vector a student's exam average is calculated, then the name and exam average for that student is displayed. So if there are 10 students, you should display the 10 names with the proper exam average. After this, the user should be returned to the main menu.
Topic archived. No new replies allowed.