Battleship Game
Dec 15, 2020 at 3:40am UTC
Hello,
I'm having trouble with two lines of the code below.
Lines 37 and 38 are giving errors
Line 37
" 'voidFire(char)': cannot convert argument 1 from 'char [25][25] to 'char'"
Line 38
" 'voidFleetSunk(char, int&)' : cannot convert argument 1 from 'char [25][25] to 'char'"
A friend asked me to look at this code for him and after searching online I or him can't seem to come up with why the error is coming up.
Code is running in Microsoft Visual studio.
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
#include<iostream>
#include<fstream>
#include <string.h>
using namespace std;
void Fire(/* inout*/ char board [25][25]);
void FleetSunk(/*in*/ char board [25][25],/*inout*/ int & fs);
int main()
{
char board[25][25];
ifstream infile;
infile.open("board.dat" );
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' ) {
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;
};
}.
Last edited on Dec 16, 2020 at 2:35am UTC
Dec 15, 2020 at 4:48am UTC
This
> void Fire(/* inout*/char board);
Must match this
> void Fire(char board[25][25])
Dec 15, 2020 at 9:22am UTC
The protypes of the functions (line 7/9) are wrong. They need to match the actual functions (line 47/90).
Dec 16, 2020 at 2:34am UTC
Thanks for the input Code is running as it should for the most part. When you input the Column and Line the program returns a 'Hit' 'Miss' as it should. However, it also returns the 'cout << from line 102' how can I get this to wait until all points have been hit??
Dec 16, 2020 at 5:48am UTC
You need to move a couple of lines down, so it's outside the outer loop.
1 2 3 4 5 6 7 8 9 10 11 12
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;
}
Normally, a function evaluating a yes/no question would return a boolean.
1 2 3 4 5 6 7 8 9 10
bool FleetSunk(char board[25][25]) {
for (int i = 0; i < 25; i++) {
for (int j = 0; j < 25; j++) {
if (board[i][j] == '#' ) {
return false ;
}
}
}
return true ;
}
So in main, it looks like this.
1 2 3 4
do {
Fire(board);
} while ( !FleetSunk(board) );
cout << "The Fleet has been destroyed!" << endl;
Topic archived. No new replies allowed.