Need help in using vector in class

dhuiwh djwqodj
Last edited on
An actual question and a line number might help.
You are hired to develop a Final Year Project (FYP) management system for a university. The business rules are listed below:
· A student may register a project with a supervisor.
· A project may have up to 2 students and must have 1 supervisor only.

The system must be able to perform the following functions:
1. Create lecturers. [0.5m]
2. Delete lecturers. [1m]
3. Create students. [0.5m]
4. Delete students. [1m]
5. Create project. [0.5m]
6. Delete project. [1m]
7. Register a student to a supervisor for a project. [0.5m]
8. Register project according to the quota. [1m]
9. Unregister a student from a project. [1m]
10. List all lecturers (all attributes, student, and project must be shown) [1m]
11. List all students (all attributes, project, and supervisor must be shown) [1m]
12. List all projects (all attributes, student, and supervisor must be shown) [1m]
13. Read data from file and save data to file. [1m] (You may provide some initial data to facilitate testing during interview.)

Note that the correct way of doing Composition is the attribute itself must be a class. A example for composition is listed below:
class Project {
...
vector<Student*> students; // or "vector<Student> students;" or "Student students[2];",
// but not "vector<int> students;" nor "string students[2];"
Lecturer* supervisor;
...
};


You are strongly advised to complete the assignment with composition but without inheritance and polymorphism, then convert the program to incorporate inheritance and polymorphism.
So what is the problem?
Well my problem i need to use vector in class....As u can see my class is nt using vector at all....I need to noe how to change it... at least one example of above class should help me
y there is no reply?
Because we don't do homework. Not that I mind helping, but you should at the very least be able to understand your task and identify the problem on your own.
y there is no reply?

Patience is a virtue, you are not the only person on this forum that needs helping, not does everyone get a magic message when YOU need help!

Now to your issue:
http://lmgtfy.com/?q=c%2B%2B+vector+tutorial
Just for completeness, the original post was this monstrosity :)



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
class Lecturer
{
	protected:
	string lec_name;
	string lec_id;
	string lec_fac;

	public:
	Lecturer(string lec_name="", string lec_id="",string lec_fac="")
	{
		this->lec_name = lec_name;
		this->lec_id = lec_id;
		this->lec_fac = lec_fac;
	}
	string getLecName() {return lec_name;}
	string getLecId() {return lec_id;}
	string getLecFac() {return lec_fac;}

};

class Student
{
	protected:
	string stud_name;
	string stud_id;
	string stud_fac;

	public:
	Student(string stud_name="", string stud_id="",string stud_fac="")
	{
		this->stud_name = stud_name;
		this->stud_id = stud_id;
		this->stud_fac = stud_fac;
	}
	string getStudName() {return stud_name;}
	string getStudId() {return stud_id;}
	string getStudFac() {return stud_fac;}
};

class Project
{
	protected:
	string proj_name;
	string proj_id;

	public:
	Project(string proj_name="", string proj_id="")
	{
		this->proj_name = proj_name;
		this->proj_id = proj_id;
	}
	string getProjName() {return proj_name;}
	string getProjId() {return proj_id;}
};

vector <Lecturer> lecturer;
vector <Student> student;
vector <Project> project;
string temp1, temp2,temp3;
bool lec_exist=0, stud_exist=0, proj_exist=0, duplicate=0;
int position;

class Registration : public Lecturer, public Student, public Project
{
	public:
	Registration(string lec_name, string lec_fac,string lec_id,string stud_name, string stud_id,string stud_fac,string proj_name,string proj_id)
	: Lecturer(lec_name,lec_id,lec_fac), Student(stud_name,stud_id,stud_fac), Project(proj_name,proj_id) {}
};

vector <Registration> registration;

void Combine(string lecturer_name, string student_name, string project_name);
void ListAll(string input);
void ListOne(string input);
void Add(string input);
void Delete(string input);
void RegistrationFunction(string input);
void Check();
string Upper(string input);

void End()
{
	cout << endl << endl;
	system("pause");
}

void ShowMessage(int msgcode=0)
{
	if (msgcode == 0)
		cout << "\nNULL value is found";
	else if (msgcode == 1)
		cout << endl << "Lecturer " << temp1 << " cannot be found";
	else if (msgcode == 2)
		cout << endl << "Student" << temp2 << " cannot be found";
	else if (msgcode == 3)
		cout << endl << "Project " << temp3 << " cannot be found";
	else if (msgcode == 4)
		cout << endl << "Lecturer " << temp1 << " cannot be found";
	else if (msgcode == 5)
		cout << endl << temp1 << " has been added previously";
	else if (msgcode == 6)
		cout << endl << temp1 << " has registered " << temp2 << " previously";
	else if (msgcode == 7)
		cout << endl << temp1 << " has not register the subject " << temp2 << " previously";
	else if (msgcode == 96)
		cout << endl << temp1 << " has dropped " << temp2 << " successfully";
	else if (msgcode == 97)
		cout << endl << temp1 << " has registered " << temp2 << " successfully";
	else if (msgcode == 98)
		cout << endl << temp1 << " has been removed permanently";
	else if (msgcode == 99)
		cout << endl << temp1 << " has been registered under the faculty " << temp2 << " successfully";

	if (msgcode<50)
		system("color 04");
	else
		system("color 02");
}


