what's the problem

can someone tell me why this code doesnt work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void room::emptyMB3()
{
	for(int i = 0; i < MB3.size(); i++)
	{
		MB3[i]->setstatus("UNASSIGNED");
	}
	for(int i = 0; i < students.size(); i++)
	{
		if(students[i] == MB3[i])
			students[i]->setstatus("UNASSIGNED");	
			noRoom.push_back(students[i]);
	}
	MB3.erase(MB3.begin(),MB3.end());
}


the problem here is noRoom.push_back(students[i]); doesnt work, it pushes back for the MB3.size()-1 instead of for students.size().

for example say my MB3.size() = 3 and my students.size() = 10

it will only push back students[i] when it is within MB3.size()-1 which is 2.

so if students[1] == MB3[1];
or students[2] == MB3[2];

it pushes back

but if it's 3 and above, it wont. i dont get why this happens. the setstatus seems fine so i dont get why push_back is not.
You don't tell what all your variables are.

Line 11 guessing from your indentation is not included in if. I guess that an error. Use brackets for this.
SO basically for your code you push_back for every element of students.
variables are vector<class student:int ID, string name, char gender, string status>MB3

i tried it using brackets as well and the result is the same.
i made a mini version of the code for you guys to try out. to check for elements in noRoom go to "list students" then "list students without rooms". for students go to "list all students". try creating 4-5 students then assign student 1 and 3 to block A1. after that check the elements of noRoom and students. then empty block A1 and check again. the problem will present itself then.

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
#include <iostream>
#include <vector>
#include <cstdlib>
#include <set>
#include <algorithm>
using namespace std;
class room;
class student;
class menu
{
	public:
	void mainmenu();
	void MAmenu();
	void MAdelmenu();
	void roomGo();
	void listSmenu();
};
class functions:public menu
{
	public:
	void add(room& r1, student*& s1, char choice);
	void addAB();
	void emptyAB();
	void addMA(char option,room& r1,student*& s1,char choice,int target,int num);
	void emptyMA(char option,room& r1,student*& s1,char choice,int target,int num);
	void addMA1(room& r1, student*& s1, char choice, int& target, int& num);
};
class student
{
private:
	string name, dummy, status;
	char gender;
	int ID;	
public:
	student() : name("UNKNOWN"), ID(0), gender('-'), status("UNASSIGNED"){}
	student (string Name, int ID, char Gender)
	{
		this->name = Name;
		this->ID = ID;
		this->gender = Gender;
	}
	student(const student& s): name(s.name), ID(s.ID), gender(s.gender){}
	void setstudent();
	void getstudent(string name, int ID, char gender, string status);
	void setstatus(string status) {this->status = status;}
	void print(int i) const;
	int getid(){return ID;}
};
void student::getstudent(string name, int ID, char gender, string status)
{
	this->name = name;
	this->ID = ID;
	this->gender = gender;
	this->status = status;
}
void student::setstudent()
{
	cout << "Enter student's ID: ";
	cin >> ID;
	cout << "Enter student's gender(M/F): ";
	cin >> gender;
	gender = toupper(gender);
	getline(cin, dummy);
	cout << "Enter student's name: ";
	getline(cin, name);
	cout << endl;
}
void student::print(int i) const
{
	cout << i+1 << ". ID: " << ID << " | NAME: " << name << 
	" l SEX: " << gender << " l ROOM: " << status << endl;
}
class room
{
private:
	vector<student*> students;
	vector<student*> noRoom;
	vector<student*> MA1;
public:
	void addstudent(student* s);
	void addnoRoom(student* s);
	void addMA1(int target);
	void emptyMA1();
	void printALL();
	void printnoRoom();
	void printMA1();
	void printRoom();
	int studentsize(){return students.size();}
	int noRoomsize() {return noRoom.size();}
	int roomMA1size(){return MA1.size();}
};
void room::addstudent(student* s)
{
	students.push_back(s);
}

void room::addnoRoom(student* s)
{
	noRoom.push_back(s);
}
void room::addMA1(int target)
{
	MA1.push_back(noRoom[target]);
	noRoom[target]->setstatus("MA1");
	for(int i = 0; i < students.size(); i++)
	{
		if(noRoom[target] == students[i])
			students[i]->setstatus("MA1");
	}
	noRoom.erase(noRoom.begin()+target);
}
void room::emptyMA1()
{
	for(int i = 0; i < MA1.size(); i++)
		MA1[i]->setstatus("UNASSIGNED");
	for(int i = 0; i < students.size(); i++)
	{
		if(students[i] == MA1[i])
		{
			students[i]->setstatus("UNASSIGNED");
			noRoom.push_back(students[i]);
		}
	}
	MA1.erase(MA1.begin(),MA1.end());
}
void room::printnoRoom()
{
	for(int i = 0; i < noRoom.size(); i++)
		noRoom[i]->print(i);
}

