Help on creating multiple objects in loops

Here is the assignment in trying to complete(not homework, just trying to practice): The crossed out lines are what i have currently completed


Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)
arrays
pseudo random number generation
strings & string functions
functions
structures/classes
enumerated data
file input/output
pointers
sorting
linked lists
advanced classes

Write a program that creates a linked list of bunny objects.
Each bunny object must have
Sex: Male, Female (random at creation 50/50)
color: white, brown, black, spotted
age : 0-10 (years old)
Name : randomly chosen at creation from a list of bunny names.
radioactive_mutant_vampire_bunny: true/false (decided at time of bunny creation 2% chance of true)

At program initialization 5 bunnies must be created and given random colors.
Each turn afterwards the bunnies age 1 year.
So long as there is at least one male age 2 or older, for each female bunny in the list age 2 or older;
a new bunny is created each turn. (i.e. if there was 1 adult male and 3 adult female bunnies, three new bunnies would be born each turn)
New bunnies born should be the same color as their mother.
If a bunny becomes older than 10 years old, it dies.
If a radioactive mutant vampire bunny is born then each turn it will change exactly one non radioactive bunny into a radioactive vampire bunny.
(if there are two radioactive mutant vampire bunnies two bunnies will be changed each turn and so on...)
Radioactive vampire bunnies are excluded from regular breeding and do not count as adult bunnies.
Radioactive vampire bunnies do not die until they reach age 50.
The program should print a list of all the bunnies in the colony each turn along w/ all the bunnies details, sorted by age.
The program should also output each turns events such as
"Bunny Thumper was born!
Bunny Fufu was born!
Radioactive Mutant Vampire Bunny Darth Maul was born!
Bunny Julius Caesar died!
The program should write all screen output to a file.
When all the bunnies have died the program terminates.
If the bunny population exceeds 1000 a food shortage must occur killing exactly half of the bunnies (randomly chosen)

★ Modify the program to run in real time, with each turn lasting 2 seconds, and a one second pause between each announement.

★★ Allow the user to hit the 'k' key to initiate a mass rabit cull! which causes half of all the rabits to be killed (randomly chosen).

★★★★ Modify the program to place the rabits in an 80x80 grid. Have the rabits move one space each turn randomly.
Mark juvenile males with m, adult males w/ M,
juvenile females w/ f, adult femails w/ F
radioactive mutant vampire bunnies with X

Modify the program so that radioactive mutant vampire bunnies only convert bunnies that end a turn on an adjacent square.
Modify the program so that new babies are born in an empty random adjacent square next to the mother bunny. (if no empty square exits then the baby bunny isn't born)

★★★★★ Modify the program so that it saves each turn to a file and can play back at accelearted speed all subsequent turns.


MY PROBLEM: In main.cpp i want to create a loop and counts bunnies in the program, decides if there are males and females over 2, then create a bunny object accordingly. How would i go about doing that? how would i code a loop that will create new bunny objects on the spot when condition are right? or do i need to create 1000 dynamically allocated bunny object before the loop runs(max population allowed for bunnies is 1000) and just add in a bunny to the population everytime the conditions are met? Thank you very much for the help!

Here's what i have so far:
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "Bunny.h"

int main()
{
    bool Running = true;
    Bunny BunnyMan[5];
    while(Running)
    {
        //run code
        //if there are a male and female over 2, create another bunny object
        //etc...etc...
        //if no bunnies, running = false
    }
}


bunny.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef BUNNY_H
#define BUNNY_H
#include <string>

class Bunny
{
    static const char * BunnyNamesMale[10]; //Male names for bunnies
    static const char * BunnyNamesFemale[10]; //Female names for bunnies
    public:
        Bunny();
        void SetColor(int ); //set color using a random allocated int int in constructor
        void SetName(int, char); //set a random name, uses a random allocated int and a char to
        //choose either a male or female name from the arrays.
    private:
    char Sex; //gender, (M)ale and (F)emale
    int age; //starts at 0
    std::string Name; //random
    bool RadMut; // 2% chance of a mutant bunny being born
    std::string Color; // 4 color choices
};

#endif // BUNNY_H


bunny.cpp
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
#include "Bunny.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
const char * Bunny::BunnyNamesMale[10] = {"JIMMY", "HON", "DARTH","GRANT", "MILES", "NICK", "JOHN", "TOM", "NICK", "SAWYER"};
const char * Bunny::BunnyNamesFemale[10] = {"MARTHA", "MARGE", "LAURA", "KARRY", "KELLY", "CATHERINE", "MAU5", "FEMALE", "WHODAT", "FIFA"};
//set arrays to the specified names for random selection
Bunny::Bunny():
age(0),
RadMut(false)
{
    static bool once = true;
    if(once){
        srand(time(NULL));
        once = false;
    }
     //seed time
    int * Random= new int[4]; //allocate space for 4 random ints to be created and used
    Random[0] = rand() % 2 + 1; //GENDER
    Random[1] = rand() % 4 + 1; //COLOR
    Random[2] = rand() % 100 + 1; //MUTANT
    Random[3] = rand() % 10; //NAME
    if(Random[0] == 1) //if MALE
    {Sex = 'M';
     SetName(Random[3],'M');
    }
    else //if FEMALE
    {Sex = 'F';
     SetName(Random[3],'F');
    }

    if(Random[2] < 3){RadMut = true;} //out of 100 different numbers that could have been randomly
    //chosen, if the number is less than 2(2% chance) then the bunny is mutant
    std::cout << Name << " " << Sex <<" " << Color <<" " << RadMut <<" \n";
    //display information on bunnies(temporary)
    delete [] Random; //deallocate memory
}


void Bunny::SetColor(int R2) //second random int
{
    switch(R2) //R2 - random int passed into function to determine color
    {
        case 1:
            Color = "White";
            break;
        case 2:
            Color = "Brown";
            break;
        case 3:
            Color = "Black";
            break;
        case 4:
            Color = "Spotted";
            break;
    }
}

void Bunny::SetName(int R4, char MF) //fourth random int
{
    int p = 0; //declare outside of block so that it stays initialized throughout the function
            for(p = 0; p <= R4;p++){ //cycle through numbers
                    if(p == R4 && MF == 'M'){ //if they match and is a male
                        Name = BunnyNamesMale[p]; //set the name
                    }else if(p == R4 && MF == 'F'){ //if they match and is a female
                        Name = BunnyNamesFemale[p]; //set the name
                    }
            }

}

Last edited on
You didn't make a linked list. You really want this for when you have to delete bunnies.

You want to figure out if there is a non-mutant male over 2, and if there is, get the count of the non-mutant females over 2 and then create that many new bunnies.
Last edited on
could you link me to a tutorial for creating and modifying linked lists? i believe Ive never looked over that topic before. Thanks for the reply
Learning linked lists right now, thanks for the help! im still quite confused on how i would go about dynamically creating objects during the program when a baby of the population is born. Do i create a thousand bunny objects before the program starts running? or how? Thanks again!
still confused on this subject
Topic archived. No new replies allowed.