Lost in the loops!
Jul 22, 2013 at 7:03pm UTC
I'm attempting to make a text-art fish tank that can hold multiple text-art fish in random positions. The displayTank() function works well other than the fact that the new fish replaces the latter. Opposed to having two or more fish in the tank, I only have one. How do I replace the empty tank with a more fish rather than replacing the old fish with the new one?
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
//Uses a vector to store different parts of the tank.
void displayTank()
{
tank_frame.resize(10);
//Build tank
for ( int j = 0; j < 10; j++ )
{
//Top
if ( j == 0 )
{
string top;
top = ' ' ;
for ( int i = 1; i < tank_size; i++ )
{
top += '_' ;
}
tank_frame[j] = top;
}
//Middle
else if ( j > 0 && j < 9 )
{
string middle;
middle = '|' ;
for ( int i = 1; i < tank_size; i++ )
{
middle += ' ' ;
}
middle += '|' ;
tank_frame[j] = middle;
}
//Bottom
else
{
string bottom;
bottom = '|' ;
for ( int i = 1; i < tank_size; i++ )
{
bottom += '_' ;
}
bottom += '|' ;
tank_frame[j] = bottom;
}
}
//Generate random numbers for fish positions in vector
if ( fish_collection.size() != 0 )
{
vector <int > randHeight;
vector <int > randWidth;
randHeight.resize(fish_collection.size());
randWidth.resize(fish_collection.size());
for ( int i = 0; i < fish_collection.size(); i++ )
{
srand (time(NULL));
randHeight[i] = rand() % 10 + 1;
randWidth[i] = rand() % (tank_size - 5) + 1;
}
for ( int j = 0; j < fish_collection.size(); j++ )
{
tank_frame[randHeight[j]].replace ( randWidth[j], fish_collection[j].size(), fish_collection[j] );
}
}
//Print the vector
for ( int j = 0; j < tank_frame.size(); j++ )
{
cout << tank_frame[j];
cout << endl;
}
}
void newFish()
{
string input;
cout << "Create new fish (up to 5 characters long):\n\n" ;
cin >> input;
if ( input.size() > 5 )
{
cout << "Fish is too big!\n\n" ;
}
else
{
fish_collection.push_back(input);
}
}
Last edited on Jul 22, 2013 at 7:05pm UTC
Topic archived. No new replies allowed.