Array corruption error

Hey guys, I finished my project and it does work, but every time I hit enter to end the program I get an error that says my variable array is corrupted.

here is my code:

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
#include <iostream>
#include <ctime>

using namespace std;

int main()
{
	system ("color C");

	int i = 0;
	int loop = 0;
	int loop2 = 0;
	int total = 0;
	int track = 0;

	/*int Raph = 0;
	int Mikey = 0;
	int Leo = 0;
	int Don = 0;
	int Splinter = 0;
	int Shredder = 0;
	int April = 0;
	int Kraang = 0;*/

	int array1[8] = {0, 0, 0, 0, 0, 0, 0, 0}; //this doesn't work


	srand(time(NULL));
	loop = 1000;	// How many loops the function does
	loop2 = 1000;	// How many times the program runs
	
	for (int q = 0; q < loop2; q++)
	{

	for (int j = 0; j<loop;j++)
	{

		i = rand()%8+1; //shows a range from 1-8

		if (i == 1)	//if 1, increase Raph value
		{
			array1[0]++;
			
		}
		if (i == 2)	//if 2, increase Mikey value
		{
			array1[1]++;
			
		}
		if (i == 3)	//if 3, increase Leo value
		{
			array1[2]++;
			
		}
		if (i == 4)	//if 4, increase Don value
		{
			array1[3]++;
		
		}
		if (i == 5)	//if 5, increase Splinter value
		{
			array1[4]++;
			
		}
		if (i == 6)	//if 6, increase Shredder value
		{
			array1[5]++;
			
		}
		if (i == 7)	//if 7, increase April value
		{
			array1[6]++;
			
		}
		if (i == 8)	//if 8, increase Kraang value
		{
			array1[7]++;
			
		}

		break;

	}


	track += array1[0];
	
	array1[8] = 0;
	
	}

	if(array1[0] >1&& array1[1]>1 && array1[2] > 1 && array1[3] > 1&& array1[4]>1 && array1[5] > 1 && array1[6] > 1 && array1[7] > 1)
	{
		float final = track / 1000;
		cout<<"It took you an average of " << final << "/1000 tries to get at least one of each toy.\n\n";
	}

	else
	{
		cout<<"Sorry, even after 1000 tries you did not collect at least one of each toy :( \n\n";
	}

	system ("pause");
	return 0;

}


How could I stop this end error from occurring?
Last edited on
Line 88:

 
	array1[8] = 0;


element [8] is out of bounds. You must not access it.



Also.. the whole point to using an array is so you can use a variable to index it.

This crap:

1
2
3
4
5
6
7
8
9
10
11
12
13
		i = rand()%8+1; //shows a range from 1-8

		if (i == 1)	//if 1, increase Raph value
		{
			array1[0]++;
			
		}
		if (i == 2)	//if 2, increase Mikey value
		{
			array1[1]++;
			
		}
		// ... etc etc 


Can be replaced with this:

1
2
3
    i = rand()%8; // <- don't add 1 to get a range of 0-7
    array1[i]++;  // <- just use i to index the array directly, rather than doing
        // a giant else/if chain 


or... even more compact:
 
    array1[ rand() % 8 ]++;


If you do that, you can get rid of lines 38-79.


Also... what's the point of the loop on line 35 if you just break out of it on line 81?
Okay, well answering the last question first: the loop is supposed to run 1000 times and the other loop runs that loop 1000 times over (right?) If not, I'll just take out the other loop and see how that works.

I did compress the code to the first suggestion. Thanks for that info!

I'm still having issues with Line 88 though. Would I need to make it
array1[7] = 0 since it would be in the range?
I'm still having issues with Line 88 though. Would I need to make it
array1[7] = 0 since it would be in the range?

What's the point of that line? Why do you want to set one of the elements of the array to 0? And which element do you want to set to 0? The last one?
In another program I made I had to reset the variable I used that would count up. I figured this would be the case also.
In another program I made I had to reset the variable I used that would count up.

"Count up" to what? What are you using that 8th member in the array for? Why do you need to reset it to 0?
At the moment I have to go to another hour in class, so I will reply if I figure out what to do. Thanks for the help though!
Topic archived. No new replies allowed.