all rabbits have same value

linked list of rabbit objects,but it seems like all the rabbits have the same values even though they were created after each other any idea why?

thanks

here is the output

0
Grey
1
20
0
Grey
1
20
0
Grey
1
20


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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  #include <iostream>
#include <ctime>
#include <cstdlib>


using namespace std;

class Rabbit{

   public:
       string name;
       string color;
       int age;
       bool friendly;
       int happiness;

   public:
    Rabbit(){
       srand(time(0));
       int random1 = 1+(rand()%4);

       if(random1 == 1){

        color = "Black";
       }
       if(random1 == 2){

        color = "Grey";
       }
       if(random1 == 3){

        color = "White";
       }
       if(random1 == 4){

        color = "Brown";
       }
       int random2 = 1+(rand()%4);
       if(random2 == 1){

        friendly = false;
       }
       if(random2 == 2){

        friendly = false;
       }
       if(random2 == 3){

        friendly = true;
       }
       if(random2 == 4){

        friendly = false;
       }
       age = 0;
       happiness = 20;
    }

    void nextYear(){
     age++;
    }

    void feed(int amount){

       if(amount > 1 || amount  <5){

        happiness+=2;
       }
       if(amount > 5 || amount < 10){

        happiness+=5;
       }
       if(amount > 10 || amount < 20){

        happiness+=10;
       }
    }

     Rabbit *next;

};


Rabbit *head = NULL;

void addRabbit(){

       Rabbit* rabbit = new Rabbit;
       rabbit->next = head;
       head = rabbit;
}

void printRabbits(){

    Rabbit *current = head;
    while(current != NULL){

        cout << current->age << endl;
        cout << current->color << endl;
        cout << current->friendly << endl;
        cout << current->happiness << endl;

        current = current->next;

    }
}

int main()
{
    addRabbit();
    addRabbit();
    addRabbit();

    printRabbits();
}
Don't call srand(time(0)) more than once per program. Call it at the beginning of main(), and no where else. Delete it from your rabbit constructor.
thanks Ganado much appreciated,

how would I generate random values for each object that is created such as in the constructor?

thanks
Last edited on
Not exactly sure what you mean, because it looks like you already are, for at least color and friendly (once you move srand out of the constructor and into main). But you are still assigning age = 0; and happiness = 20; each time. If you want those to be random as well, assign them as a random number, like age = rand() % 10 + 1; or something.

You also could just have one comparison instead of four:
1
2
int random2 = rand() % 4 + 1; // generates either 1, 2, 3, 4
friendly = (random2 == 3); // assigns true to friendly if random2 == 3, false otherwise. 


Also, you have some errors in boolean logic.

amount > 1 || amount <5
I believe you mean to do amount > 1 && amount < 5.

amount > 1 || amount < 5 will always evaluate to true, since it's the union of everything about 1 and everything below 5. Try it with some example values like amount = 0.

Edit: Fixed a mistake in my boolean statements
Last edited on
thanks Ganado very good point it should be && nice spot =)

the code now seems to run perfect after moving srand() to main just wondering how come you can only call srand() once and when I called it from inside the constructor how come it gave me the same results for each object?

thanks
Hi Ganado I found the answer I'll post it here in case anyone else stumbles upon this thread

anyway Srand () takes a seed,Rand uses this seed to produces a set of random values based on the seed now in my program the seed was always the same because the code was being executed in the same second which will give the same seed so the sequence of random values will always be the same

as Ganado said to avoid this just set the seed once in the application,and you should be fine but note that for short applications such as command line applications like mine if you were to by chance run two instances of the program in the same second the seed would be the same so it would product the same sequence of values

Topic archived. No new replies allowed.