Vector BATTLES!

This program takes in restaurants names and stores them in a vector to ultimately decide which one to eat at. My function cut is suppose to Prompt the user for a number. If the cut point is out of range say out of range.It then takes the specified number of Restaurant names off of the top of the vector of restaurant names, and put them in the same order at the bottom. If the restaurant name list contains a, b, c, and d and the cut point is 3, then after the cut the list should be: d, a, b, and c.Cut can be done by moving things around in the existing vector, but you might find that difficult. Instead, build a new vector as described and then return the newly assembled one from your cut function. I cant get my Cut function to work.

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;
}
If the restaurant name list contains a, b, c, and d and the cut point is 3, then after the cut the list should be: d, a, b, and c

http://www.cplusplus.com/reference/algorithm/rotate/
Cut can be done by moving things around in the existing vector, but you might find that difficult. Instead, build a new vector as described and then return the newly assembled one from your cut function.
Follow this advice, unless the assignment will let you use the rotate() function that gunnerfunner mentioned.
I still cant get it to work. This is what i got so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void CutRestaurant(vector<string>& restaurantList, int cutChoice) {
   vector<string> changeVector(cutChoice);
   if (cutChoice > changeVector.size()) {
      cout << endl << "The restaurants can not be cut there, there are only " << someVector.size() << " current - number - of - restaurants ";
		return;
   }
   for(unsigned int i = 0; i < cutChoice; i++){
      changeVector.at(i) = restaurantList.at(0);
      restaurantList.erase(restaurantList.at(i));
   }
   for(unsigned int i = 0; i < cutChoice; i++){
        restaurantChoices.push_back(restaurantChoices.at(i));
      }
 return;  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int main()
{
    std::vector<std::string> restaurantList
    {"Nobu", "Claridges", "Chez Gaston", "KFC", "Goodmans", "Sketch", "Nandos", "Light of India"};
    //If the restaurant name list contains a, b, c, and d and the cut point is 3
    std::rotate(restaurantList.begin(), restaurantList.begin()+3, restaurantList.end());
    for (const auto & elem : restaurantList)std::cout << elem << " ";
    std::cout << " \n";
}
edit: don't forget to validate the cut point vis-a-vis the vector size
Last edited on
I'm thinking something like:
1
2
3
4
5
6
7
8
9
   vector<string> changeVector(cutChoice);
   if (cutChoice > changeVector.size()) {
	cout << endl << "The restaurants can not be cut there, there are only " << someVector.size() << " current - number - of - restaurants ";
	return;
   }
   for(unsigned int i = 0; i < restaurantList.size(); i++){
	size_t pos = (i+cutChoice) % restaurantList.size();
	changeVector.push_back(restaurantList[pos]);
   }
Thank you both for your help
Topic archived. No new replies allowed.