Hi all. I'm having trouble creating a program that creates a fish tank with which fish can be added to. The function below is kind of a fish tank builder. I haven't gotten too involved in vectors until starting this program. I'm attempting to use a 2D vector to put the pieces of the fish tank in, so that they can be edited when adding a fish. But it keeps crashing.
//This function will be called in order to fill a two dimensional array that
//represents a fish tank and then print it.
void displayTank()
{
//Adjusting the size.
tank_frame.resize(10);
for ( int i = 0; i < 11; i++ )
{
tank_frame[i].resize(tank_size);
}
//Append first line
for (int j = 0; j < 11; j++)
{
tank_frame[0][0] = ' ';
if ( j == 0 )
{
for ( int i = 0; i < tank_size; i++ )
{
tank_frame[j][i] = '_';
}
}
//Append middle lines
if ( j > 0 && j < 11 )
{
tank_frame[j][0] = '|';
for ( int i = 0; i < tank_size; i++ )
{
tank_frame[j][i] = ' ';
}
tank_frame[j][6] = '|';
}
}
//Print tank
for ( int j = 0; j < tank_frame.size(); j++ )
{
for ( int i = 0; i < tank_frame[j].size(); i++ )
{
char print = tank_frame[j][i];
cout << print;
}
}
}
Oh! I see. Thank you very much. I'm having another problem when writing to elements of the vector using a variable and a constant. If I write line 14 like this:
tank_frame[j][0] = ' ';
It will not print the way it does when it's written like the OP even though at that time j is equal to 0.