I have a couple of C++ labs that I need to finish before the end of the semester and I have been beating my head against this computer for days and I am not getting the code to work. I need some serious help. I need to get these done before the end of next week. ANY HELP will be greatly appreciated!!!! I can show you the code I have so far.

Recommended Answers

All 16 Replies

I can show you the code I have so far.

Please do...there's no way to help you without it.

// battleship.cpp
// structure definitions and function prototypes
// for the battleship assignment
// christopher l. hawkins jr.
// 12/04/2010

#include "battleship.h"
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

// initialization functions
//
// places an array of battleships in
// random locations in a 5 x 5 grid (the ocean)
void deploy(ship deployShip[]) {

    int shipsDeployed = 0;
    location possibleLocation;

    // loops until all 5 ships deployed
    while (shipsDeployed != FLEET_SIZE) {

        // iterates over already deployed ships to ensure unique locations
        for (int i = 0; i <= shipsDeployed; i++) {

            // checks location of deployed ship to new random location
            // if no match assigns location to next ship
            if (match(deployShip[i], possibleLocation))
                break;
            else {
                deployShip[i].loc = possibleLocation;
                deployShip[i].sunk = false;
            }
        }
        // increments deployed ship counter
        shipsDeployed++;
    }
}
// generates a random location
location pick(void) {

    int x;
    char y;
    location gridLocation;

    x = rand() % FIELD_SIZE + 1;

    // converts a random number from 1 - 5 to letters 'a' - 'e'
    switch (rand() % FIELD_SIZE + 1) {
    case 1:
        y = 'a';
        break;

    case 2:
        y = 'b';
        break;

    case 3:
        y = 'c';
        break;

    case 4:
        y = 'd';
        break;

    case 5:
        y = 'e';
        break;

    default:
        break;
    }

    gridLocation.x = x;
    gridLocation.y = y;

    return gridLocation;
}
// returns true if this location matches
// the location of the ship
// returns false otherwise
bool match(ship tryShip, location tryLocation) {

    bool flag;

    if ((tryShip.loc.x == tryLocation.x) && (tryShip.loc.y == tryLocation.y))
        flag = true;
    else
        flag = false;

    return flag;
}
//
// display functions
//
// prints the locations of all the ships and whether they
// are sunk or not
void printFleet(const ship battleFleet[]) {
    for (int i = 0; i < FLEET_SIZE - 1; i++) {
        printShip(battleFleet[i]);
        cout << ", ";
    }
}
// prints the location and status (sunk or not) of a single ship
void printShip(ship battleShip) {
    cout << battleShip.loc.y << battleShip.loc.x << " ";
    if (battleShip.sunk = true)
        cout << "sunk";
    else
        cout << "up";
}
// battle functions
//
// returns true if at least one ship in the array
// is not sunk
bool operational(const ship operationalFleet[]) {
    bool flag;

    for (int i = 0; i <= FLEET_SIZE - 1; i++){
        if (operationalFleet[i].sunk == false)
            flag = true;
        else
            flag = false;
    }
    return flag;
}
// asks the user to input the coordinates of the next
// shot
location fire(void) {
    location shot;

    cout << "Please enter the letter and number of your shot: ";
    cin >> shot.y >> shot.x;

    return shot;

}
// note that match() is also used in the game
//
// sets "sunk" member variable of the ship to true
void sink(ship& sinkShip) {
    sinkShip.sunk = true;
}
//
//
//



// battleship.h
// structure definitions and function prototypes
// for the battleship assignment
// christopher l. hawkins jr.
// 12/04/2010
//
#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_
//
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
//
// data structures definitions
//
const int FLEET_SIZE=5; // number of battleships
const int 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 deploy(ship[]); // places an array of battleships in
                     // random locations in the ocean
//
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
//
//
//
//
// display functions
//
void printFleet(const ship[]); // prints the locations of all the ships and whether they
                               // are sunk
void printShip(ship); // prints the location and status (sunk or not) of a single ship
//
//
//
//
//
// battle functions
//
bool operational(const ship[]);  // returns true if at least one ship in the array
                                 // is not sunk
//
location fire(void);       // asks the user to input the coordinates of the next
                           // shot
//
// note that match() is also used in the game
void sink(ship&);          // sets "sunk" member variable of the ship to true
//
//
//
//
//
#endif /* BATTLESHIP_H_ */




