Help with passing in a boolean for a function.

For my code I am trying to pass in a boolean isPowerTwo into the function void Shuffle, to check if the vector string restaurant is a power of two. However when I run my code it states:
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 1) >= this->size() (which is 1)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <math.h>
#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <limits>

using namespace std;

const int NOT_FOUND = -1;

//Initialize Vector
vector<string> initialize() {
	vector<string> restaurants;
	return restaurants;

}

//Showing all the restaurants
void printRestaurants(vector<string>& restaurants) {
	cout << "Here are the current restaurants: " << endl << endl;
	for (int index = 0; index < restaurants.size(); index++) {
		if (index < restaurants.size() - 1) {
			cout << "\t" << "\"" << restaurants[index] << "\" " << endl;
		}
		else
			cout << "\t" << "\"" << restaurants[index] << "\"" << endl;
	}

}

//Finding the restaurant name from the user
int find(string restaurantName, vector<string> restaurants) {
	for (int index = 0; index < restaurants.size(); index++) {
		if (restaurantName == restaurants[index]) {
			return index;
		}
	}
	return NOT_FOUND;
}

//Add a restaurant
void addRestaurant(string restaurantName, vector<string>& restaurants) {
	int found = find(restaurantName, restaurants);
	if (found == NOT_FOUND) {
		restaurants.push_back(restaurantName);
		cout << restaurantName << " has been added." << endl;
	}
	else {
		cout << "That restaurant is already on the list, you can not add it again." << endl;
	}
}

//Remove a restaurant
void removeRestaurant(string restaurantName, vector<string>& restaurants) {
	int found = find(restaurantName, restaurants);
	if (found != NOT_FOUND) {
		int last_pos = restaurants.size() - 1;
		restaurants.erase(restaurants.begin() + found);
		cout << restaurantName << " has been removed." << endl;
	}
	else {
		cout << "That restaurant is not on the list, you can not remove it." << endl;
	}
}

void removeLoserRestaurant(string restaurantName, vector<string>& restaurants) {
	int found = find(restaurantName, restaurants);
	if (found != NOT_FOUND) {
		int last_pos = restaurants.size() - 1;
		restaurants.erase(restaurants.begin() + found);
	}
	else {
		cout << "The restaurant" << restaurantName << "was not removed because it was not in the vector" << endl;
	}
}

////Cut the list of restaurants
void cutRestaurant(vector<string>& restaurants) {
	vector<string> changeVector = restaurants;
	//int blastedChoice = cutChoice;
	const int OUT_OF_RANGE = 0;
	int cutChoice = 0;
	cin >> cutChoice;
	
	while (cutChoice > changeVector.size() || cutChoice < OUT_OF_RANGE || cin.fail()) {
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		cout << endl << "The cut number must be between 0 and " << changeVector.size() << endl;
		cout << "How many restaurants should be taken from the top and put on the bottom?" << endl;
		cin >> cutChoice;
	}
	for (int i = 0; i < cutChoice; i++) {
		changeVector.at(i) = restaurants.at(0);
		restaurants.erase(restaurants.begin());
	}
	for (int j = 0; j < cutChoice; j++) {
		restaurants.push_back(changeVector.at(j));
	}
	return; 
}
	
//bool isPowerTwo(int x) {
//	return !(x & (x - 1));
//}

bool isPowerTwo(int x) { //x = restaurants.size()
	int blastedTwo = 2;
	while (x > 1) {
		if (x % 2 != 0) {
			return false;
		}
		x /= 2;
	}
	return true;
}

//Shuffling the Vector Restaurants
void shuffleVector(vector<string>& restaurants) {

	vector<string> newVector;
	vector<string> firstVector;
	if (isPowerTwo(restaurants.size())) {
	for (int i = 0; i < ((restaurants.size() / 2) - 1); i++) {
		firstVector.push_back(restaurants.at(i));
	}
	for (int i = ((restaurants.size() / 2) - 1); i < restaurants.size(); i++) {
		lastVector.push_back(restaurants.at(i));
	}

	for (int i = 0; i < restaurants.size() / 2; i++) {
		newVector.push_back(firstVector.at(i));
		newVector.push_back(lastVector.at(i));
	}
	restaurants = newVector;
}
else {
		cout << "The current tournament size(" << restaurants.size() << ") is not a power of two(2, 4, 8...)." << endl;
		cout << "A shuffle is not possible. Please add or remove restaurants." << endl;
	}
}

