Battleship Program: Errors

I am getting an error in my battleship program I have not seen before. I am a novice when it comes to debugging my own programs. The error messages I am getting are:

g++ -ansi -Wall -c gameboard.cpp
gameboard.cpp:126: error: expected â,â or â...â before â&â token
gameboard.cpp: In member function âvoid Gameboard::printGrid(char) constâ:
gameboard.cpp:131: error: expected initializer before â<â token
gameboard.cpp:225: error: expected primary-expression at end of input
gameboard.cpp:225: error: expected `;' at end of input
gameboard.cpp:225: error: expected primary-expression at end of input
gameboard.cpp:225: error: expected `)' at end of input
gameboard.cpp:225: error: expected statement at end of input
gameboard.cpp:131: warning: unused variable âjâ
gameboard.cpp:225: error: expected `}' at end of input
make: *** [gameboard.o] Error 1



Here is my code.

gameBoard.h
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
/**************************************************
   Author: 
   Logon ID: 
   Due Date: March 3, 2011
   
   Purpose: The gameboard header file contains the 
            class definition, constructor and method
            prototypes.
            
****************************************************/

#ifndef GAMEBOARD_H
#define GAMEBOARD_H

   //global declarations
   static const int GRIDSIZE = 8;
   const int SHIPS = 'S';
   const int NUM_SHIPS = 4;
   const int EMPTY = '.';
   
class Gameboard
   {
   private:
   
      //data members
      char gameBoard [GRIDSIZE][GRIDSIZE];
      
   public:
      
     //method prototypes
     void set_Gameboard(); // Loads gameboard to a blank state
     int set_randomPieces(); // Sets pieces randomly
     int set_playerPiece(); // Sets player pieces
     void printGrid(const char)const; //Prints the grids
     int player_bombsDropped(); //Tracks players bombs
     int computer_bombsDropped(); //Tracks computer bombs
     bool Gameover(); //Loop added to decide GG
   };
   
#endif // GAMEBOARD_H


gameboard.cpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "gameboard.h"
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cctype>

using std::cout;
using std::endl;
using std::cin;
using std::setw;
using std::toupper;


/*****************************************************************
   Function: set_Gameboard()
   
   Use: Initializes the elements of the array to an empty state.
   
   Arguments: None.
*****************************************************************/
void Gameboard::set_Gameboard()
   {
   int water[GRIDSIZE][GRIDSIZE];
   // Function to initialize the grid to empty space.
   for (int row=0; row <GRIDSIZE; row++)
      {
            for (int col=0; col <GRIDSIZE; col++)
            {
               water[row][col] = EMPTY;
            }
      }
   }

/*****************************************************************
   Function:set_randomPieces()
   
   Use: Randomly places the pieces for the computer
   
   Arguments: None
******************************************************************/   
int Gameboard::set_randomPieces()
   {
   srand(time(0));
   int row, col;
   int water[GRIDSIZE][GRIDSIZE];
   //Randomly placing ships within grid.
   for(int shipNum = 0; shipNum < NUM_SHIPS; shipNum++)   
      {
      bool randomset = true;   
         while (randomset)
            {
            row = rand() % GRIDSIZE;
            col = rand() % GRIDSIZE;
               if (water[row][col] == EMPTY)
                  {
                  water[row][col] = SHIPS;
                  randomset = false;
                  }
               else
                  randomset = true;
                  
            }
      }
   }
/*****************************************************************
   Functions:set_playerPieces()
   
   Use: Method that allows the player to manually set his/her pieces
   
   Arguments: None
*****************************************************************/
int Gameboard::set_playerPiece()
   {
   int lrow, ncol;
   char letter = 'A';
   int plyrPiece;
   cout <<"It is now time to place your ships within the grid." <<endl;
   cout <<"You will be able to place " <<NUM_SHIPS <<"within the grid." <<endl; 
   cout <<"Enter the row number first(0-7):" <<endl;
   cin >> lrow;
   cout <<"Enter the column letter next(A-H):" <<endl;
   cin >> letter;
   
   //Placing the players ship in the row
   for (int i = 0; i < NUM_SHIPS; i++)
      {
      bool plyrPiece = true;
         while (plyrPiece)
         {
            if (lrow < 0 && lrow > GRIDSIZE)
               {
               cout <<"No, No, No, you can't do that" <<endl;
               plyrPiece = false;
               }
            else 
               plyrPiece = true;
         }  
      }   
   //Ship placement loop
   for (int j = 0; j < NUM_SHIPS; j++)
      {
      while  (plyrPiece)
         {
            if (ncol < 0 && ncol > GRIDSIZE)
               {
               cout <<"No, No, No, you can't do that" <<endl;
               plyrPiece = false;
               }
            else
               plyrPiece = true;
         }         
      }
   
   //Letter to number conversion
   ncol = toupper(letter);
   }
   

/****************************************************************
   Functions:printGrid()
   
   Use: Method that prints out the grids and bombs dropped
   
   Arguments: None
*****************************************************************/
void Gameboard::printGrid(const char gameBoard& computerBoard) const
   {
   cout <<setw(5) <<"Your Bomb" << setw(5) << "Your Navy" <<endl;
   
   //prints out the column letters on top.
   for (int j = 0, j <1, j++)
      {
      for (int column ='A', column-'A'<GRIDSIZE, column++)
         {
            cout <<column <<endl;
               
            // loop row
         for (int x=0, x<GRIDSIZE, x++)
            {
               if (computerBoard.water[row][col] == SHIPS)
               cout <<EMPTY;
               else
               cout <<computerBoard.water[row][col] <<endl;  
                  
               // loop column
            for (int y=0, y<GRIDSIZE, y++)
               {
                  if (computerBoard.water[row][col] == SHIPS)
                     cout <<EMPTY;
                  else   
                  cout <<computerBoard.water[row][col] <<endl;
               }
            }
         }
       }   
      cout <<endl <<endl;
   }
/****************************************************************
   Functions:player_bombsDropped()
   
   Use: Method that allows the player to enter coordinates
        to drop bombs on the computer opponent.
        
   Arguments: None.
****************************************************************/
int Gameboard::player_bombsDropped()
   {
   int row,col;
   char letter;
   cout <<"Bombing coordinates please(Row,Column): " <<endl;
   cin >> row;    
   cout <<endl;
   cin >> letter;
   cout <<endl;
   
   //Loop to see if row was a hit or miss
   if (row == SHIPS && row > 0 && row <GRIDSIZE)
      cout <<"Nice shot! That was a hit!" <<endl;
   else if (row == EMPTY && row > 0 && row <GRIDSIZE)
      cout <<"That was a miss, sir" <<endl;
   else
      cout <<"Can not place coordinate outside the grid" <<endl;   
               
   //Loop to see if column was a hit or miss
   if (col == SHIPS && col > 'A' && col <GRIDSIZE)
      cout <<"Nice shot! That was a hit!" <<endl;
   else if (col == EMPTY && col > 'A' && col <GRIDSIZE)
      cout <<"That was a miss, sir" <<endl;
   else
      cout <<"Can not place coordinate outside the grid" <<endl;   
       
   
   //Letter to number conversion
   
   }
/****************************************************************
   Functions:computer_bombsDropped()
   
   Use: Method that allows the computer to chose coordinates to 
        drop bombs on his/her opponent.
        
   Arguments: None.
*****************************************************************/
int Gameboard::computer_bombsDropped()
   {
   int row, col;
   char letter;
   cout <<"Now it is computer's Turn!" <<endl;
   
   //Loop for random row pick
   if (row == SHIPS && row > 0 && row <GRIDSIZE)
      
      cout <<"The computer has hit one of our ships!" <<endl;
   else if (col == EMPTY && col > 'A' && col <GRIDSIZE)
      cout <<"The computer has missed!" <<endl;
   else
      cout <<"The computer has chosen coordinates outside the grid" <<endl;
       
   //Letter to number conversion
   
   
   //Random generator
   row = rand() % GRIDSIZE;
   col = rand() % GRIDSIZE;
   };      


I just can not find out what is wrong? I have been looking and changing but nothing. Any help or suggestions to make code better or faster would be helpful as well.


printGrid() does not match the prototype. And I don't know if you can take a reference of a const char the way you are trying.
Hmm. How would I pass the array into the print method? It would be a const though since I am changing nothing in the array. I am just printing out two grids side by side. Any hints?
It's a 2-dimensional array, correct? So just pass const int** const array or something like that.
Topic archived. No new replies allowed.