// battleshipInput.cpp
// structure definitions and function prototypes
// for the battleship assignment
// christopher l. hawkins jr.
// 12/04/2010

#include "battleship.h"
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main(){

    int answer;
    ship fleet[FLEET_SIZE];
    location shot;

    // seed the random number generator
    srand(time(NULL));

    // assigns random locations to array of ships and sets sunk flag
    // to false on each
    deploy(fleet);

    cout << "would you like a printout of the ships locations?:  [y/n]";
    cin >> answer;
    if (answer == 'Y' || answer == 'y'){
        printFleet(fleet);
    }

    // game play until last ship is sunk
    while (operational(fleet)){

        shot = fire();

        for (int i = 0; i <= FLEET_SIZE - 1; i++) {

            if  (match(fleet[i], shot)) {
                sink(fleet[i]);
                cout << "Hit!";
            }
            else
                cout << "Miss!";
        }
    }
    cout << "Congratulations you sank all of the Battleships!";
    printFleet(fleet);
}

Next time please use the code tags to post your code [code]

[/code]

Ok, so what are the issues with it?

from what I can tell it is not leaving the inner loop of deploy() and all the ships are getting the same location, then it just like ricochets through my print functions.

location possibleLocation; You declare this in deploy() but you never give it a value.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

// initialization functions
//
// places an array of battleships in
// random locations in a 5 x 5 grid (the ocean)
void deploy(ship deployShip[]) {

	int shipsDeployed = 0;
	location possibleLocation;

	// loops until all 5 ships deployed
	while (shipsDeployed != FLEET_SIZE) {

		possibleLocation = pick();

		// iterates over already deployed ships to ensure unique locations
		for (int i = 0; i <= shipsDeployed; i++) {

			// checks location of deployed ship to new random location
			// if no match assigns location to next ship
			if (match(deployShip[i], possibleLocation))
				break;
			else {
				deployShip[i].loc = possibleLocation;
				deployShip[i].sunk = false;
			}
		}
		// increments deployed ship counter
		shipsDeployed++;
	}
}
// generates a random location
location pick(void) {

	int x;
	char y;
	location gridLocation;

	x = rand() % FIELD_SIZE + 1;

	// converts a random number from 1 - 5 to letters 'a' - 'e'
	switch (rand() % FIELD_SIZE + 1) {
	case 1:
		y = 'a';
		break;

	case 2:
		y = 'b';
		break;

	case 3:
		y = 'c';
		break;

	case 4:
		y = 'd';
		break;

	case 5:
		y = 'e';
		break;

	default:
		break;
	}

	gridLocation.x = x;
	gridLocation.y = y;

	return gridLocation;
}
// returns true if this location matches
// the location of the ship
// returns false otherwise
bool match(ship tryShip, location tryLocation) {

	bool flag;

	if ((tryShip.loc.x == tryLocation.x) && (tryShip.loc.y == tryLocation.y))
		flag = true;
	else
		flag = false;

	return flag;
}
//
// display functions
//
// prints the locations of all the ships and whether they
// are sunk or not
void printFleet(const ship battleFleet[]) {
	for (int i = 0; i < FLEET_SIZE - 1; i++) {
		printShip(battleFleet[i]);
		cout << ", ";
	}
}
// prints the location and status (sunk or not) of a single ship
void printShip(ship battleShip) {
	cout << battleShip.loc.y << battleShip.loc.x << " ";
	if (battleShip.sunk = true)
		cout << "sunk";
	else
		cout << "up";
}
// battle functions
//
// returns true if at least one ship in the array
// is not sunk
bool operational(const ship operationalFleet[]) {
	bool flag;

	for (int i = 0; i <= FLEET_SIZE - 1; i++){
		if (operationalFleet[i].sunk == false)
			flag = true;
		else
			flag = false;
	}
	return flag;
}
// asks the user to input the coordinates of the next
// shot
location fire(void) {
	location shot;

	cout << "Please enter the letter and number of your shot: ";
	cin >> shot.y >> shot.x;

	return shot;

}
// note that match() is also used in the game
//
// sets "sunk" member variable of the ship to true
void sink(ship& sinkShip) {
	sinkShip.sunk = true;
}
//
//
//

sorry, I had that in there before but I tried a different approach that was really bad and I had to retype the deploy() function from a hard copy. However, the program still doesn't work.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

