Vectors and such...
Mar 28, 2017 at 12:39am UTC
My program is suppose to take in restaurant names into a vector and in one of the options print the vector, but for some reason mine won't print them. This is how it should print:
Enter your selection now:
Here are the current restaurants:
"Super Taqueria"
"Noglu, New York"
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
#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;
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;
restaurantChoices.push_back(newRestaurant);
return ;
}
int main() {
int menuChoice = 0;
vector<string> restaurantChoices;
string restaurantName;
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);
break ;
case 2:
cout << "What is the name of the restaurant you want to add? " << endl;
cin.ignore();
getline (cin, restaurantName);
AddRestaurant(restaurantName);
cout << endl;
cout << restaurantName << " has been added." << endl;
PrintMenu();
break ;
case 0:
cout << "Goodbye!" ;
return 0;
break ;
}
} while (true );
return 0;
}
Mar 28, 2017 at 12:48am UTC
1 2 3 4 5
void AddRestaurant(string newRestaurant) {
vector<string> restaurantChoices; //local variable goes out of scope
restaurantChoices.push_back(newRestaurant);
return ; // no need for this
}
Pass the vector as an argument by reference to the function.
void AddRestaurant(const std::string& newRestaurant, std::vector<std::string>& restaurantChoices) {
Good Luck !!
Mar 28, 2017 at 1:14am UTC
Thank you! I get confused with passing by reference and by value, can you explain it to me?
Mar 28, 2017 at 1:35am UTC
You could Google it, there is a wiki page about that very thing :+)
Mar 28, 2017 at 1:39am UTC
Gotcha, thanks man!
Topic archived. No new replies allowed.