arrays in battleship game. help.

I can't for the life of me figure out these damn multi-dimensional arrays. I can't figure out what I need to put in the prototype or in the actual function call. Also I am getting the gameboard with pre-set game pieces from an outside text file. '~' is ocean, '#' is ships, and 'H' are spaces that were hit. It compiles but at running it gives me an error after user inputs rows and columns in the Fire function. Any ideas on how to make this work would be great. Thanks.

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
  #include<iostream>
#include<fstream>
#include<string.h>

using namespace std;

void Fire(/* inout*/char board);

void FleetSunk(/*in*/char board ,/*inout*/ int&);



void main()
{
   char board[25][25];		// Create a 25 x 25 array for gameboard


	// get board.dat and get ready to assign the value to the board
	ifstream infile;
	infile.open("board.dat");
	
	if ( !infile){				//check file 
		cout << "Can not open file." << endl;
	}

// nested for loops to assign chars in the text file to spaces in an array.
	for  (int i = 0; i < 25; i++) {			// goes through each row
		for (int j = 0; j < 25; j++){	//goes through each column
			char bS = 0;					// bS is the character given that is being assigned to a space on the board	
			infile.get(bS);					// get character from "board.dat" 				
			board[i][j] = bS;        //assign that character to board[i][j]
		}
	}
	

	int fS = 0;					// Create an int variable to determine when to exit loop 	
	
	do {
		
		
		Fire(board);		//calls the fire function, passing board by reference
		FleetSunk(board, fS);	// calls the FleetSunk function passing board by value and fS by reference
	
	}
	while(fS == 0 );			// closes loop is all of the ships have been sunk

	
	system("PAUSE");
}




// The Fire function takes in board[] by reference and uses it for the fire function 
void Fire(char board[25][25])
{
	int row = 0;			//initializes the int x and y that will represent row and column respectively
	int col = 0;

	cout << "Enter the Row and Column that you would like to try and hit :" ;
	cin >> row;   // 
	cin >> col;


	switch (board[row][col]){		// switch statement to determine what character is at board[x][y]
		case '#':					// if ship(#) then print 'Hit' and replace '#' with 'H'
			if (board[row-1][col] == 'H'){			//checks to see if any of the spaces around the spot hit has been hit previously
				cout << "HIT AGAIN" << endl;
			board[row][col] = 'H'; 
			}
			else if (board[row+1][col] == 'H'){
				cout << "HIT AGAIN" << endl;
			board[row][col] = 'H'; 
			}
			else if (board[row][col-1] == 'H'){
				cout << "HIT AGAIN" << endl;
			board[row][col] = 'H'; 
			}
			else if (board[row][col+1] == 'H'){
				cout << "HIT AGAIN" << endl;
			board[row][col] = 'H'; 
			}
			else{
			cout << "HIT" << endl;			// if there is no ship next to the spot hit then dispay "hit"
			board[row][col] = 'H';
			}
			break;
		case '~' :				// if ocean(~) then print miss and move on
			cout << "MISS" << endl;
			break;
		case 'H' :				// if 'H' then that means user already hit this location.  
			cout << "You already destroyed these coordinates.";
			break;

	}

}
// checks game board for any #/ unhit ship pieces
void FleetSunk(char board[25][25], int& fS) {
	for (int i= 0; i < 25; i++){
		for (int j=0 ; j< 25; j++){
			if (board[i][j] == '#'){
				fS = 0;
				 return;			// if there is ship left return back to program
			}
		}
	}	
	cout << "The Fleet has been destroyed!" << endl; 
				fS =1;				// if there is no longer any # left fS = 1;
					
	}
}
I'm really wondering if this code will compile. You should declare your function prototypes using the same signature as their implementations.

Some tips:
1. To avoid buffer overflow, you should assert that variables row and col values in function Fire() are in valid range, say 0..24. (In "real" programming languages other than C/C++ you may declare an appropriate range type).
2. Even if row and col values are of valid range your implementation may fail f.e. in line 71 if row is 24. Your code accesses an array element out of range for reading. This may at least result into an undefined value read.
Topic archived. No new replies allowed.