// initialization functions
//
// places an array of battleships in
// random locations in a 5 x 5 grid (the ocean)
void deploy(ship deployShip[]) {

	int shipsDeployed = 0;
	location possibleLocation;

	// loops until all 5 ships deployed
	while (shipsDeployed != FLEET_SIZE) {

		possibleLocation = pick();

		// iterates over already deployed ships to ensure unique locations
		for (int i = 0; i <= shipsDeployed; i++) {

			// checks location of deployed ship to new random location
			// if no match assigns location to next ship
			if (match(deployShip[i], possibleLocation))
				break;
			else {
				deployShip[i].loc = possibleLocation;
				deployShip[i].sunk = false;
			}
		}
		// increments deployed ship counter
		shipsDeployed++;
	}
}
// generates a random location
location pick(void) {

	int x;
	char y;
	location gridLocation;

	x = rand() % FIELD_SIZE + 1;

	// converts a random number from 1 - 5 to letters 'a' - 'e'
	switch (rand() % FIELD_SIZE + 1) {
	case 1:
		y = 'a';
		break;

	case 2:
		y = 'b';
		break;

	case 3:
		y = 'c';
		break;

	case 4:
		y = 'd';
		break;

	case 5:
		y = 'e';
		break;

	default:
		break;
	}

	gridLocation.x = x;
	gridLocation.y = y;

	return gridLocation;
}
// returns true if this location matches
// the location of the ship
// returns false otherwise
bool match(ship tryShip, location tryLocation) {

	bool flag;

	if ((tryShip.loc.x == tryLocation.x) && (tryShip.loc.y == tryLocation.y))
		flag = true;
	else
		flag = false;

	return flag;
}
//
// display functions
//
// prints the locations of all the ships and whether they
// are sunk or not
void printFleet(const ship battleFleet[]) {
	for (int i = 0; i < FLEET_SIZE - 1; i++) {
		printShip(battleFleet[i]);
		cout << ", ";
	}
}
// prints the location and status (sunk or not) of a single ship
void printShip(ship battleShip) {
	cout << battleShip.loc.y << battleShip.loc.x << " ";
	if (battleShip.sunk = true)
		cout << "sunk";
	else
		cout << "up";
}
// battle functions
//
// returns true if at least one ship in the array
// is not sunk
bool operational(const ship operationalFleet[]) {
	bool flag;

	for (int i = 0; i <= FLEET_SIZE - 1; i++){
		if (operationalFleet[i].sunk == false)
			flag = true;
		else
			flag = false;
	}
	return flag;
}
// asks the user to input the coordinates of the next
// shot
location fire(void) {
	location shot;

	cout << "Please enter the letter and number of your shot: ";
	cin >> shot.y >> shot.x;

	return shot;

}
// note that match() is also used in the game
//
// sets "sunk" member variable of the ship to true
void sink(ship& sinkShip) {
	sinkShip.sunk = true;
}
//
//
//

NVM: I found it

"answer" in battleshipinput.cpp is an int, it should be a char. You were putting in 'y' and the stream went bad so it looped.

For extra credit: see if you can express match() as one line.

I made some changes and now it runs but it still puts all ships in the same loc and isn't seeing the sunk flag in operational() I think.

// battleship.cpp
// structure definitions and function prototypes
// for the battleship assignment
// christopher l. hawkins jr.
// 10/27/2010

#include "battleship.h"
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

