VL-CPP/VL03-02-01/main.cc

100 lines
2.3 KiB
C++

#include <iostream>
#include <vector>
void checkGuest(std::vector<std::string> &guests) {
std::cout << "Guest: ";
std::string guest;
std::cin >> guest;
bool is_guest = false;
for (std::vector<std::string>::iterator it = guests.begin(); it != guests.end();) {
if (*it == guest) {
is_guest = true;
break;
} else {
++it;
}
}
std::cout << guest << " is " << ((is_guest) ? "" : "NOT ") << "on the guest list!" << std::endl;
}
void createGuest(std::vector<std::string> &guests) {
bool keepInput = true;
do {
std::string guest;
std::cout << "Guest: ";
std::cin >> guest;
guests.push_back(guest);
std::cout << "More? (1 = Yes - 0 = No) ";
std::cin >> keepInput;
} while (keepInput);
}
void removeGuest(std::vector<std::string> &guests) {
std::cout << "Guest: ";
std::string guest;
std::cin >> guest;
for (std::vector<std::string>::iterator it = guests.begin(); it != guests.end();) {
if (*it == guest) {
it = guests.erase(it);
} else {
++it;
}
}
}
void showGuests(std::vector<std::string> &guests) {
std::cout << "-- List of all guests --" << std::endl;
for (std::string &guest : guests) {
std::cout << "\t" << guest << std::endl;
}
}
int showMenuSelection() {
std::cout << std::endl << std::endl;
std::cout << "-- Menu (Guest list manager) --" << std::endl;
std::cout << "\t 1 - New guest" << std::endl;
std::cout << "\t 2 - Remove guest" << std::endl;
std::cout << "\t 3 - Check guest" << std::endl;
std::cout << "\t 4 - Show guests" << std::endl;
std::cout << "\t 5 - Clear guest list" << std::endl;
std::cout << "\t 9 - Exit" << std::endl;
std::cout << std::endl << "Selection: ";
int selection;
std::cin >> selection;
return selection;
}
int main() {
std::vector<std::string> guests;
while (int selection = showMenuSelection()) {
switch (selection) {
case 1:
createGuest(guests);
break;
case 2:
removeGuest(guests);
break;
case 3:
checkGuest(guests);
break;
case 4:
showGuests(guests);
break;
case 5:
guests.clear();
std::cout << "The guest list was cleared!" << std::endl;
break;
case 9:
return 0;
break;
}
}
return 0;
}