Rand() function and dynamic memory for Battleship.

I was working on a one player battleship game where the computer will randomly place the amount of ships that the player requests on the board, randomly. However when I run the program it simply stops running if the user enters a number above one. And if the user does enter 1 the program seems to always place the ship at (6,6). I have tried running the program multiple times and with multiple different variations and still have gotten the same result. If someone could explain to me why the rand function keeps giving 6 as the result and how to properly use dynamic memory correctly, to create multiple ships.
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
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
	cout << "This is your game board. Ships taking up one spot each will be placed on your game board. You have to shoot all of them down to win. Good luck\n";
	string board[7][7] = { { "0 ", "1 ", "2 ", "3 ", "4 ", "5 ", "6 " },
	{ "1 ", "O ", "O ", "O ", "O ", "O ", "O " },
	{ "2 ", "O ", "O ", "O ", "O ", "O ", "O " },
	{ "3 ", "O ", "O ", "O ", "O ", "O ", "O " },
	{ "4 ", "O ", "O ", "O ", "O ", "O ", "O " },
	{ "5 ", "O ", "O ", "O ", "O ", "O ", "O " },
	{ "6 ", "O ", "O ", "O ", "O ", "O ", "O " } };
	for (int i = 0; i <= 6; i++)
	{
		for (int j = 0; j <= 6; j++)
		{
			cout << board[i][j];
		}
		cout << endl;
	}
	int row, column, ships, x = 0, turns = 0, i, * sr, * sc, ships_left;
	bool hit = false;
	B:
	cout << "Enter the amount of ships you want on the board, up to 36. ";
	cin >> ships;
	if (ships < 0 || ships > 36)
	{
		cout << "That number is invalid. Make sure to enter a value more than zero and less than 36. ";
		goto B;
	}
	ships_left = ships;
	sr = new int[ships];
	sc = new int[ships];
	C:
	for (i = x; i < ships; i++)
	{
		sc[i] = rand() % 6 + 1;
		sr[i] = rand() % 6 + 1;
		for (int a = i; a >= 1; a--)
		{
			if (sc[i] == sc[a] && sr[i] == sr[a])
			{
				goto C;
			}
		}
		x++;
	}

This is only the beginning of the code, where I used the dynamic memory and rand () function however if it is needed I can place the rest of the code.
You need to initialize the random generator. http://www.cplusplus.com/reference/cstdlib/srand/
Topic archived. No new replies allowed.