Array subscript out of range???

I'm running this code in VS2016 and instead of getting a normal error, the program stops running and I get a "array subscript out of range" pop up. This is my first C++ class. I don't think it is supposed to be a difficult fix, but I'm lost (as usual).

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
  // Description: Run-time errors in loops and branching
//----------------------------------

//**begin #include files************
#include <iostream> // provides access to cin and cout
#include <time.h>
#include <iomanip>
#include <array>
#include <vector>
//--end of #include files-----------
//----------------------------------

using namespace std;
//----------------------------------


//**begin main program**************
int main()
{
	// seed random number generator
	srand( int ( time(NULL)));
	// create enum for symbols
	enum symbol
	{
		Lemon, Cherry, Orange, Bell, Jackpot
	};
	// create a struct for slot machine wheel
	struct Wheel
	{
		array <string, 10> symbols;
		array <symbol, 10> eSymbols;
		int position;
		string selected;
	};
	//create an array of three slot machine wheels
	array <Wheel, 3> slotMachine =
	{
		{
			{
				{ "Cherry", "Orange", "Lemon", "Orange", "Bell", "Orange", "Lemon", "Cherry", "Jackpot", "Bell" },
				{ Cherry, Orange, Lemon, Orange, Bell, Orange, Lemon,Cherry, Jackpot, Bell },
				0,"Cherry"
			},
			{
				{ "Cherry", "Bell", "Lemon", "Orange", "Bell", "Jackpot", "Lemon", "Cherry", "Jackpot", "Bell" },
				{ Cherry, Bell, Lemon, Orange, Bell, Jackpot, Lemon, Cherry, Jackpot, Bell },
				1,"Bell"
			},
			{
				{ "Cherry", "Orange", "Lemon", "Orange", "Lemon", "Orange", "Lemon","Cherry", "Jackpot", "Bell" },
				{ Cherry, Orange, Lemon, Orange, Lemon, Orange, Lemon, Cherry, Jackpot, Bell },
				2,"Lemon"
			}
		}
	};

	bool gameOn = true;
	bool winner = false;
	int thePot = 100;
	int bet = 1;
	vector <int> combo;
	while (gameOn)
	{
		for (int i = 1; i < 4; i++)
		{
			slotMachine[i].position = (slotMachine[i].position + rand() % 10) % 10;
			slotMachine[i].selected = slotMachine[i].symbols[slotMachine[i].position];
			cout << setw(10) << left << slotMachine[i].selected.c_str();
			combo.push_back(slotMachine[i].eSymbols[slotMachine[i].position]);
		}
		if ((combo[0] == combo[1]) && (combo[1] == combo[2]))
		{
			if (combo[0] == Lemon)
			{
				cout << "You keep your bet." << endl;
			}
			else if (combo[0] = Jackpot)
			{
				cout << "**** You hit $1000 Jackpot!!! ****" << endl;
				thePot += 1000;
				winner = true;
				gameOn = false;
			}
			else
			{
				cout << "WINNER! You win $" << combo[0] * 5 << endl;
				thePot += combo[0] * 5;
			}
		}
		else
		{
			thePot -= bet;
			if (thePot > 0) gameOn = false;
		}
		cout << "You now have $" << thePot << endl;
		combo.clear();
		cout << endl;
		cin.get();
	}
	if (winner) cout << "You walk away a winner." << endl;
	else cout << "You have lost all your money." << endl;
	// Wait for user input to close program when debugging.
	cin.get();
	return 0;
}
//--end of main program-------------
//---------------------------------- 
array <Wheel, 3> slotMachine =
Valid indices for an array of size 3 are 0 through 2. You are using indices 1 through 3.
So will I fix it by changing 3 to 4?
No, remember arrays are zero based and end at size - 1. Your loop should start at zero and end at the array.size().

Topic archived. No new replies allowed.