I am having trouble with the beginning of a battleship program. Here is the header file I must use for the program. All of the ships are just 1 space big. I am getting a bunch of error messages saying "local function definitions are illegal" and "syntax error: identifier 'loc'". Can someone please tell me what I am doing wrong? I am very new to c++.
//
// data structures definitions
//
constint FLEET_SIZE=5; // number of battleships
constint FIELD_SIZE=5; // the field (ocean) is FIELD_SIZExFIELD_SIZE
// coordinates (location) of the ship and shots
struct location{
int x; // 1 through FIELD_SIZE
char y; // 'a' through FIELD_SIZE
};
// contains ship's coordinates (location) and whether is was sunk
struct ship{
location loc;
bool sunk;
};
//
// initialization functions
//
void initialize(ship[]); // places all ships in -1 X location to signify
// that the ship is not deployed
location pick(void); // generates a random location
bool match(ship, location); // returns true if this location matches
// the location of the ship
// returns false otherwise
int check(const ship[], location); // returns the index of the element
// of the ship[] array that matches
// location. Returns -1 if none match
// uses match()
void deploy(ship[]); // places an array of battleships in
// random locations in the ocean
Line 46: loc does not have a type. I think you meant location loc
Line 49: a) Incompatible types. Maybe you meant if(s[i].loc == loc)
b) Above line have only 1/256 chance to work, because it compares both location.x and location.y, which is undefined in initialize() function.
c) You didn't use match() function as required
Im honestly not sure what to do in the check() function. I just tried this as a starting point and that clearly wasn't right. I also didn't use match() because I didn't know what to do or exactly what its purpose was. Also, should the initialize function also set location.y to -1? I wasn't sure on how to approach that.
statement after if will execute only if logical statement evaluates to true. match() is a functions which returns bool (true or false). So line 5 in this example will be executed only if match(s[i], loc) returns true.
Lool at the equivalent snippet: