Vector subscript out of range

I'm working on a code to create a round-robin style tournament for a person to choose their favorite restaurant from a list of 16. All goes well until halfway through the list, when Starbucks is pitted against itself. So you enter Starbucks and go on, and soon enough another restaurant is against itself, and then another directly after, at which point the code runs across a fatal error: Vector Subscript out of range.

I don't know why this is popping up, what it means, or how to fix it. Any help on this would be much appreciated. As a bonus, I need to be able to redirect the user to the matchup if they input something incorrect (ie Panda Express when the options are McDonalds and Tucanos), so help on that would also be welcome.

Thank you!

(Coding for the tournament bit starts at line 133, the rest is included in case it holds any clues)

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
 #include <iostream>
#include <iomanip>
#include <ctime>
#include <vector>
#include <cmath>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
	srand(time(NULL));
	vector<string> restaurant;
		restaurant.push_back("Wendy's");
		restaurant.push_back("McDonalds");
		restaurant.push_back("Popeye's");
		restaurant.push_back("Starbucks");
		restaurant.push_back("Tucanos");
		restaurant.push_back("Cafe Rio");
		restaurant.push_back("The Slab");
		restaurant.push_back("Hoagieville");
		restaurant.push_back("Taco John's");
		restaurant.push_back("Olive Garden");
		restaurant.push_back("Lion's Tap");
		restaurant.push_back("The Roasted Pear");
		restaurant.push_back("Panda Express");
		restaurant.push_back("Cinnabon");
		restaurant.push_back("Texas Roadhouse");
		restaurant.push_back("The Alley Connection");

	int bob=0;
	while (bob==0)
	{

	cout<<endl<<"Welcome! "<<endl<<endl<<"Main Menu:"<<endl<<endl;
	cout<<"Display all restaurants: Please enter A"<<endl;
	cout<<"Add a restaurant: Please enter B"<<endl;
	cout<<"Remove a restaurant: Please enter C"<<endl;
	cout<<"Shuffle the restarurants: Please enter D"<<endl;
	cout<<"Begin the tournament: Please enter E"<<endl;
	cout<<"Quit the program:Please enter Q"<<endl;
		
	string selection;
	getline(cin,selection);
	
	if (selection=="A"||selection=="a")
	{ 
		for (int i=0; i < restaurant.size();i++)
		{ 
			cout<<restaurant[i]<<endl;
		}
	}

	if (selection=="B"||selection=="b") 
	{
		string input;
		cout<<"Please enter the restaurant you would like to add"<<endl;
		getline(cin, input);
		
		bool valid= true;

		for (int i=0; i< restaurant.size();i++)
		{ 
			if (restaurant[i]== input)
				{
					cout<<"This restaurant is already on the list. Congratulations."<<endl;
					valid=false;
				}
		}

		if (valid==true)
				{ 
					restaurant.push_back(input);
					cout<< input <<" has been added to the list. Congratulations."<<endl;
				}
		}


	if (selection=="C"||selection=="c")
	{ 
		string input;
		cout<<"Which restaruant would you like to remove from the lineup?"<<endl;
		getline(cin, input);

		bool valid=true;
		int pos=0;
		for (int i=0; i < restaurant.size(); i++)
			{
			if (restaurant[i]== input)
				{
					cout<<"This restaurant is on the list. Congratulations."<<endl;
					pos=i;
					valid=true;
				}
			}
		if (valid==true)
			{
				int last_pos=restaurant.size()-1;
				restaurant [pos]= restaurant[last_pos];
				restaurant.pop_back();
				cout<<"This restaurant has been removed from the list. Congratulations."<<endl;
			}
	}

	if (selection=="D"||selection=="d")
	{
		for (int i=0; i < 100; i++)
			{
				int x= rand() % restaurant.size();
				int y= rand() % restaurant.size();
				string olditem=restaurant[y];
				restaurant[y]=restaurant[x];
				restaurant[x]=olditem;
			}
	}

	if (selection=="E"||selection=="e")
	{ 
		bool valid=false;

		for (double i=0; i<5; i++)
		{
			double power= pow(2,i);

			if (restaurant.size()==power)
				{
					valid=true;
				}
		}
		if (valid==true)
		{
			cout<<"Welcome to the Tournament of Restaurants."<<endl;

			while (restaurant.size()>1)
			{
				for (int i=0; i< restaurant.size(); i++)
				{
					cout<<"Please choose between:"<<endl;
					cout<<restaurant[i]<<" and "<<restaurant[restaurant.size()-1]<<endl;
					string answer;
					getline(cin, answer);

					if (answer==restaurant[i])
						{
							restaurant.pop_back();
						}
					if (answer==restaurant[restaurant.size()-1])
						{
							restaurant[i]=restaurant[restaurant.size()-1];
							restaurant.pop_back();
						}

				}

			}
			cout<<restaurant[1]<<" is the winner!!"<<endl;


		}

		else 
		{
			cout<<"This is not a valid number of restaraunts. Please go back to the menu to add or remove restaurants until you have 2, 4, 16, or 32."<<endl;
		}
		
	}
	if (selection=="Q"||selection=="q")
	{	return 0;
	}
	}
	system ("PAUSE");
	return 0;
	
}

Few issues. For one, pop_back only deletes the last element of a vector, not the first. Two, you seem to have flipped first and last concerning vectors and the respective iterator. Three, look at line 157. Vectors start at 0, not 1. Four, this whole thing could really benefit from some functions so there are less repeat variables.
Topic archived. No new replies allowed.