Stuck on the bunny challenge

Hello All,

I am working through the bunny challenge problem from post 6281 (I think). I am making good progress IMHO, but I am stuck with two areas where I keep getting segmentation erros during debugging and I can't figure out why.

I have posted my code below (sorry it is quite long), but I do not know how to include code line numbers in the forum so I will try to explain.

The first is in the rlist constructor (in list.cpp). I have commented the delete out to overcome the problem, but I would prefer not to. I am not sure if commenting it out will cause memory leaks since this is a local variable.

The second is in rabbit.cpp (string rabbit::getfname()), when I get to "return fname" in the debugger, it seems to get stuck in some sort of infinite loop and I need to shut down and restart codeblocks.

If there is anyone who could provide some insight, I would really appreciate your feedback.

Main.cpp

#include <ctime>
#include <cstdlib>
#include "list.h"

using namespace std;

int main()
{
srand(time(0));

rlist *blist = new rlist();
blist->printlist();
}

===========================================================================

list.h

#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#include "rabbit.h"

class rlist
{
private:
int populationsize;
int number_of_mutants;
int number_of_juvenile_males;
int number_of_juvenile_females;
//int number_of_adult_males;
//int number_of_adult_females;
public:
rabbit *head;
rlist();
int populationsizereport() {return populationsize;};
int add(rabbit *newrabbit);
void printlist();
};

#endif // LIST_H_INCLUDED

===========================================================================

list.cpp
#include "list.h"
#include "rabbit.h"
#include "utils.h"

#define STARTINGAMOUNT 5

rlist::rlist()
{
int i;
rabbit *tempr = NULL;
for (i = 0; i < STARTINGAMOUNT; i++)
{
tempr = new rabbit;
tempr->setsex();
tempr->setfname(tempr->getsex());
tempr->setmutant();
tempr->setcolour();
add(tempr);
}
//delete tempr;
}

int rlist::add(rabbit *newrabbit)
{
rabbit *temp = new rabbit;
temp = newrabbit;
temp->next = head;
head = temp;
if (temp->getsex()) number_of_juvenile_males++; else number_of_juvenile_females++;
if (temp->getmutant()) number_of_mutants++;
delete temp;
return populationsize++;
}

void rlist::printlist()
{
rabbit *temp = head;

while (temp != NULL)
{
printdata(temp);
temp = temp->next;
}
}

===========================================================================

rabbit.h

#ifndef RABBIT_H_INCLUDED
#define RABBIT_H_INCLUDED
#include <string>

using namespace std;

class rabbit
{
private:
int age;
bool sex, mutant, spotted, alive;
string fname, sname, bcolour, scolour;
public:
rabbit *next;
string setfname(bool sex);
string getfname();
void setsex();
void setmutant();
void setcolour();
bool getsex();
bool getmutant();
bool getspotted();
string getbcolor();
string getscolor();
};

#endif // RABBIT_H_INCLUDED

===========================================================================

rabbit.cpp

#include "rabbit.h"
#include "utils.h"
#include <iostream>
#include <string>

using namespace std;

const string bfnames[] = {"Andrew","Brett","Carl","Derrick","Eric","Fred","Garth","Henry","Ian","James","Karl","Leon","Mark","Neil","Oilver","Patrick","Quniton","Robert","Sean","Trevor","Victor","Warren"};
const string gfnames[] = {"Amy","Bernice","Carol","Dedre","Eunice","Francis","Gail","Harriet","Ilse","Jenny","Karen","Lesley","Mary","Naomi","Odette","Patricia","Quance","Rachel","Sally","Tammy","Verity","Wendy"};
const string snames[] = {"Abrahams","Bartello","Cabanis","Dabbadie","Eades","Faas","Gabany","Haack","Iocavelli","Jabezmt","Kable","Labar","Maak","Nabors","Oak","Pabodie","Quakenbush","Raab","Saathoff","Tabaka","Ubelhor","Vacik","Wach","Yaddow","Zabel"};
const string furcolours[] = {"Black","Brown","White","Gray","Tan"};


string rabbit::setfname(bool sex)
{
int temp;
if (sex)
{
fname = bfnames[random_int(sizeof(bfnames)/sizeof(bfnames[0]))];
}
else
{
fname = gfnames[random_int(sizeof(gfnames)/sizeof(gfnames[0]))];
}
return fname;
}

void rabbit::setsex()
{
sex = random_bool();
}

bool rabbit::getsex()
{
return sex;
}

void rabbit::setmutant()
{
mutant = random_bool_prob(2,100);
}

bool rabbit::getmutant()
{
return mutant;
}

void rabbit::setcolour()
{
bcolour = furcolours[random_int(sizeof(furcolours)/sizeof(furcolours[0]))];
scolour = furcolours[random_int(sizeof(furcolours)/sizeof(furcolours[0]))];
if (bcolour == scolour) {spotted = false;} else {spotted = true;}
}

bool rabbit::getspotted()
{
return spotted;
}

string rabbit::getbcolor()
{
return bcolour;
}

string rabbit::getscolor()
{
return scolour;
}

string rabbit::getfname()
{
return fname;
}

===========================================================================

utils.h


#ifndef UTILS_H_INCLUDED
#define UTILS_H_INCLUDED
#include "rabbit.h"

#define BLACK 0
#define BLUE 1
#define GREEN 2
#define CYAN 3
#define RED 4
#define MAGENTA 5
#define BROWN 6
#define LIGHTGREY 7
#define DARKGREY 8
#define LIGHTBLUE 9
#define LIGHTGREEN 10
#define LIGHTCYAN 11
#define LIGHTRED 12
#define LIGHTMAGENTA 13
#define YELLOW 14
#define WHITE 15
#define BLINK 128

int random_int(int max);
bool random_bool();
bool random_bool_prob(int prob,int max);
void set_text_colour(int fcol,int bcol);
void printdata(rabbit *newrabbit);

#endif // UTILS_H_INCLUDED

===========================================================================

utils.cpp

#include "utils.h"
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <iomanip>
#include <iostream>

int random_int(int max)
{
return rand() % max;
}

bool random_bool()
{
return rand() % 2 == 1;
}

bool random_bool_prob(int prob,int max)
{
return ((rand() % (max+1)) < prob);
}

void set_text_colour(int fcol,int bcol)
{
HANDLE screen;
int color_attribute;

screen = GetStdHandle(STD_OUTPUT_HANDLE);
color_attribute = fcol + bcol*16;
SetConsoleTextAttribute(screen,color_attribute);
}

void printdata(rabbit *newrabbit)
{
if (newrabbit->getsex()) set_text_colour(LIGHTBLUE,BLACK); else set_text_colour(LIGHTMAGENTA,BLACK);
cout << setw(15) << newrabbit->getfname();
set_text_colour(LIGHTGREY,BLACK);

if (newrabbit->getmutant())
{
set_text_colour(WHITE,LIGHTRED);
cout << " is a mutant! ";
set_text_colour(LIGHTGREY,BLACK);
}
else
{
set_text_colour(LIGHTGREEN,BLACK);
cout << " is not a mutant! ";
set_text_colour(LIGHTGREY,BLACK);
}
cout << ": Fur colour = " << newrabbit->getbcolor();
if (newrabbit->getspotted()) {cout << ": Spot colour = " << newrabbit->getscolor();}
cout << endl;
}
Topic archived. No new replies allowed.