I am trying to complete the zombie rabbit program I found in one of the forums here as a practice problem. I have created a class that has functions that set name, age, sex, color and radioactivity. I have been able to create objects using the functions and store them in a list. I am able to get this:
Will male 0 brown No
Susan female 0 black No
Susan female 0 brown No
Mary female 0 black No
Will male 0 black Yes
I also write it to a file to keep track of all the bunnies created. I have hit a wall when I have to increase the age. Basically I am trying to iterate through a list of objects to change a single element. I've looked at several forums on this site and get stuck when I try to replace the 0 with a 1. both using the replace function or setting up an if statement that would compare the element to 0 and if it is equal to replace it. I know, or at least believe, that you can't use the == operator when iterating through a list. I'm still determined to complete this project, can anyone point me in right direction?
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <list>
#include <ctime>
class Bunnies {
public:
void setAge(int newage) {age = newage;}
int getAge() const {return age;}
private:
int age;
};
int main()
{
using std::cout;
using std::endl;
std::list<Bunnies> population;
srand(time(NULL));
for (int i=0; i<5; i++) {
Bunnies test;
test.setAge(rand() % 10);
population.push_back(test);
}
// Is there's some Bunnies younger then one year?
for (constauto it : population)
cout << it.getAge() << ' ';
cout << endl;
// Let's check for very young Bunnies and make them older
for(auto& it : population) {
if( it.getAge() == 0) {
it.setAge(1);
}
}
// Again: is there's some Bunnies younger then one year?
for (constauto it : population)
cout << it.getAge() << ' ';
cout << endl;
return 0;
}
Thank you for the response. There are some parts of your code I'm not familiar with so give me some time to review it and implement it. I'll let you know as soon as I try it.