Program crashes, I think it's a memory violation but i dont know

So I'm writing a c++ version of the game battleship. I am currently trying to get random placement for my ships on the board, it works sometimes but other times it crashes. I think it's accessing memory from outside my array but I'm not sure. Any advice would be appreciated. Below is my function for placing the battleship. The array it's being entered into is [10][10]

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
  void ship::placeBattleship(board & boardA)
{
	int x=0;
	int front;
	int roll1;
	char bow='O';
	
  do{	
	front=(rand()%10);
	roll1=(rand()%2);
	if(roll1==0)
	{
		int x;
		x=(rand()%10);
		boardA.boardA[front][x]=bow;
		
		if(front<5)
		{
			for(int i=0;i<4;i++)
			boardA.boardA[front+i][x]='O';
		}
		if(front>=5)
		{
			for(int i=0;i<4;i--)
			boardA.boardA[front-i][x]='O';
		}
	
	}	
	else
	{
		int x;
		x=(rand()%10);
		boardA.boardA[x][front]=bow;
		
		if(front<5)
		{
			for(int i=0;i<4;i++)
			boardA.boardA[x][front+i]='O';
		}
		if(front>=5)
		{
			for(int i=0;i<4;i--)
			boardA.boardA[x][front-i]='O';
		}
	
	}
    }while(front<0||front>9 && x<0||x>9);
}
for(int i=0;i<4;i--)
This may be the problem.

Also you are going to want to use parenthesis for your while loop on line 47 more than likely.
Last edited on
When will this loop end?
for(int i=0;i<4;i--)
Topic archived. No new replies allowed.