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
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int STRING_CATCHER = 6;
const int STRING_CATCHER_OUT = 10;
const int IGNORE_VALUE = 1000;
const int MENU_MAX = 6;
const int MENU_MIN = 0;
const int NOT_FOUND = -1;
int FindRestaurant(string newRestaurant, vector<string> restaurantChoices);
void PrintMenu(){
cout << "Please select one of the following options: " << endl;
cout << endl;
cout << "0 - Quit the program" << endl;
cout << "1 - Display all restaurants" << endl;
cout << "2 - Add a restaurant" << endl;
cout << "3 - Remove a restaurant" << endl;
cout << "4 - \"Cut\" the list of restaurants" << endl;
cout << "5 - \"Shuffle\" the list of restaurants" << endl;
cout << "6 - Begin the tournament" << endl;
cout << endl;
cout << "Enter your selection now: " << endl;
return;
}
bool stringCatcher() {
if (cin.fail()) {
cin.clear();
cin.ignore(IGNORE_VALUE, '\n');
return true;
}
else {
return false;
}
}
bool menuError(int menuChoice) {
if (menuChoice > MENU_MAX || menuChoice < MENU_MIN)
{
cout << "Invalid selection. Please try again." << endl;
PrintMenu();
return true;
}
else {
return false;
}
}
void PrintVector(vector<string> restaurantChoices) {
for (unsigned int i = 0; i < restaurantChoices.size(); ++i) {
cout << "\t\"" << restaurantChoices.at(i) << "\"" << endl;
}
}
void AddRestaurant(string& newRestaurant, vector<string>& restaurantChoices) {
int found = 0;
found = FindRestaurant(newRestaurant, restaurantChoices);
if (found == NOT_FOUND) {
restaurantChoices.push_back(newRestaurant);
cout << newRestaurant << " has been added." << endl;
}
else {
cout << "That restaurant is already on the list, you can not add it again." << endl;
}
return;
}
int FindRestaurant(string newRestaurant, vector<string> restaurantChoices)
{
for (unsigned int i = 0; i < restaurantChoices.size(); i++)
{
if (newRestaurant == restaurantChoices[i])
{
return i;
}
}
return NOT_FOUND;
}
void RemoveRestaurant(string newRestaurant, vector<string>& restaurantChoices){
int found = 0;
found = FindRestaurant(newRestaurant, restaurantChoices);
if (found != NOT_FOUND) {
restaurantChoices.erase(restaurantChoices.begin() + found);
cout << newRestaurant << " has been removed." << endl;
}
else {
cout << "That restaurant is not on the list, you can not remove it." << endl;
}
return;
}
void CutRestaurant(vector<string>& restaurantChoices, int& cutPoint) {
for(unsigned int i = cutPoint; i < restaurantChoices.size(); i++){
unsigned int temp = restaurantChoices.at(i);
for(unsigned int j = cutPoint-1; j > i - cutPoint; j--){
restaurantChoices.at(j+1) = restaurantChoices.at(j);
}
restaurantChoices.at(i - cutPoint) = temp;
}
return;
}
int main() {
int menuChoice = 0;
vector<string> restaurantChoices;
string restaurantName;
int userCutPoint;
cout << "Welcome to the restaurant battle!" << endl;
cout << endl;
cout << endl;
PrintMenu();
do {
do {
cin >> menuChoice;
if (stringCatcher() == true) {
menuChoice = STRING_CATCHER;
}
} while (menuError(menuChoice));
switch (menuChoice) {
case 1:
cout << "Here are the current restaurants: " << endl;
cout << endl;
PrintVector(restaurantChoices);
PrintMenu();
break;
case 2:
cout << "What is the name of the restaurant you want to add? " << endl;
cin.ignore();
getline (cin, restaurantName);
AddRestaurant(restaurantName, restaurantChoices);
cout << endl;
PrintMenu();
break;
case 3:
cout << "What is the name of the restaurant you want to remove?" << endl;
cin.ignore();
getline(cin, restaurantName);
RemoveRestaurant(restaurantName, restaurantChoices);
cout << endl;
PrintMenu();
break;
case 4:
cout << "How many restaurants should be taken from the top and put on the bottom?" << endl;
cin.ignore();
getline(cin, userCutPoint);
CutRestaurant(restaurantChoices, userCutPoint);
cout << endl;
PrintMenu();
break;
case 0:
cout << "Goodbye!";
return 0;
break;
}
} while (true);
return 0;
}
|