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.
#include <iostream>
usingnamespace 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) {
returnfalse;
} else {
hgt = height;
returntrue;
}
}
/*
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) {
returnfalse;
} else {
wid = width;
returntrue;
}
}
/*
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;
}
}
}
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?
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."