Soo... After learning some more C++ I decided to write the "Graduation" program from http://www.cplusplus.com/forum/articles/12974/ .
But I am stuck at very begining; This code below compiles but program instantly crashes.
The output, however, does indicate that something is going wrong with colour selection. In your code, it is possible to read color[4]; however, that is off the end of the array, so it's possible to read the wrong memory. This is something that can lead to a segFault. Same with reading the name.
Mmm this is getting weird. Sometimes crashes, sometimes works. Also ideone.com outputed string from name_list as value in color string, which is pretty impossible if you read code carefully.
Need more help. I have problems with creating new bunnies onto array of bunny objects. Also I figured that when bunnies die, and I delete their object, there will be empty spot in array. How should I shift back all other bunnies to prevent those holes?
Ok I know how does this look. But I swear it sounded like brilliant idea before i got to computer and compiled it... :D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
int main(){
//Init/Start game
srand(time(NULL));
int num_of_bunnies = 5;
bunny *pBunny = new bunny[num_of_bunnies];
//Create new bunnies each turn if its possible?
for(int i=0; i<num_of_bunnies; i++){
if((*pBunny[i]->getSex() == "male") && (*pBunny[i]->getAge() >=2)){
for(int i=0; i<num_of_bunnies; i++){
if((*pBunny[i]->getSex() == "female") && (*pBunny[i]->getAge() >=2)){
num_of_bunnies += 2;
//bunny *pBunny = new bunny[num_of_bunnies];
}
}
}
}
return 0;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class bunny{//Bunny class file
public:
bunny();
~bunny();
void setAge() {age++;}
int getAge() {return age;}
string getSex() {return sex;}
private:
protected:
int age;
bool isvamp;
string color;
string sex;
string name;
};
EDIT:
If I do this, program compiles, but I cant add any new bunnies to array!
As Hanst99 indicates, if you want your arrays to be flexible and expandable and able to have bits deleted and so forth, you're crying out for proper C++ containers.