// Main Function
int main() {
	srand(time(0));
	vector<string> restaurants;
	int menu = 1;
	bool done = false;
	int menuOption = 1;
	int cutChoice = 1;
	const int BLASTED_ONE_THOUSAND = 1000;
	const string QUIT_OPTION = "quit";
	const string DISPLAY_RESTAURANTS = "display";
	const string ADD_RESTAURANT = "add";
	const string REMOVE_RESTAURANT = "remove";
	const string CUT_VECTOR = "cut";
	const string SHUFFLE_VECTOR = "shuffle";
	string userInput;

	//vector<string> restaurants = initialize();
	cout << "Welcome to the restaurant battle! Enter \"options\" to see options." << endl << endl;
	/*cout << "Enter your selection:" << endl;
	cin >> userInput;*/

	while (!done) {
		cout << "Enter your selection:" << endl;
		cin >> userInput;
		cin.ignore(); // i am just trying to get this to work!!!!!!!
	
		if (userInput == DISPLAY_RESTAURANTS) {
			printRestaurants(restaurants);
			cout << endl;
		}
		else if (userInput == ADD_RESTAURANT) {
			cout << "What is the name of the restaurant you want to add?" << endl;
			string restaurantName;
			getline(cin, restaurantName);
			//cout << "Will this pause 1" << endl;
			addRestaurant(restaurantName, restaurants);
			cout << endl;
		}
		else if (userInput == REMOVE_RESTAURANT) {
			cout << "What is the name of the restaurant you want to remove ? " << endl;
			string restaurantName;
			getline(cin, restaurantName);
			removeRestaurant(restaurantName, restaurants);
			cout << endl;
		}
		else if (userInput == CUT_VECTOR) {
			cout << "How many restaurants should be taken from the top and put on the bottom?" << endl;
			//string cutChoice;
			//getline(cin, cutChoice);
			
			cutRestaurant(restaurants);
			cout << endl;
		}
		else if (userInput == SHUFFLE_VECTOR) {
			//cout << "Did this actually enter this vector" << endl;
			shuffleVector(restaurants);
			cout << endl;
		}

		}
		else if (userInput == QUIT_OPTION) {
			menu = 0;
			cout << endl;
			cout << "Goodbye!" << endl;
			//done = true;
			return 0;
		}
		else {
			cin.clear();
			cin.ignore(BLASTED_ONE_THOUSAND, '\n');
			cout << "Invalid selection " << endl;
			printMenu();
		}
	}


//system("pause");
	return 0;
}
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
#include <iostream>
#include <limits>
#include <vector>
#include <string>
#include <algorithm>
#include <random>
#include <iomanip>

std::size_t get_cut_number( std::size_t sz ) {

    std::cout << "How many restaurants should be taken from the top and put at the bottom?\n"
              << "The cut number must be between 0 and " << sz-1 << ": " ;

    std::size_t cut_number ;
    if( std::cin >> cut_number ) {

        if( cut_number < sz ) return cut_number ;
        else std::cout << "the value is out of range. try again.\n" ;
    }

    else
    {
        std::cout << "error: non-integer input. try again\n" ;
        std::cin.clear() ;
        std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    }

    return get_cut_number(sz) ;
}

std::vector<std::string>& cutRestaurant( std::vector<std::string>& restaurants, std::size_t cut_number ) {

    // http://en.cppreference.com/w/cpp/algorithm/rotate
    std::rotate( restaurants.begin(), restaurants.begin()+cut_number, restaurants.end() ) ;
    return restaurants ;
}

std::vector<std::string>& shuffleVector( std::vector<std::string>& restaurants ) {
    
    // http://en.cppreference.com/w/cpp/numeric/random
    static std::mt19937 engine( std::random_device{}() ) ;
    
    // http://en.cppreference.com/w/cpp/algorithm/random_shuffle
    std::shuffle( restaurants.begin(), restaurants.end(), engine ) ;
    return restaurants ;
}

void printRestaurants( const std::vector<std::string>& restaurants) {

    std::cout << "Here are the current restaurants:\n" ;
    
    // http://www.stroustrup.com/C++11FAQ.html#for
    // http://www.stroustrup.com/C++11FAQ.html#auto
    // http://en.cppreference.com/w/cpp/io/manip/quoted
    for( const auto& r : restaurants ) std::cout << std::quoted(r) << "  " ;
    std::cout << '\n' ;
}

int main()
{
    std::vector<std::string> restaurants { "abc", "def g", "ij klmn", "opq", "rs", "tuv", "wxyz" } ;
    printRestaurants(restaurants) ;

    const std::size_t cut_number = get_cut_number( restaurants.size() ) ;
    cutRestaurant( restaurants, cut_number ) ;
    std::cout << "\nafter cutRestaurant (cut_number==" << cut_number << "): " ;
    printRestaurants(restaurants) ;

    shuffleVector(restaurants) ;
    std::cout << "\nafter shuffleVector: " ;
    printRestaurants(restaurants) ;
}

http://coliru.stacked-crooked.com/a/34f782e423f70d86
Topic archived. No new replies allowed.