You declare an array like this:
string myNames[10];
This creates an array big enough for ten string objects. Here is a list of all those elements:
1 2 3 4 5 6 7 8 9 10
|
myNames[0];
myNames[1];
myNames[2];
myNames[3];
myNames[4];
myNames[5];
myNames[6];
myNames[7];
myNames[8];
myNames[9];
|
As you can see, there are ten of them.
You then try to write into the array like this:
cin >> myNames [weekendBeer];
where
weekendBeer
takes the values 0,1,2,3,4,5,6,7,8,9,10
Let's write those elements out:
1 2 3 4 5 6 7 8 9 10 11
|
myNames[0];
myNames[1];
myNames[2];
myNames[3];
myNames[4];
myNames[5];
myNames[6];
myNames[7];
myNames[8];
myNames[9];
myNames[10];
|
There are eleven of them. But there is only space in the array for ten. Oh no. You are trying to write to
myNames[10]
, which is off the end of your array. It is memory that does not belong to you.