void room::printALL()
{	
	for(int i = 0; i < students.size(); i++)
		students[i]->print(i);
}
void room::printMA1()
{
	for(int i = 0; i < MA1.size(); i++)
		MA1[i]->print(i);
}
void room::printRoom()
{
	printMA1();
}
void functions::addMA(char option,room& r1,student*& s1,char choice,int target,int num)
{
	cout << "Choose option(1-10): ";
	cin >> option;
	option = toupper(option);
	switch(option)
	{
		case'1': addMA1(r1,s1,choice,target,num);
				 MAmenu();
				 addMA(option,r1,s1,choice,target,num);
				 break;
		case'9': roomGo();
		case'0': break;
	}
}
void functions::emptyMA(char option,room& r1,student*& s1,char choice,int target,int num)
{
	cout << "Choose Option(1-10): ";
	cin >> option;
	option = toupper(option);
	switch(option)
	{
		case'1': r1.emptyMA1();
				 MAmenu();
				 break;
		case'9': roomGo();
		case'0': break;
	}
}
room r1;
	menu m;
	int i = 0;
	functions f;
	student* s1 = NULL;
	int target;
	int num;
	bool done = false;
	char choice, option;
int main()
{do
	{
		m.mainmenu();
		cout << "Select option (1-6): ";
		cin >> option;
		option = toupper(option);
		switch (option)
		{
			case'1': f.add(r1,s1,choice);
						break;
			case'3': m.roomGo();
					 f.addAB();
						break;			
			case'5': m.roomGo();
					 f.emptyAB();
						break;		
			case'6': m.listSmenu();
						break;
			case'8': done = true;
						break;
		}
	}
	while(!done);
	return 0;
}
void menu::mainmenu()
{
cout <<"|[1] Add student                            |" << endl <<
	"|[3] Assign rooms                           |" << endl <<
	"|[5] Empty rooms                            |" << endl <<
	"|[6] List students                          |" << endl;
}
void menu::listSmenu()
{
	system("cls");
	cout << "*====================================*" << endl <<
	"|@Students Log                       |" << endl <<
	"|------------------------------------|" << endl <<
	"|[1] List All students               |" << endl <<
	"|[2] List students with rooms        |" << endl <<
	"|[3] List students without rooms     |" << endl <<
	"|[4] back to main                    |" << endl <<
	"*=====================================+" << endl << endl;
	cout << "Choose Option (1-4): ";;
	cin >> option;
	option = toupper(option);
	switch(option)
	{
		case'1': r1.printALL();
				 system("pause");
				 listSmenu();
				 break;
		case'2': r1.printRoom();
				 system("pause");
				 listSmenu();
				 break;				 
		case'3': r1.printnoRoom();
				 system("pause");
				 listSmenu();
				 break;				 
		case'4': break;
	}
}
void menu::MAmenu()
{
	system("cls");
    cout << "|[1] A1 (" << 2-r1.roomMA1size() <<")|" << endl << endl;
}
void menu::MAdelmenu()
{
	system("cls");
    cout << "|[1] A1 (" << r1.roomMA1size() <<")|" << endl << endl;
}
void menu::roomGo()
{
	system("cls");
	cout << "*====================*" << endl <<
	"|@MALE HOSTELS(M)    |" << endl <<
	"|--------------------|" << endl <<
	"|[1] Block A         |" << endl << endl;
}
void functions::add(room& r1, student*& s1, char choice)
{
	cout << "Add student? (Y/N):";
	cin >> choice;
	while (toupper(choice) == 'Y')
	{
		s1 = new student;		
		s1->setstudent();
		r1.addstudent(s1);
		r1.addnoRoom(s1);
		cout << "add more students?(Y/N): ";
		cin >> choice;
	}
	cout << endl;
	system("cls");
	r1.printALL();
	system("pause");
}
void functions::addAB()
{
	cout << "Choose Hostel Block(1-2: Male, 3-4: Female): ";
	cin >> option;
	option = toupper(option);
	switch(option)
	{
		case'1': MAmenu();
				 f.addMA(option,r1,s1,choice,target,num);
				 break; 
		case'5': break;
	}
}
void functions::emptyAB()
{
	cout << "Choose Hostel Block(1-2: Male, 3-4: Female): ";
	cin >> option;
	option = toupper(option);
	switch(option)
	{
		case'1': MAmenu();
				 f.emptyMA(option,r1,s1,choice,target,num);
				 break;				 
		case'5': break;
	}
}
void functions::addMA1(room& r1, student*& s1, char choice, int& target, int& num)
{
	cout << "Assign to room A1? (Y/N):";
	cin >> choice;
	num = 2;					
	while (toupper(choice) == 'Y' && num > r1.roomMA1size())
	{
		r1.printnoRoom();
		cout << "choose student(1-" << r1.noRoomsize() << "): ";
		cin >> target;
			if(target > r1.noRoomsize() || target <= 0)
			{
				cout << "Invalid Student\n";
				break;
			}
		target--;					
		r1.addMA1(target);						
		cout << "Assign more?(Y/N) CAPACITY LEFT = " << num - r1.roomMA1size() << " :";
		cin >> choice;
			if(r1.noRoomsize()== 0)
			{
				cout << "no more student" << endl;
				system("pause");
				break;
			}			
	};
	r1.printMA1();
	if(r1.roomMA1size() == 2)
	{
	cout << endl;
	cout << "capacity reached\n\n";
	}
	system("pause");
}


Topic archived. No new replies allowed.