The bunny exercise in the forum

I have been making the last exercise, which can be found here: http://www.cplusplus.com/forum/articles/12974/

I think many of you here know the task.. Its still not complete, but it aint working anyway. New bunnies arent born, although they should. When using the printhorde function, it prints out that everyone is a mutant bunny. The code itself is quite long now and therefore finding a erorr is getting harder and harder. I believe i have errors in syntax when using pointers. Any comments would be appreciated ;)

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <iostream>
#include <time.h>
#include <string>

using namespace std;

char *Names[15]={"Phill","Colin","Bucket","Fluffy","Mister","Lilly","Rose","Shamandale","Yoshie","Jane","PeanutButter","Plank","Sucubus","Broken","Horse Shit"};

class Bunny
{
public:
	string Sex;
	string Color;
	int Age;
	string Name;
	bool Mutant;

	Bunny *Next;

	int Random(int x)
	{
		return x=rand()%(x+1);
	}
	
	bool MutantBunny()
	{
		int X=Random(100);
		if(X<2)
		{
			return true;
		}
		return false;
	}

	string SexIsNotAChoise()
	{
		int R=Random(10);
		if(R<5)
		{
			Sex="Male";
		}
		else
		{
			Sex="Female";
		}
		return Sex;
	}

	string HopeImNotBlack()
	{
		int R=Random(100);
		if(R<24)
		{
			Color="White";
		}
		else if(R>24 && R<=49)
		{
			Color="Brown";
		}
		else if(R>50 && R<=75)
		{
			Color="Black";
		}
		else if(R>76 && R<=101)
		{
			Color="Spotted";
		}
		return Color;
	}

	int ImNotOld()
	{
		Age=0;
		return Age;
	}

	string HelloMyNameIs()
	{
		if(Mutant==true)
		{
			Name=Names[rand()%(14-10)+10];
		}
		else if(Sex=="Male")
		{
			Name=Names[rand()%5];
		}
		else if(Sex=="Female")
		{
			Name=Names[rand()%(9-5)+5];
		}
		return Name;
	}

	Bunny()
	{
		Mutant=MutantBunny();
		if(Mutant==true)
		{
			Sex=SexIsNotAChoise();
			Color="Green";
			Age=ImNotOld();
			Name=HelloMyNameIs();
		}
		else if(Mutant==false)
		{
			Sex=SexIsNotAChoise();
			Color=HopeImNotBlack();
			Age=ImNotOld();
			Name=HelloMyNameIs();
		}
		
	}

	void print()
	{
		cout<<"Bunny "<<Name<<" was born!";
		if(Mutant==true)
		{
			cout<<" He's a mutant..!"<<endl;
		}
		else
		{
			cout<<endl;
		}
	}

	~Bunny()
	{
		cout<<"A bunny with the name "<<Name<<" has died!"<<endl;
	}

};

class BunnyList
{
private:
	int size;
public:
	Bunny *Head;

	BunnyList()
	{
		size=0;
		Head=NULL;
	}

	int Count()
	{
		return size;
	}

	int AddBunny(Bunny* NewBunny)
	{
		if(Head==NULL)
		{
			Bunny *Temp=new Bunny;
			Temp=NewBunny;
			Temp->Next=Head;
			Head=Temp;
			return size++;
		}
		else
		{
			Bunny *Current=new Bunny;
			Current=Head;
			while(Current->Next!=NULL) 
			{
				Current=Current->Next;
			}
			Current->Next=NewBunny; 
			NewBunny->Next=NULL;
			return size++;
		}
	}

	Bunny *GetBunny(int Pos)
	{
		Bunny *Current=Head;
		for(int i=1;i<Pos && Current!=NULL;i++)
		{
			Current=Current->Next;
		}
		return Current;
	}

	bool Delete(int Pos)
	{
		if(GetBunny(Pos)==NULL)
		{
			return false;
		}
		else
		{
			if(Pos==1)
			{
				Bunny *Temp=Head->Next;
				Head=Temp;
				size--;
				return true;
			}
			else
			{
				GetBunny(Pos-1)->Next=GetBunny(Pos+1);
				size--;
			}
			return true;
		}
	}

