Fish Tank Classes

I need to make a fish tank class as part of a bigger fish project that is able to be cleared, drawn on, and printed. I'm not sure what I am doing wrong, but my height, width, row, and col are never initialized and the tank is never printed. I've attached my header file, .cpp file, and test .cpp file in that order.

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
 #ifndef FISHTANK_H
#define FISHTANK_H
#include <iostream>
using namespace std;

class FishTank {
private:
	static const int MAX_HGT = 50;
	static const int MAX_WID = 200;
	char tank_space[MAX_HGT][MAX_WID];
	int height;
	int width;
	int row_num;
	int col_num;
	char symbol;
public:
	FishTank(); 
	FishTank(int hgt, int wid); 
	bool set_height(int hgt); 
	bool set_width(int wid); 
	int get_height(); 
	int get_width();
	void clear_tank();
	void update_at(int row, int col, char c);
	void show_tank();

};

#endif  


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
 #include <iostream>
using namespace std;
#include "fishtank.h"

//Initializer
//Sets height, width, row, and column to 0
//Fills tank picture with blanks
//Parameters: none
//Returns: nothing

FishTank::FishTank()
{
	height = 0;
	width = 0;
	row_num = 0;
	col_num = 0;
	symbol = ' ';
	for(int i = 0; i < MAX_HGT; i++)
	{
		for (int j = 0; j < MAX_WID; j++)
		{
			tank_space[i][j] = ' ';
		}
	}
}

/*
Purpose: Sets the height and width to given values
		 Otherwise acts like empty constructor
Parameters: int hgt, int wid
Returns: None
*/

FishTank::FishTank(int hgt, int wid)
{
	height = hgt;
	width = wid;
	for(int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			tank_space[i][j] = ' ';
		}
	}
}

/*
Purpose: Sets height variable, returns true if valid (less than or equal
		 to max height of 50. returns false otherwise
Parameters: int hgt
Returns: bool
*/

bool FishTank::set_height(int hgt)
{
	if (height > 50) {
		return false;
	} else {
		hgt = height;
		return true;
	}
}

/*
Purpose: Sets width variable, returns true if valid (less than or equal 
		 to max width of 200. returns false otherwise
Parameters: int wid
Returns: bool
*/

bool FishTank::set_width(int wid)
{
	if (width > 200) {
		return false;
	} else {
		wid = width;
		return true;
	}
}

/*
Purpose: Gets height of tank
Parameters: none
Returns: int height
*/

int FishTank::get_height()
{
	return height;
}

/*
Purpose: Gets width of tank
Parameters: none
Returns: int width
*/

int FishTank::get_width()
{
	return width;
}

/*
Purpose: Clears the tank space and fill it with empty spaces
Parameters: none
Returns: none
*/

void FishTank::clear_tank()
{
	for(int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			tank_space[i][j] = ' ';
		}
	}
}

/*
Purpose: Checks first to see if entered row and col are valid
		 If they are, updates the character map at given row and 
		 column with given character
Parameters: int row, int col, char c
Returns: none
*/

void FishTank::update_at(int row, int col, char c)
{
	row_num = row;
	col_num = col;
	symbol = c;
	if ((row <= height) && (col <= width)) {
		tank_space[row][col] = c;
	}
}

/*
Purpose: Prints out the character map to cout
Parameters: none
Returns: none
*/

void FishTank::show_tank()
{
	for(int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			cout << tank_space[i][j] << endl;
		}
	}
} 


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
 #include <iostream>
#include "fishtank.h"
using namespace std;

int main()
{
	FishTank fishtank1, fishtank2;

	fishtank1.set_height(-1);
	fishtank1.set_width(-1);

	cout << "Height: " << fishtank1.get_height() << endl;
	cout << "Height should be invalid" << endl;
	cout << "Width: " << fishtank1.get_width() << endl;
	cout << "Width should be invalid" << endl;

	fishtank1.set_height(50);
	fishtank1.set_width(200);

	cout << "Height: " << fishtank1.get_height() << endl;
	cout << "Height should be: 50" << endl;
	cout << "Width: " << fishtank1.get_width() << endl;
	cout << "Width should be: 200" << endl;

	fishtank1.set_height(10);
	fishtank1.set_width(10);
	fishtank1.show_tank();

	cout << "Height: " << fishtank1.get_height() << endl;
	cout << "Height should be: 10" << endl;
	cout << "Width: " << fishtank1.get_width() << endl;
	cout << "Width should be: 10" << endl;

	fishtank1.update_at(5, 5, 'a');
	fishtank1.show_tank();


	return 0;

} 


Last edited on
Please edit your post and use code tags its currently unreadable - http://www.cplusplus.com/articles/jEywvCM9/
I apologize! I'm new to this site. I believe it should be readable now
You got these mixed up -
1
2
3
hgt = height;

 wid = width;


Should be the other way around right? Think about it.

1
2
height = hgt;
width = wid;
Oh, thank you - that does make sense.
However, when I switch these around and run my testtank.cpp, the height and width are always reported as being 0, when they should be different values. Is that a problem with my code or my syntax in my test function?
All I did was switch them around and this is the output

http://gyazo.com/a8ad433800ff46274a231e157380e669

Works fine... Double check that youve actually switched them around, make sure they look like this

1
2
height = hgt;
width = wid;


In both the set functions.
Hmm...odd. I compile by using clang++ -Wall -Wextra testtank.cpp fishtank.cpp

All of the results show a 0 and I am sure I switched those two variables in the set_height function as well as the set_width function

In addition, when compiling with g++, I get an warning message saying that parameters 'wid' and 'hgt' were set but not used.
Last edited on
Honestly Its a bit weird that the set functions are bool, they should be integers. Try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int FishTank::set_height(int hgt)
{
	if (hgt <= 50)
	{
		height = hgt;
		return height;
	}

}

int FishTank::set_width(int wid)
{
	if (wid <= 200)
	{
		height = wid;
		return wid;
	}
}


P.S. Dont forget to change the prototypes in the class aswell.
Last edited on
That is what I thought to do at first, but the project guidelines require these functions to be bool. They are meant to "to set the corresponding state variables. The function returns true if the values are valid and returns false if they are not valid."
Topic archived. No new replies allowed.