I fairly new to C++ and I am constructing a program that is to simulate a colony of bunnies. The program will be able to automatically add bunnies, give them names, ages, color etc. I am looking for advice on how I could change my code so that the print function will work outside of the while loop.
What I have tried:
#include <iostream>
#include <ctime>
#include <vector>
#include <cstdlib>
using namespace std;
const int POSSIBLE_NAMES = 18;
const int POSSIBLE_COLORS = 4;
static std::string possibleNames[] ={
"Jen",
"Alex",
"Janice",
"Tom",
"Bob",
"Cassie",
"Louis",
"Frank",
"Bugs",
"Daffy",
"Mickey",
"Minnie",
"Pluto",
"Venus",
"Topanga",
"Corey",
"Francis",
"London",
};
static std::string possibleColors[] ={
"White",
"Brown",
"Black",
"Spotted"
};
struct Bunny
{
public:
string name;
int age;
string color;
char sex;
Bunny(){
setSex();
setColor();
setAge(0);
setName();
}
int randomGeneration(int x){
return rand() % x;
}
void setSex()
{
int randomNumber = randomGeneration(2);
( randomNumber == 1 ) ? sex = 'm' : sex = 'f';
}
char getSex()
{
return sex;
}
void setColor()
{
int randomNumber = randomGeneration(POSSIBLE_COLORS);
color = possibleColors[randomNumber];
}
string getColor()
{
return color;
}
void setAge(int age)
{
this->age = age;
}
int getAge()
{
return age;
}
void setName()
{
int i = randomGeneration(POSSIBLE_NAMES);
name = possibleNames[i];
}
string getName()
{
return name;
}
void printBunny()
{
cout << "Name: " << getName() << endl;
cout << "Sex: " << getSex() << endl;
cout << "Color: " << getColor() << endl;
cout << "Age: " << getAge() << endl;
}
};
int main()
{
int i;
vector< Bunny > colony;
while(i > 6)
{
colony.push_back( Bunny() );
colony[i].printBunny(); cout << endl;
i++;
}
return 0;
}