/
StudentSearch.cpp
190 lines (175 loc) · 4.32 KB
/
StudentSearch.cpp
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
#include "StudentList.h"
//Search screen for user
//Display lines:
//1. Search by name
//2. Search by dOB
//3. Search by address
//4. Search by entry year
//5. Search by class name
//6. Search by major
//0. Back
//Enter command:
////////////////////
//When user input, call the respective functions:
//'1': searchByName()
//'2': searchByDOB()
//'3': searchByAddress()
//'4': searchByEnterYear()
//'5': searchByClassName()
//'6': searchByMajor()
//'0': return;
//Otherwise: show error, ask for other input.
void StudentList::mainSearch() {
for(;;) {
cout << endl << endl;
cout << "Search a student: " << endl;
cout << "1. Search by name." << endl;
cout << "2. Search by dOB." << endl;
cout << "3. Search by address." << endl;
cout << "4. Search by entry year." << endl;
cout << "5. Search by class name." << endl;
cout << "6. Search by major." << endl;
cout << "0. Back." << endl;
cout << "Enter a number: ";
// Verify a command if it is integer and belong to {0..6}
int command;
while (true) {
cin >> command;
if (cin && (command < 7) && (command > -1)) break;
cout << "Invalid command!" << endl;
cin.clear();
cin.ignore(256,'\n');
}
//////////////////////////////////////////////////////////
switch (command) {
case 1: searchByName();
break;
//mainScreen();
case 2: searchByDOB();
//mainScreen();
break;
case 3: searchByAddress();
//mainScreen();
break;
case 4: searchByEnterYear();
//mainScreen();
break;
case 5: searchByClassName();
break;
case 6: searchByMajor();
break;
case 0: return;
default:
cout << "Invalid command!" << endl;
break;
}
}
}
//Sub functions
//Ask user for searching material (string/int)
//Ask for re-enter if the input is invalid
//Display all students who has the same info
void StudentList::searchByName() {
cout << "Enter a name to search: ";
cin.ignore();
string search_name;
getline(cin, search_name);
cout << endl;
student_pos i;
for (i = stu_list.begin(); i != stu_list.end(); ++i) {
string temp_string = i->Get_name();
size_t found = temp_string.find(search_name);
if (found != std::string::npos) {
i->Print();
cout << endl;
}
}
}
void StudentList::searchByDOB() {
cout << "Enter DoB to search: ";
cin.ignore();
string search_DoB;
getline(cin, search_DoB);
cout << endl;
student_pos i;
for (i = stu_list.begin(); i != stu_list.end(); ++i) {
string temp_DoB = i->Get_dOB();
size_t found = temp_DoB.find(search_DoB);
if (found != std::string::npos) {
i->Print();
cout << endl;
}
}
}
void StudentList::searchByAddress() {
cout << "Enter address to search: ";
cin.ignore();
string search_address;
getline(cin, search_address);
cout << endl;
student_pos i;
for (i = stu_list.begin(); i != stu_list.end(); ++i) {
string temp_address = i->Get_address();
size_t found = temp_address.find(search_address);
if (found != std::string::npos) {
i->Print();
cout << endl;
}
}
}
void StudentList::searchByEnterYear() {
cout << "Enter year to search: ";
cin.ignore();
int year;
cin >> year;
cout << endl;
string search_year;
ostringstream convert1;
convert1 << year;
search_year = convert1.str();
student_pos i;
for (i = stu_list.begin(); i != stu_list.end(); ++i) {
int temp = i->Get_enter_year();
string temp_year;
ostringstream convert2;
convert2 << temp;
temp_year = convert2.str();
size_t found = temp_year.find(search_year);
if (found != std::string::npos){
i->Print();
cout << endl;
}
}
}
void StudentList::searchByClassName() {
cout << "Enter class to search: ";
cin.ignore();
string search_class;
getline(cin, search_class);
cout << endl;
student_pos i;
for (i = stu_list.begin(); i != stu_list.end(); ++i) {
string temp_class = i->Get_class_name();
size_t found = temp_class.find(search_class);
if (found != std::string::npos) {
i->Print();
cout << endl;
}
}
}
void StudentList::searchByMajor() {
cout << "Enter major to search: ";
cin.ignore();
string search_major;
getline(cin, search_major);
cout << endl;
student_pos i;
for (i = stu_list.begin(); i != stu_list.end(); ++i) {
string temp_major = i->Get_major();
size_t found = temp_major.find(search_major);
if (found != std::string::npos) {
i->Print();
cout << endl;
}
}
}