Battleship C++

First time submitting anything, I'm new to coding and new to c++. I know my code probably looks a mess to the experienced eye. I have to write a code as described. I having trouble just wrapping my mind on how I should lay this out and the functionality there-in. I prof has been very difficult to reach outside of class and he recommended looking for answers here. Recognize that it is still an unfinished product but I cant get past "pointer to a function used in arithmetic." and its making me question my whole project. Any help or suggestions would be appreciate but layman terms please :)

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

using namespace std;

/*Test #2, battleship game.
Write a C++ program that implements a bi-dimensional array of 4x4 elements and initialize it with 0s.
Each battleship is two positions long in either vertical or horizontal position.
The program should prompt the user as follow:
Enter #1 to position the battleships.
Enter #2 to start playing.
Enter #4 to quit.
If the user enters a number different than 1, 2 or 4, then display and error message and display the above options again.
If the user enters #1, then ask the user to enter each of the locations of the 3 battleships with specifying each position as (x,y), locations containing battleships should be set from 0 to 1.
0 0 0 0          0 1 0 0
0 0 0 0 ===> 0 1 0 0
0 0 0 0           0 0 1 1
0 0 0 0           1 1 0 0
In the example above, the user has position one battleships on (1,0)-(1,1) the second one on (2,2)-(3,2) and the third on on (0,3)-(1,3)
After the user has finished positioning the third ship, the program should go back to the main menu.
When user enters #2, the program should prompt the user to start shooting by asking to enter the value of x and the value of y, one shoot at the time, and then show if it Failed or Hit a battleship.
If the user enters 4 at any time the program must display a message saying “goodbye” and quit.
The user could keep shooting until hitting all the battleships or reaching a maximum number of 16 shoots, on either case, then the program should show the outcome as:
F H 0 F
F H F 0
0 F H H
H H F 0
Total shoots = 12, Hits = 6, Fails = 6*/



int x,y,x1,y1,x2,y2,h,f;
	
int gameboard[4][4];

void initialize_gameboard(int gameboard[4][4]);
const int noship = 0; //point without ship
const int ship = 1; //point with a ship
const char hit = h; //tried and hit ship
const char miss = f; //tried and missed ship

int num,num1,num2,num3,num4;
float ship1(float x,float y);
float ship2(float x1,float y1);
float ship3(float x2,float y2);

int main()
{
	
	cout<<"Enter 1 to position the battleships.\n";
	cout<<"Enter 2 to start playing.\n";
	cout<<"Enter 4 to quit.\n";
	cin>>num;
	
	

	if (num==1)
	{
		cout<<"Enter three Battleship positions.\n";
		cout<<"First Battleship.\n";
		cin>>x,y;
		{
		
			for (int x=0;x<4;x++)
			cout<<ship1[x][y]<<" ";
		
			{
		
				for (int y=0;y<4;y++);
			}
		}
		cout<<"Second Battleship.\n";
		cin>>x1,y1;
		{
		
			for (int x1=0;x1<4;x1++)
			cout<<ship2[x1][y1]<<" ";
		
			{
		
				for (int y1=0;y1<4;y1++);
			}
		}
		cout<<"Third Battleship.\n";
		cin>>x2,y2;
		{
			
			for (int x2=0;x2<4;x2++)
			cout<<ship3[x2][y2]<<" ";
		
			{
		
				for (int y2=0;y2<4;y2++)
			
				
						
				cout<<endl;
			}
		}
	}
	else if (num==2)
	{
		
		
		
	}
	else if (num==4)
	{
	
		cout<<"Goodbye!\n";
		return 0;
	}
}
> but I cant get past "pointer to a function used in arithmetic."
This is a function prototype
float ship1(float x,float y);

This is a function definition
1
2
3
float ship1(float x,float y) {
  return x + y;
}

This is a function call
x = ship1(1.0,2.0);

This is a function pointer used in arithmetic.
cout<<ship1[x][y]<<" ";

Note the use of ( ) vs [ ].

> cin>>x,y;
To input two values, use
cin >> x >> y;


As a general comment, you have way too many global variables, and some very poor names for things.


Also, your main is going to bloat with code unless you take steps to structure things.
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
#include<iostream>
#include<iomanip>
using namespace std;

const int BOARD_SIZE = 4;

void initialize_gameboard(int gameboard[BOARD_SIZE][BOARD_SIZE]);
void place_ships(int gameboard[BOARD_SIZE][BOARD_SIZE]);
void play_game(int gameboard[BOARD_SIZE][BOARD_SIZE]);

// remove this before submission
void dump_board(int gameboard[BOARD_SIZE][BOARD_SIZE]) {
    for ( int r = 0 ; r < BOARD_SIZE ; r++ ) {
        for ( int c = 0 ; c < BOARD_SIZE ; c++ ) {
            cout << gameboard[r][c] << " ";
        }
        cout << endl;
    }
}

void show_menu() {
    cout<<"Enter 1 to position the battleships.\n";
    cout<<"Enter 2 to start playing.\n";
    cout<<"Enter 4 to quit.\n";
}

