Battle ship, trouble compiling know where, not why

I am having trouble getting this code to work and it is only one line that stops it from compiling but that one line sets my ships and that is crucial. Any thoughts on why it won't work? The problem is my SetShips function when I call it in my main function (I commented it out and it built okay, I just need to know why and how to fix it!) Thanks so much in advance!

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
 {#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

const int Rows = 15;
const int Columns = 15;

void printBoard(char board[][Columns]);		// Creates the 15x15 board
void setShips(char board[][Columns], int size);		// Randomly places ships
void checkBoard(char board[][Columns], bool &victory);		// Checks the board to see if you have won

int main()
{
	srand( static_cast<unsigned int >(time(NULL)));		// starts the random generator
	char board[Rows][Columns];		//sets the array of 15x15
	int sizeShip, nShips;		// creates the ships and their sizes
	nShips = 5;

	for (int i =0; i < Rows; i++)
	{
		for (int j  = 0; j < Columns; j++)
		{
			board[i][j] = '~';		// creates the wave for the ships
		}
	}

	cout << "Welcome to battleship!" << endl << endl;		// welcomes the user

	while(nShips < 6)		// supposed to set sizes of ships 
	{
		for(int i = 1; i <=nShips; i++)
		{
			if(i<2)
			{
				sizeShip = 2;
			}
			if(i>2 && i<4)
			{
				sizeShip = 3;
			}
			if(i>4 && i < 6)
			{
				sizeShip = 4;
			}
			//while (sizeShip > 1 || sizeShip <6);
			//{
				//setShips(board, sizeShip);		// THIS IS THE PROBLEM sets ships to the board
			//}
		}
	
	}

	int Attacks = 60, iGuess, jGuess;		// sets number of guesses to 60
	bool victory = false;		// bool is false because you haven't played yet
	
	if(nShips != 1)		// introducing the game
	{
		cout << "Here is your game board. There are " << nShips << " enemy battleships hidden on it." << endl;
	}
	else
	{
		cout << "Here is your game board. There are " << nShips << " enemy battleships hidden on it." << endl;
	}
	cout << "You have 60 trys to find all of the ships. Input a row and column coordinate (x y) to attack." << endl;
	cout << "Make your first move. Good luck." << endl;
	

	for (int n =1; n <= Attacks && !victory; n++)		// subtracting attacks for each guess
	{
		printBoard(board);
		cout << "Attack number" << n << ", x and y: ";
		cin >> iGuess >> jGuess;
		if(board[iGuess - 1][jGuess - 1] == 'S')
		{
			cout << "You got a hit! You hit at the coordinates: " << iGuess << " " << jGuess << endl;
			board[iGuess - 1][jGuess - 1] = '#';
		}
		else if (board[iGuess - 1][jGuess - 1] == '~')
		{
			cout << "I'm sorry. You missed!" << endl;
			board[iGuess - 1][jGuess - 1] = 'o';
		}
		victory = true;
		checkBoard(board, victory);		// checks if victory is true
	}

	cout << "Here is the final board: " << endl;		// printing the final board
	printBoard(board);
	cout << "A '~' is open sea, 'o' is a miss, '#' is a hit, and 'S' is part of a ship you did not sink." << endl;
	if (victory)		// if won or lost
	{
		cout << "Congratulations, you sunk all of the enemy battleships!" << endl;
	}
	else
	{
		cout << "You didn't win. Better luck next time!" << endl;
	}

	system ("PAUSE");
	return 0;
}

void printBoard(char board[][Columns])		// print board function
{
	for(int i = 0; i < Rows; i++)
	{
		for(int j = 0; j < Columns; j++)
		{
			cout << board[i][j];
		}
		cout << endl;
	}
	return;
}

void SetShips(char board[][Columns], int size)		// randomly set ships
{
	int iStart, jStart, ort;

Randomize:
	do
	{
		iStart = rand()%Rows;
		jStart = rand()%Columns;
		ort = rand()%2;
	}
	while (board[iStart][jStart] == 'S');
	{
		if(ort == 0)
		{
			while(jStart + size >= Columns)		// checks if there is a ship under it
			{
				jStart = rand()%Columns;
			}
			for(int j = jStart; j < jStart + size; j++)		// checks if it is off the board
			{
				if(board[iStart][j] == 'S')
				{
					goto Randomize;
				}
			}
			for (int j = jStart; j < jStart + size; j++)
			{
				board[iStart][j] = 'S';
			}
		}
		if (ort == 1)
		{
			while (iStart + size >= Rows)		// checks if there is a ship under it
			{
				iStart = rand()%Rows;
			}
			for(int i = iStart; i < iStart + size; i++)		// checks if ship is off the board
			{
				if (board[i][jStart] == 'S')
				{
					goto Randomize;
				}
			}
			for (int i = iStart; i < iStart + size; i++)
			{
				board[i][jStart] = 'S';		// places ship if no obstacles
			}
		}
	}
		return;
}

void checkBoard(char board[][Columns], bool &victory)		// checks board to see if you won
{
	for(int i = 0; i < Rows; i++)
	{
		for(int j = 0; j < Columns; j++)
		{
			if (board[i][j] == 'S')		// S means there is a ship, if there is a S you did not win
			{
				victory = false;
			}
		}
	}
	return;
}
}
There is no definition for function called setShips in your code.
However there is one for SetShips, which is not used.
Great! Now it builds, but does nothing other than welcome the user... It doesn't even get to, here is the game board, is there a problem with my ship size function.
nships is 5, so how the hell you reckon you're gonna get out of this loop:

1
2
3
4
5
6
7
while(nShips < 6)
{
...
...


}


also:
1
2
if(i>2 && i<4)

is the same as
 
if(i == 3)


and likewise for the next if too


However your main issue:
 
//while (sizeShip > 1 || sizeShip <6);  // <-- semi-colon here is BAD! 

you do this in more than one place.

Also, why return from a void function? You shouldn't be doing this either really.

I think you need to check your entire logic in that function.

and.... using goto is not good either.
Last edited on
Thank you so much! It works, it just shows the board with the ships on it, which isn't much fun.
Is there a way to label the columns by letters, I should declare an enumeration but how to I make it set as the columns for the array. Is there also a way to label the ships and show when you have sunk them?
Last edited on
Ok, so it works. Is there a way to label my columns as letters, I know I should use an enumeration, and also is there a way for it to output if my ships have been sunk, and also the ship's name?
Topic archived. No new replies allowed.