// initialization functions
//
// places an array of battleships in
// random locations in the ocean
void deploy(ship deployShip[]) {

	int nextShipToDeploy = 0;
	location possibleLocation;

	while(nextShipToDeploy != FLEET_SIZE) {

		possibleLocation = pick();

		for (int i = 0; i <= nextShipToDeploy; i++) {

			if (match(deployShip[i], possibleLocation))
				break;
			else {
				deployShip[i].loc = possibleLocation;
				deployShip[i].sunk = false;
			}
		}
		nextShipToDeploy++;
	}
}
// generates a random location
location pick(void) {

	int x;
	char y;
	location gridLocation;

	x = rand() % FIELD_SIZE + 1;

	switch (rand() % FIELD_SIZE + 1) {
	case 1:
		y = 'a';
		break;

	case 2:
		y = 'b';
		break;

	case 3:
		y = 'c';
		break;

	case 4:
		y = 'd';
		break;

	case 5:
		y = 'e';
		break;

	default:
		break;
	}

	gridLocation.x = x;
	gridLocation.y = y;

	return gridLocation;
}
// returns true if this location matches
// the location of the ship
// returns false otherwise
bool match(ship tryShip, location tryLocation) {

	bool flag;

	if (tryShip.loc.x == tryLocation.x && tryShip.loc.y == tryLocation.y)
		flag = true;
	else
		flag = false;

	return flag;
}
//
// display functions
//
// prints the locations of all the ships and whether they
// are sunk
void printFleet(const ship battleFleet[]) {
	for (int i = 0; i <= FLEET_SIZE - 1; i++) {
		printShip(battleFleet[i]);
		cout << ", ";
	}
}
// prints the location and status (sunk or not) of a single ship
void printShip(ship battleShip) {
	cout << battleShip.loc.y << battleShip.loc.x << " ";
	if (battleShip.sunk = true)
		cout << "sunk";
	else
		cout << "up";
}
// battle functions
//
// returns true if at least one ship in the array
// is not sunk
bool operational(const ship operationalFleet[]) {
	bool flag = false;
	for (int i = 0; i <= FLEET_SIZE - 1; i++){
		if (operationalFleet[i].sunk == false){
			flag = true;
			return flag;
		}
	}
	return flag;
}
// asks the user to input the coordinates of the next
// shot
location fire(void) {
	location shot;

	cout << "Please enter the letter and number of your shot: ";
	cin >> shot.y >> shot.x;

	return shot;

}
// note that match() is also used in the game
//
// sets "sunk" member variable of the ship to true
void sink(ship& sinkShip) {
	sinkShip.sunk = true;
}
//
//
//



// battleship.h
// structure definitions and function prototypes
// for the battleship assignment
// christopher l. hawkins jr.
// 10/27/2010
//
#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_
//
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
//
// data structures definitions
//
const int FLEET_SIZE=5; // number of battleships
const int 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 deploy(ship[]); // places an array of battleships in
                     // random locations in the ocean
//
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
//
//
//
//
// display functions
//
void printFleet(const ship[]); // prints the locations of all the ships and whether they
                               // are sunk
void printShip(ship); // prints the location and status (sunk or not) of a single ship
//
//
//
//
//
// battle functions
//
bool operational(const ship[]);  // returns true if at least one ship in the array
                                 // is not sunk
//
location fire(void);       // asks the user to input the coordinates of the next
                           // shot
//
// note that match() is also used in the game
void sink(ship&);          // sets "sunk" member variable of the ship to true
//
//
//
//
//
#endif /* BATTLESHIP_H_ */



// battleshipInput.cpp
// structure definitions and function prototypes
// for the battleship assignment
// christopher l. hawkins jr.
// 10/27/2010

#include "battleship.h"
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main(){

	char answer;
    ship fleet[FLEET_SIZE];
    location shot;

    srand(time(NULL));

    deploy(fleet);

    cout << "would you like a printout of the ships locations?:  [y/n]";
    cin >> answer;
    if (answer == 'Y' || answer == 'y')
    	printFleet(fleet);

    while (operational(fleet)){

    	shot = fire();

    	for (int i = 0; i <= FLEET_SIZE - 1; i++) {

    		if	(match(fleet[i], shot)) {
    			sink(fleet[i]);
    			cout << "Hit!";
    		}
    		else
    			cout << "Miss!";
    	}
    }
    cout << "Congratulations you sank all of the Battleships!";
    printFleet(fleet);
}

I made some changes and now it runs but it still puts all ships in the same loc and isn't seeing the sunk flag in operational() I think.

// battleship.cpp
// structure definitions and function prototypes
// for the battleship assignment
// christopher l. hawkins jr.
// 10/27/2010

#include "battleship.h"
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

