I am trying to create a simple slot machine that uses three vectors, one for the left, right, and center reels. For this code i am getting a debugging error "vector subscript out of range" but do not know why. All i want to do is take the vectors or reels, select a random element inside each of them and print out the next two numbers that come after the randomly selected elements. After i call spin function more then once the program crashes.
void spin(vector<int> lReel, vector<int> cReel, vector<int> rReel)
{
int random = rand() % lReel.size();
int lselection1 = lReel[random];
int lselection2 = lReel[random + 1];
int lselection3 = lReel[random + 2];
cout << lselection1 << " " << lselection2 << " " << lselection3 << endl;
int random2 = rand() % cReel.size();
int cselection1 = cReel[random2];
int cselection2 = cReel[random2 + 1];
int cselection3 = cReel[random2 + 2];
cout << cselection1 << " " << cselection2 << " " << cselection3 << endl;
int random3 = rand() % rReel.size();
int rselection1 = rReel[random3];
int rselection2 = rReel[random3 + 1];
int rselection3 = rReel[random3 + 2];
cout << rselection1 << " " << rselection2 << " " << rselection3 << endl;
}
int main()
{
vector<int> Left_Reel{ 1, 2, 3, 4, 5, 2, 2, 3, 4, 2, 2, 1, 1, 3, 3, 4, 2, 1, 1, 1, 4,
3, 2, 2, 1 };
vector<int> Center_Reel{ 3, 1, 2, 2, 3, 3, 4, 4, 2, 2, 3, 2, 1, 2, 4, 3, 2, 2, 1, 5, 4,
1, 3, 2, 2 };
vector<int> Right_Reel{ 2, 3, 4, 4, 4, 3, 1, 1, 1, 2, 3, 5, 4, 3, 2, 2, 2, 1, 1, 1, 3,
2, 1, 1, 2 };
cout << "SPIN" << endl;
spin(Left_Reel, Center_Reel, Right_Reel);
cout << "SPIN" << endl;
spin(Left_Reel, Center_Reel, Right_Reel);
system("Pause");
return 0;
}