RNG point generator crashes at 13 for unseen causes

I am attempting to write code that will generate a number of points based on an inputted value, on (obstacle number). The reason the code is using an if(k!=11) is because if k is equal to eleven, than j cannot equal 11 as well. When ever I input a number larger than 13 for on, the program generates 13 points, displays them, but then crashes, and I cannot figure out why. I do not know the severity of this question, so if I posted this in the wrong forum I do apologize. The reasons for the seemingly extra include files and variables is because this is part of a larger piece of code meant to be a game. Thank you for any help you can give.

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
 #include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
using namespace std;

int main()
{
	int np, x, y, xc=0, yc=0, n, ox[n]={}, oy[n]={}, j=0, k, oct, on; 
	string bp[12][12], bpp[12][12], p="*", pc="P", o="O";
	
	srand(time(NULL));
	
	xc=rand()%10;
	yc=rand()%10;
	
	bp[xc][yc]=pc;
	
	cin>>on;
	
	for(n=0;n<on;n++)
	{
		
		k=rand()%12;
	
		if(k!=11)
		{
			do
			{
				oct=0;
				j=rand()%12;
				ox[n]=k;
				oy[n]=j;
				for(np=0;np>n;np++)
				{
					if((ox[n]==ox[np]&&oy[n]==oy[np])||(ox[n]==xc&&oy[n]==yc))
					{
						oct=1;
					}					}
			}while(oct==1);				
		}
		else	
		{
			do
			{
				oct=0;
				j=rand()%11;
				ox[n]=k;
				oy[n]=j;
				for(np=0;np>n;np++)
				{
					if((ox[n]==ox[np]&&oy[n]==oy[np])||(ox[n]==xc&&oy[n]==yc))
					{
						oct=1;
					}
				}
			}while(oct==1);			
		}
	bp[k][j]=o;
	}
	
	cout<<"Pc = "<<xc<<","<<yc<<endl;
	for(n=0;n<on;n++)
	{
		cout<<n<<" "<<ox[n]<<","<<oy[n]<<endl;
	}
}
How large do you think are arrays ox and oy? n is uninitialised, it contains garbage.

Also, those are variable-length arrays which are not standard C++ - but if you use them, at least specify a valid size - and take care not to access elements outside the bounds of the array.
I moved the ox and oy arrays down past the on input and set their sizes to on, and it worked! Thank you! Could you explained why it worked? it was my understanding that if you were to assign a number to a slot in the array that doesn't exist it would add a slot? Why did array size become a problem only at 13 and not any number below it?
it was my understanding that if you were to assign a number to a slot in the array that doesn't exist it would add a slot?

That would be a misunderstanding. Even when the size of the array is determined at runtime, once set, the size is fixed. If assigning to a slot which doesn't exist, that value is stored in some adjacent memory location - which will probably already be in use - results are undefined, but definitely not good.

Why did array size become a problem only at 13 and not any number below it?

Well the array size is unknown, so the point at which the invalid memory use starts to become an obvious problem cannot be determined. Sometimes there might be no apparent problem, then some time later, some other part of the program could start to misbehave.

Generally in C++ when the size of an array is not known until the program is run, a std::vector is used. The ordinary array syntax using [index] to access each element can be used, but there is also the syntax .at(index) which will check the index value is in the valid range before permitting it to be used. Vectors also have the advantage that they can be re-sized even during use, if necessary.
Last edited on
Topic archived. No new replies allowed.