// initialization functions
//
// places an array of battleships in
// random locations in the ocean
void deploy(ship deployShip[]) {

	int nextShipToDeploy = 0;
	location possibleLocation;

	while(nextShipToDeploy != FLEET_SIZE) {

		possibleLocation = pick();

		for (int i = 0; i <= nextShipToDeploy; i++) {

			if (match(deployShip[i], possibleLocation))
				break;
			else {
				deployShip[i].loc = possibleLocation;
				deployShip[i].sunk = false;
			}
		}
		nextShipToDeploy++;
	}
}
// generates a random location
location pick(void) {

	int x;
	char y;
	location gridLocation;

	x = rand() % FIELD_SIZE + 1;

	switch (rand() % FIELD_SIZE + 1) {
	case 1:
		y = 'a';
		break;

	case 2:
		y = 'b';
		break;

	case 3:
		y = 'c';
		break;

	case 4:
		y = 'd';
		break;

	case 5:
		y = 'e';
		break;

	default:
		break;
	}

	gridLocation.x = x;
	gridLocation.y = y;

	return gridLocation;
}
// returns true if this location matches
// the location of the ship
// returns false otherwise
bool match(ship tryShip, location tryLocation) {

	bool flag;

	if (tryShip.loc.x == tryLocation.x && tryShip.loc.y == tryLocation.y)
		flag = true;
	else
		flag = false;

	return flag;
}
//
// display functions
//
// prints the locations of all the ships and whether they
// are sunk
void printFleet(const ship battleFleet[]) {
	for (int i = 0; i <= FLEET_SIZE - 1; i++) {
		printShip(battleFleet[i]);
		cout << ", ";
	}
}
// prints the location and status (sunk or not) of a single ship
void printShip(ship battleShip) {
	cout << battleShip.loc.y << battleShip.loc.x << " ";
	if (battleShip.sunk = true)
		cout << "sunk";
	else
		cout << "up";
}
// battle functions
//
// returns true if at least one ship in the array
// is not sunk
bool operational(const ship operationalFleet[]) {
	bool flag = false;
	for (int i = 0; i <= FLEET_SIZE - 1; i++){
		if (operationalFleet[i].sunk == false){
			flag = true;
			return flag;
		}
	}
	return flag;
}
// asks the user to input the coordinates of the next
// shot
location fire(void) {
	location shot;

	cout << "Please enter the letter and number of your shot: ";
	cin >> shot.y >> shot.x;

	return shot;

}
// note that match() is also used in the game
//
// sets "sunk" member variable of the ship to true
void sink(ship& sinkShip) {
	sinkShip.sunk = true;
}
//
//
//



// battleship.h
// structure definitions and function prototypes
// for the battleship assignment
// christopher l. hawkins jr.
// 10/27/2010
//
#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_
//
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
//
// data structures definitions
//
const int FLEET_SIZE=5; // number of battleships
const int 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 deploy(ship[]); // places an array of battleships in
                     // random locations in the ocean
//
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
//
//
//
//
// display functions
//
void printFleet(const ship[]); // prints the locations of all the ships and whether they
                               // are sunk
void printShip(ship); // prints the location and status (sunk or not) of a single ship
//
//
//
//
//
// battle functions
//
bool operational(const ship[]);  // returns true if at least one ship in the array
                                 // is not sunk
//
location fire(void);       // asks the user to input the coordinates of the next
                           // shot
//
// note that match() is also used in the game
void sink(ship&);          // sets "sunk" member variable of the ship to true
//
//
//
//
//
#endif /* BATTLESHIP_H_ */



// battleshipInput.cpp
// structure definitions and function prototypes
// for the battleship assignment
// christopher l. hawkins jr.
// 10/27/2010

#include "battleship.h"
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main(){

	char answer;
    ship fleet[FLEET_SIZE];
    location shot;

    srand(time(NULL));

    deploy(fleet);

    cout << "would you like a printout of the ships locations?:  [y/n]";
    cin >> answer;
    if (answer == 'Y' || answer == 'y')
    	printFleet(fleet);

    while (operational(fleet)){

    	shot = fire();

    	for (int i = 0; i <= FLEET_SIZE - 1; i++) {

    		if	(match(fleet[i], shot)) {
    			sink(fleet[i]);
    			cout << "Hit!";
    		}
    		else
    			cout << "Miss!";
    	}
    }
    cout << "Congratulations you sank all of the Battleships!";
    printFleet(fleet);
}

Thank you for your help so far.

line 106 in battleship.cpp is not correct: if (battleShip.sunk = true) I don't know if that's the problem, but I just noticed it.

Also a quick aside: if(CONDITION) has a condition that is boolean, you don't need to test it versus true or false: if(battleship.sunk == true) is the same thing as if(battleship.sunk)

Thanks, that didn't fix all the problems with the program but it is moving me in the right direction.

It's definitely generating duplicate pairs and seems to be printing out the last value chosen five times when you ask for it to display the ships.

I'll keep looking it over, but the bugs are subtle. If you can narrow down where it is going awry that would be even better.

yeah I've noticed that bugs can be frustratingly subtle. I'm sure there is a problem with my inner loop in the deploy() function. I think I kinda need to turn it inside out if you understand what I mean.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.