Begginer Excercise

closed account (10oTURfi)
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.

Help :)
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
#include <iostream>
#include <time.h>
#include <string>
#include <cstdlib>
using namespace std;

class bunny{
public:
	bunny();
	void demo(){ cout << age << " " << color << " " << isvamp << " " << name << " " << sex << endl; }
private:
	int age;
	bool isvamp;
	string color;
	string sex;
	string name;
protected:
};

bunny::bunny(){
	//Lists, consts and rand stuff
	const int name_num = 4;
	int temp;
	string name_list[name_num] = { "Pero", "Jozo", "Darko", "Ivan" };
	string color_list[4] = { "White", "Black", "Brown", "Spotted" };

	//Init age
	age = 0;

	//Init isvamp
	temp = rand() % 100+1;
	if(temp == 1 || (temp == 2))
		isvamp = true;
	else isvamp = false;

	//Init sex
	temp = rand() % 2+1;
	if(temp == 2)
		sex = "female";
	else sex = "male";

	//Init name
	temp = rand() % name_num+1;
	name = name_list[temp];
	
	//Init color
	temp = rand() % 4+1;
	color = color_list[temp];
}
	

int main(){
	//Init rand
	srand(time(NULL));

	//DEMO
	bunny test;
	test.demo();


	//END
	return 0;
}
Running exactly that code on ideone.com does not always lead to a crash:

http://ideone.com/F6aJv

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.

Last edited on
closed account (10oTURfi)
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.

edit: yah. im so stupid.
Last edited on
closed account (10oTURfi)
OW and one more thing.

How do I create new bunny objects during runtime of program?
You can make them on the stack, like this:

bunny test;
test.demo();


or on the heap, like this:

bunny* pTest = new (bunny);
pTest->demo();
closed account (10oTURfi)
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!
1
2
3
4
5
6
7
8
9
10
11
12
	bunny pBunny[5];

	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];
				}
			}
		}
	}


EDIT2: And if I try to allocate memory for all of the bunnies at begining, constructor goes mad (see above...)
Last edited on
If you need to remove elements from the middle of the collection during runtime, it's usually a sign that using a list may be a good idea.

http://cplusplus.com/reference/stl/list/
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.
closed account (10oTURfi)
If you need to remove elements from the middle of the collection during runtime, it's usually a sign that using a list may be a good idea.

Oh.. wow. That looks like something I was looking for. Thanks alot!
Topic archived. No new replies allowed.