Class program using struct and vector

I am writing a program and these are the instructions.
The Student class will contain the following member variables. lastName and firstName (which are strings), and exam1, exam2 and exam2 which are of type double. The Student class will also contain a method to display the student data (all 5 member variables), and a method that returns the exam scores. You can have the exam score method return a single score or all 3 at once--it's up to you. Also, you should include a constructor that allows initializing all the student data when a Student object is created. And, follow the conventions; member variables should be private and values should be returned using public methods.

The Course class should contain a private vector that contains the student objects, and you may want to have an integer counter that keeps track of the number of Students in the Course, depending on how you set things up. You may set up the constructor any way you want.

The Course class should contain 6 public methods that do the following:
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.

There will be a menu in the main() method that allows the user to call any of the above options. This should be similar to programs you have written earlier.
This is what I have written so far and now Im stuck.Can someone give me some guidance on fixing this please.
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
#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{  // this class manages the MileageEntries
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].firstN << " " << entries[i].lastN << endl;

        cout << endl << " Enter the number of the file that you wish to delete: 1 through " << students.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::displayAverageScore() {

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

	for(int i = 0; i < student.size(); i++) {
		totalEx1 += entries[i].getEx1();// accumulate totals
		totalEx2 += entries[i].getEx2();
		totalEx3 += entries[i].getEx3();
		
	}

	if(totalScore != 0) averageScore = totalEx1 + totalEx2 + totalEx3 / totalScore;  // 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].firstName << '\t'
                    << entries[i].lastName << '\t'
                    << entries[i].exam1 << '\t'
                    << entries[i].exam2 << '\t'
                    << entries[i].exam3 << endl;

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



int main() {   // test program

    StudentData entries;  // 
	int option;

	entries.loadDataFromFile();   // load from file; assumes a pre-existing file

	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:
		   mLog.loadDataFromFile();         // add an entry object to the log
		   break;
	    case 2:
		   mLog.addStudent();          // add an entry object to the log
		   break;
	    case 3:
		   mLog.displayStudentData();       // display all records
		   break;
		case 4:
			mLog.deleteStudent();
			break;
	    case 5:
		   mLog.displayAverageScore();    // display average MPG
		   break;
	    case 6:
		   mLog.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;
}
Last edited on
Whats wrong?
When I try to compile it has 20 errors. I was looking for someone to load into their environment and check the errors I don't know why my Visual Studio won't run properly
Topic archived. No new replies allowed.