	int MutantCheck()
	{
		int MutantCount=0;
		for(int i=1;i<Count() && GetBunny(i)!=NULL;i++)
		{
			if(GetBunny(i)->Mutant=true)
			{
				MutantCount++;
			}
		}
		return MutantCount;
	}

	int Male()
	{
		int M=0;
		for(int i=1;i>Count() && GetBunny(i)!=NULL;i++)
		{
			Bunny *Temp=new Bunny;
			Temp=GetBunny(i);
			if(Temp->Sex=="Male")
			{
				M++;
			}
		}
		return M;
	}

	int Female()
	{
		int F=0;
		for(int i=1;i<Count() && GetBunny(i)!=NULL;i++)
		{
			Bunny *Temp=new Bunny;
			Temp=GetBunny(i);
			if(Temp->Sex=="Female")
			{
				F++;
			}
		}
		return F;
	}

	bool WeWillMate()
	{
		int M=Male(),F=Female();
		if(M>=1 && F>=1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

};

void PrintTheHorde(BunnyList* List)
{
		cout<<"The Horde consists of "<<List->Count()<<" bunnies, here they are: "<<endl;
		cout<<endl;
		for(int i=1;i<List->Count()+1 && List->GetBunny(i)!=NULL;i++)
		{
			if(List->GetBunny(i)->Mutant=true)
			{
				Bunny *Temp=List->GetBunny(i);
				cout<<"Name: "<<Temp->Name<<" Sex: "<<Temp->Sex<<" Age: "<<Temp->Age<<" Color: "<<Temp->Color;
				if((Temp->Mutant)==true)
				{
					cout<<" Im a Mutant.."<<endl;
				}
				else if((Temp->Mutant)==false)
				{
					cout<<endl;
				}
			}
		}
}

int main()
{
	srand(time(NULL));
	BunnyList *List=new BunnyList;

	for(int i=0;i<5;i++)
	{
		Bunny *NewBunny=new Bunny();
		List->AddBunny(NewBunny);
		NewBunny->print();
	}

	while(List->Count()>0)
	{
		for(int i=1;i<List->Count()+1 && List->GetBunny(i)!=NULL;i++)
			{
				Bunny *Temp=new Bunny;
				Temp=List->GetBunny(i);
				Temp->Age++;
				if(Temp->Age>10)
				{
					List->Delete(i);
				}
			}

		bool C=List->WeWillMate();
		if(C==true)
		{
			for(int i=0;i<List->Female();i++)
			{
				Bunny *NewBunny=new Bunny();
				List->AddBunny(NewBunny);
				NewBunny->print();
			}
		}

		PrintTheHorde(List);
		system("pause");
		system("cls");
	}
	return getchar();
}
You are using = instead of == on line 215 and 274.
line 87 only needs the "else" and not the "else if"
and lines 245 to 249 you have only "if" and nothing to end the statement if its not true
line 87 only needs the "else" and not the "else if"


True, but there's no harm in having it.

and lines 245 to 249 you have only "if" and nothing to end the statement if its not true


An if need not be paired with an else and certainly in this case there is no reason to.
Your addBunny function doesn't return the size of your list. You must you a per-incriment: return ++size;.
Did some corrections, had some idiotic mistakes... now everything is running fine, except the deletion of list elements. If there are 5 female bunnies, no new bunnies arent burn and all of them should be dead when they reach 10 years, but they dont.. 3 of them die, then in the next one dies, and the last after two turns..

Is there something wrong with the Delete() function?

else if just makes things more clear to me, and i think 245-249 is just fine..

1
2
3
4
5
6
7
8
9
10
11
12
int Male()
	{
		int M=0;
		for(int i=1;i<Count()+1 && GetBunny(i)!=NULL;i++)
		{
			if(GetBunny(i)->Sex=="Male" && GetBunny(i)->Age>2)
			{
				M++;
			}
		}
		return M;
	}


Topic archived. No new replies allowed.