int main(void) {
    int gameboard[BOARD_SIZE][BOARD_SIZE];
    initialize_gameboard(gameboard);
    int choice;
    do {
        show_menu();
        cin >> choice;
        if ( choice == 1 ) {
            initialize_gameboard(gameboard);
        } else
        if ( choice == 2 ) {
            play_game(gameboard);
        } else
        if ( choice == 9 ) {
            // shhhh!!! it's a secret ;)
            dump_board(gameboard);
        }
    } while ( choice != 4 );
}


You can put a single cout statement in each function and actually run the code to check the overall program flow. Once you're happy with that, then you can start to fill in each function in turn.

Oh that’s great info! Thanks so much! Now I’ll go try and comprehend all of that! Lol
This is a function pointer used in arithmetic.
cout<<ship1[x][y]<<" ";

Looks like nonsense to me.

I did some reading and found that GCC compiles this as an extension. GCC treats the size of the pointed-to function as 1.

So I guess what happens there is that the expression (ship1[x]) yields some function type which immediately undergoes the function-to-pointer conversion. The resulting pointer would be generally useless, barring some shell-code trickery or something.

I'm certain that OP doesn't need to bother with this.
Heres my end result of this after much trial and tribulation and lots of assistance -

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

using namespace std;

char gameboard[4][4];

void initialize_gameboard(char gameboard[4][4]); // Function declaration
void print_gameboard(char gameboard[4][4]);
int prompt_user();
int num;
void askForBattleShip(char gameboard[4][4]);
void askForAllBattleShips(char gameboard[4][4]);
void startShooting(char gameboard[4][4]);
void takeshot(char gameboard[4][4]);
void checkifallarehit(char gameboard[4][4]);


const char noship = '0'; //point without ship
const char ship = '1'; //point with a ship
const char hit = 'h'; //tried and hit ship
const char miss = 'f'; //tried and missed ship


int main()
{
	cout << "Battleship" << endl;
  initialize_gameboard(gameboard);




while (true){
	
    cout<<"Enter 1 to position the battleships.\n";
	cout<<"Enter 2 to start playing.\n";
	cout<<"Enter 4 to quit.\n";
	cin>>num;
	
if (num == 1) {
		askForAllBattleShips(gameboard);
	} else if (num == 2) {
		startShooting(gameboard);
	} else if (num == 4) {
		cout<<"bye!bye!\n";
  		exit;
  		break;
	} 
		else {
		
	} 
}
}

void askForBattleShip(char gameboard[4][4])
{
    int x,y, ok=0;
    
    do
    {
		cout <<"Please enter a battle ship" << endl;     
		cout<<"please enter x coordinate.\n";
		cin>>x;
		cout<<"please enter y coordinate.\n";
		cin>>y;
	
	if (gameboard[x][y]==noship)
	{ 
			gameboard[x][y]=ship;
			ok=1;
	}
	else	
		cout<< "That coordinate was already taken!!\n";
			
	} while (ok==0);
	
	ok=0;
	
	do
	{
		cout<<"please enter 2nd x coordinate.\n";
		cin>>x;
		cout<<"please enter 2nd y coordinate.\n";
		cin>>y;
		
		if (gameboard[x][y]==noship)
		{
				gameboard[x][y]=ship;
				ok=1;
		}
			
		else	
			cout<< "That coordinate was already taken!!\n";
			
	}  while (ok==0);
	
}

void askForAllBattleShips(char gameboard[4][4]){
  for(int i = 0; i < 3 ; i ++) {
    askForBattleShip(gameboard);
  }
	print_gameboard(gameboard);
}

void initialize_gameboard(char gameboard[4][4]){ // Function Definition
  for(int i = 0; i < 4; i++) { //row
    for(int j = 0; j < 4; j++){ // column
      gameboard[i][j] = noship;
    }
  }
}

void print_gameboard(char gameboard[4][4]){
  for(int i = 0; i < 4; i++) { //row
    for(int j = 0; j < 4; j++){ // column
      cout << gameboard[i][j];
    }
    cout << endl;
  }
}

void startShooting(char gameboard[4][4])
 {
  for(int i = 0 ; i < 12; i++) 
      takeshot(gameboard);
      
    for(int i = 0; i < 4; i++)
	{  //row
    	for(int j = 0; j < 4; j++)
     	 cout << gameboard[i][j];
     	 cout << endl;
    }  

}

void takeshot(char gameboard[4][4]) {
  int x,y;
  cout << "Please enter shot x value" << endl;
  cin >>x;
  cout << "Please enter shot y value" << endl;
  cin >>y;

  if(gameboard[x][y] == ship) 
	{
  		gameboard[x][y]=hit;
  			cout<<"hit.\n";
  			
	}else if (gameboard[x][y]==noship)
	
		{
			gameboard[x][y]=miss;
				cout<<"miss.\n";
		}
	 
		
    cout << endl;
}
Topic archived. No new replies allowed.