Battleship Code

#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];



ifstream infile;
infile.open("board.txt");

if ( !infile){
cout << "Can not open file." << endl;
}


for (int i = 0; i < 25; i++) {
for (int j = 0; j < 25; j++){
char bS = 0;
infile.get(bS);
board[i][j] = bS;
}
}


int fS = 0;

do {


Fire(board);
FleetSunk(board, fS);

}
while(fS == 0 );


system("PAUSE");
}



void Fire(char board[25][25])
{
int row = 0;
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]){
case '#':
if (board[row-1][col] == 'H'){
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;
board[row][col] = 'H';
}
break;
case '~' :
cout << "MISS" << endl;
break;
case 'H' :
cout << "You already destroyed these coordinates.";
break;

}

}

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;
}
}
}
cout << "The Fleet has been destroyed!" << endl;
fS =1;

}
}


I complied it but I have five errors.. Can someone help me please !!
Well after trying to compile your program to find what errors you were talking about, my compiler told me this:

First of all, main should return an int. The standard explicitly says void main isn't allowed, so why use it?

Second of all, your declarations of Fire and FleetSunk don't match those of the actual function. That's what the compiler is trying to tell you. I'll take your Fire function as an example, since FleetSunk has the same problem.

1
2
void Fire(char board); //Declaration
void Fire(char board[25][25]); //Implementation 


Your declaration declares a function that takes a single char as argument, whereas your implementation (the correct one) declares a function that takes a 2-dimensional char array with 25 elements in each dimension.

So to resolve that problem, you should change the declaration to be the same as the implementation.

Last of all, my compiler complains about an extra closing bracket at the end of the file (after your FleeSunk function). Remove it, then your code will compile (I have no idea if it will actually run though, I didn't inspect the logic).
Topic archived. No new replies allowed.