void Combine(string x,string y,string z)
{
	int flag=0; string a,b,c,d,e,f,g,h;

	for(int i=0; i<lecturer.size();i++) // Fetch lecturer's faculty
	if(Upper(lecturer[i].getLecName())==Upper(x))
	{
		a = lecturer[i].getLecName();
		b = lecturer[i].getLecId();
		c = lecturer[i].getLecFac();
	}

	for(int i=0; i<student.size();i++) // Fetch student's faculty
	if(Upper(student[i].getStudName())==Upper(y))
	{
		d = student[i].getStudName();
		e = student[i].getStudId();
		f = student[i].getStudFac();
	}

	for(int i=0; i<project.size();i++) // Fetch project's faculty
	if(Upper(project[i].getProjName())==Upper(z))
	{
		g = project[i].getProjName();
		h = project[i].getProjId();
	}

	Registration temp(a,b,c,d,e,f,g,h); // Confirm registration
	registration.push_back(temp);
}


void ListAll(string x)
{
	system("cls");
	vector<Registration>temp = registration;

	if(x=="Lecturers")
		cout << "All lecturers\n\nNo.\tLecturers\tID\t\tFaculty\t\tStudent\t\tProject\n";
	else if(x=="Student")
		cout << "All students\n\nNo.\tStudents\tID\tFaculty\t\tLecturer\t\tProject\n";
    else if(x=="Project")
		cout << "All project\n\nNo.\tProjects\tID\t\t\tLecturer\t\tStudent\n";


	for (int i=0; i<temp.size(); i++)
	{
		bool comma=0;
		if(temp[i].getLecName()=="" && x=="Lecturer") // Make sure lecturer is not empty
		{temp.erase(temp.begin()+i); i--;}
		else if(temp[i].getStudId()=="" && x=="Student") // Make sure lecturer is not empty
		{temp.erase(temp.begin()+i); i--;}
		else if(temp[i].getProjId()=="" && x=="Project") // Make sure lecturer is not empty
		{temp.erase(temp.begin()+i); i--;}
		else
		{
			if(x=="Lecturer") // Publish all students and projects
			{
				cout << i+1 << "\t" <<  temp[i].getLecName() << "\t\t" << temp[i].getLecId() << "\t\t" << temp[i].getLecFac() << "\t\t";

				for (int j=temp.size()-1; j>=0; j--)
				if(Upper(temp[j].getLecName())==Upper(temp[i].getLecName()) && temp[j].getStudName() != "" && temp[j].getProjName() != "")
				{
					if(comma)
					cout << ", ";
					cout << temp[j].getStudName();
					if(i!=j) {temp.erase(temp.begin()+j);}
					comma = 1;
				}
				cout << endl;
			}
			else if(x=="Student") // Publish all students
			{
				cout << i+1 << "\t" <<  temp[i].getStudName() << "\t\t" << temp[i].getStudId()<< "\t\t" << temp[i].getStudFac() << "\t\t";

				for (int j=temp.size()-1; j>=0; j--)
				if(Upper(temp[j].getStudName())==Upper(temp[i].getStudName()) && temp[j].getLecName() != "" && temp[j].getProjName() != "")
				{
					if(comma)
					cout << ", ";
					cout << temp[j].getLecName();
					if(i!=j) {temp.erase(temp.begin()+j);}
					comma = 1;
				}
				cout << endl;
			}
			else if(x=="Project") // Publish all students
			{
				cout << i+1 << "\t" <<  temp[i].getProjName() << "\t\t" << temp[i].getProjId() << "\t\t";

				for (int j=temp.size()-1; j>=0; j--)
				if(Upper(temp[j].getProjName())==Upper(temp[i].getProjName()) && temp[j].getLecName() != "" && temp[j].getStudName() != "")
				{
					if(comma)

					cout << ", ";
					cout << temp[j].getProjName();
					if(i!=j) {temp.erase(temp.begin()+j);}
					comma = 1;

				}
				cout << endl;
			}
		}
	}

	End();
}

void Add(string x)
{
	system("cls");

	if(x=="Lecturer")
		cout << "You are about to add a new lecturer\n\nLecturer Name\t: ";
	else if(x=="Student")
		cout << "You are about to add a new student\n\nSubject Name\t: ";
	else if(x=="Project")
		cout << "You are about to add a new project\n\nProject Name\t: ";
	getline(cin,temp1);
	cout << "ID\t: ";
	getline(cin,temp2);
	cout << "Faculty\t: ";
	getline(cin,temp3);

	Check();

	if(temp1=="" || temp2=="" || temp3=="")
		ShowMessage(0); // Display Message
	else if(lec_exist || stud_exist || proj_exist)
		ShowMessage(5); // Display Message
	else if(x=="Lecturer")
	{
		Lecturer temp(temp1, temp2,temp3);
		lecturer.push_back(temp);
		Combine(temp1,temp2,temp3);
		ShowMessage(99); // Display Message
	}
	else if(x=="Student")
	{
		Student temp(temp1,temp2,temp3);
		student.push_back(temp);
		Combine(temp1,temp2,temp3);
		ShowMessage(99);// Display Message
	}
    else if(x=="Project")
	{
		Project temp(temp1,temp2);
		project.push_back(temp);
		Combine(temp1,temp2,temp3);
		ShowMessage(99); // Display Message
	}
	End();
}

void Delete(string x)
{
	system("cls");
	bool flag=0;

	cout << "You are about to delete a " << x << "\n\n" << x << " Name\t: ";
	getline(cin,temp1);

	for (int i=0; i<registration.size(); i++)
	if ((Upper(registration[i].getLecName())==Upper(temp1) && x=="Lecturer") || (Upper(registration[i].getStudId())==Upper(temp1) && x=="Student"|| (Upper(registration[i].getProjId())==Upper(temp1) && x=="Project")))
	{
		registration.erase(registration.begin()+i);
		flag = 1;
		i--;
	}

	if(temp1=="")
		ShowMessage(0); // Display Message
	else if (flag == 1)
		ShowMessage(98); // Display Message
	else
		ShowMessage(1); // Display Message

	End();
}

Topic archived. No new replies allowed.