Problem with C++ using classes

this is what I got so far: it's got some issues =(
Not allowed to use: vectors, grids, the .h is pretty much fixed as is




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
#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_


// coordinates (location) of the ship and shots
class location{
public:
  void locationreset(void); // void constructor, assigns -1 to X
  void pick(void); // picks a random location

  void fire(void); // asks the user to input the
                   // coordinates of the next shot
  void print(void) const; // prints location in format "a1"

  // returns true if the two locations match
  friend bool compare(location, location); 

private:
  static const int fieldSize=5; // the field (ocean) is fieldSize X fieldSize
  int x;  // 1 through fieldSize
  char y; // 'a' through fieldSize
};

// contains ship's coordinates (location) and whether is was sunk
class ship{
public:
  ship(void); // void constructor, sets sunk=false
  bool match(location) const; // returns true if this location matches
                              // the ship's location
  bool isSunk(void) const {return(sunk);}; // checks to see if the ship is sunk
  void sink(void);       // sets "sunk" member variable of the ship to true
  void setLocation(location); // deploys the ship at the specified location
  void printShip(void) const; // prints location and status of the ship

private:
  location loc;
  bool sunk;
};

// contains the fleet of the deployed ships
class fleet{
public:
  void deployFleet(void); // deploys the ships in random locations
                          // of the ocean
  bool operational(void) const; // returns true if at least
                                // one ship in the fleet is not sunk
  bool isHitNSink(location); // returns true if there was a deployed
                        // ship at this location (hit) and sinks it
                        // otherwise returns false (miss)
  void printFleet(void) const; // prints out locations of ships in fleet

private:
  static const int fleetSize=5; // number of battleships
  bool check(location);          // returns true if one of the ship's locations
                                // matches the ship, returns false 
                                // if none-match
  ship ships[fleetSize];        // battleships of the fleet
};

#endif /* BATTLESHIP_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
#include "battleship.h"
#include <iostream>

using std::cout; using std::cin; using std::endl;

int main (){
	char answer;	
	fleet myFleet;	//computer's ships
	myFleet.deployFleet(); // fleet is deployed at random locations
	cout << "Do ships' positions and status need to be printed? (y for yes)" << endl;
	cin >> answer;
	if (answer=='y')
		myFleet.printFleet();
	while (myFleet.operational ()==true){
		cout << "there are still ships up";
		location userShot;
		userShot.locationreset();
		cout << "Input location(a-e 1-5): ";
		userShot.fire();
		if (myFleet.isHitNSink(userShot)==true)
			cout << "hit";
		else
			cout << "miss";
		cout << "Do ships' positions and status need to be printed? (y for yes, q to quit)";
		cin >> answer;
		if (answer== 'y')
			myFleet.printFleet();
		else if (answer=='q')
			break;
	}
}

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 <ctime>
#include <cstdlib>
#include "battleship.h"

using std::cout; using std::endl; using std::cin;

void location::locationreset(void){
	x=-1;
	y='x';
}

void location::pick(void){
	srand(time(NULL));
	x=rand()%fieldSize +1;
	y=rand()%fieldSize;
	switch (y) { 
	case 0: y = 'a'; break;
	case 1: y = 'b'; break;
	case 2: y = 'c'; break;
	case 3: y = 'd'; break;
	case 4: y = 'e'; break;
	}
}	

void location::fire(void){
	cout << "input the coordinates of the next shot "; cin >> y >> x;

}
void location::print() const{
	cout << y << x;
}
bool compare(location loc1, location loc2) {
	return (loc1.x == loc2.x && loc1.y == loc2.y);
}

ship::ship(void){
	sunk=false;
}

bool ship::match(location shiploc) const {
	return compare(loc,shiploc);
}

void ship::sink(void) {
	sunk=true;
}

void ship::setLocation(location shiploc) {	 // deploys the ship at the specified location
	loc=shiploc;
}

void ship::printShip(void) const {    // prints location and status of the ship
	loc.print();
	if (sunk ==true)
		cout <<"sunk";
	else if (sunk == false)
		cout <<"not sunk"<<endl;
}

void fleet::deployFleet(void) { // deploys the ships in random locations of the ocean
	location loc; loc.locationreset();
	for(int i = 0; i < fleetSize; i++){
		loc.pick();
		ships[i].setLocation(loc);
	}

}

bool fleet::operational() const{ // returns true if at least one ship in the fleet is not sunk
	for(int i = 0; i < fleetSize; ++i)
		if (ships[i].isSunk() == false) 
			return true;
	return false;

}

bool fleet::isHitNSink(location shiploc){	// returns true if there was a deployed
	for(int i = 0; i < fleetSize; ++i){
		if(ships[i].match(shiploc) == true && ships[i].isSunk()==false)
		{
			ships[i].sink();
			return true;
		}
		else
			return false;
	}			
}

void fleet::printFleet(void) const{ // prints out locations of ships in fleet
	for(int i = 0; i < fleetSize; ++i)
		ships[i].printShip();
}

bool fleet::check(location shiploc){
	for (int i=0; i<fleetSize; ++i) 
		if (ships[i].match(shiploc))
			return true;
}
		else return false;
}
Last edited on
Use the [code] tags instead of the [output] tags, please edit your post.

Also, it's against the rules to include the word "help" in your topic title. It's just a misdemeanor, though.
Last edited on
mkay, what next?
Next we need your question. What are you asking for help with? What's your problem?
most of the commands for fleet class are messed up(lines 61-102 on last code block), it compiles without errors, but i have mistakes in the code, just very buggy idk?

and im getting same coordinate 5x, example:

e5 not sunk
e5 not sunk
e5 not sunk
e5 not sunk
e5 not sunk

Last edited on
1
2
3
4
void fleet::printFleet(void) const{ // prints out locations of ships in fleet
	for(int i = 0; i < fleetSize; ++i)
		ships[i].printShip();
}

I believe that is causing e5 not sunk to be printed 5 times.
FleetSize == 5, and you are looping 5 times, calling the cout function.
The problem is that you are calling srand multiple times, resetting the random sequence to the beginning every time. Call srand once at the start of main, and never again.
Topic archived. No new replies allowed.