Beginners exercises (mutant bunny)

Hi all!
Im new to c++ and programing at all. Im doing some exercises for beginners from that www: http://www.cplusplus.com/forum/articles/12974/
My problem is with the last one titled Graduation. I dont know how to do random enums in inclass function. My cod so far is:
kroliki.h
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
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
#pragma once

enum E_SEX { MALE, FEMALE};
enum E_COLOR { WHITE, BROWN, BLACK, SPOTTED};
enum E_NAME { JULIUS_CEZAR, HITLER, STALIN, POLPOT, MAO, EDMUND, EGIDA, CHURCHIL, ROOSVELT, DMOWSKI, JAPA, DUPA,
	          PUPA, MUSSOLINI, DE_GAULE,CHAVEZ, KADAFI, LUKASZENKO, CASTRO, KHAMENEI, AHMADINEJAD, BASHIR, SADDAM, KIM_IR_SONG};

class CFeatures
{
public:
	int sex;
	int age;
	std::string name;
	bool radioactive_mutant_vampire_bunny;
};

class CAdultBunny
{
public:
	CAdultBunny(void);
	bool IfRadioactive_mutant_vampire_bunny(int losowaLiczba);

	void SetSex(int losowaLiczba);
	void SetAge(int runda);
	void SetName(int losowaLiczba);

	int GetName();
	int GetSex();
	int GetAge();

	

protected:
	CFeatures Features;

public:
	~CAdultBunny(void);
};

and kroliki.cpp
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
#include <iostream>
#include <conio.h>
#include <string>
#include "kroliki.h"
using namespace std;

int LosujLiczbe(int min, int maxPlusJeden)
{
  return rand() % (maxPlusJeden - min) + min;
}

CAdultBunny::CAdultBunny(void)
{
}

CAdultBunny::~CAdultBunny(void)
{
}

bool CAdultBunny::SetRadioactive_mutant_vampire_bunny(int losowaLiczba)
{
	if (losowaLiczba <=2)
		return true;
	else 
		return false;
}

void CAdultBunny::SetSex(int losowaLiczba)
{
E_SEX = 0; //???
Features.sex = E_SEX + losowaLiczba;
}
void CAdultBunny::SetAge(int runda)
{
	Features.age = 0;
	if (runda++) // wrong, dunno lol
	Features.age++;

}
void CAdultBunny::SetName(int losowaLiczba)
{
	E_NAME =0; // ???
	Features.name = E_NAME + losowaLiczba;
}

as u can see, my fuctions SetName and SetSex are wrong, ('runda' means in polish 'turn' and losowaLiczba means 'randomNumber'). How can i do this random names and random sex with using those funtions?

And btw, what should i do with this 'if' in SetAge function? I cant write just 'turn++', although, when i will be doing rest of this program, i want this function to rise all bunnys age by one no matter how old is was (bunny).

Thx in advise for help.
cheers.
enums automatically map themselves to a range of integers starting at zero.

For instance, if you have

enum greetings{HELLO, GOODBYE, GOOD_AFTERNOON};

then HELLO would be equal to 0, GOODBYE would be equal to 1, and GOOD_AFTERNOON would be equal to 2. You already have a function LosujLiczbe(int min, int maxPlusJeden) which returns a random integer between min and maxPlusJeden - 1, inclusively. Say, if you want a random sex, just call this function with parameters 0 and 2 and assign the result to a variable of type E_SEX.

E_SEX randomSex = LosujLiczbe(0, 2);

As per your SetAge function, you would only put an if statement in such a function for error checking, like if runda is less than zero then you wouldn't want to assign the age. If you want the bunny's age to raise by one, just use Features.age++. For setting the age, just set runda to Features.age.
Perhaps you should make your CFeatures class a struct. It's just a pet-peeve of mine when people create classes with only public members instead of making a struct :/
Hi again, i did as u suggested me and this looks like this:
kroliki.h
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
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
#pragma once

enum E_SEX { MALE, FEMALE, SEX_COUNT};
enum E_COLOR { WHITE, BROWN, BLACK, SPOTTED, COLOUR_COUNT};
enum E_NAME { JULIUS_CEZAR, HITLER, STALIN, POLPOT, MAO, EDMUND, EGIDA, CHURCHIL, ROOSVELT, DMOWSKI, JAPA, DUPA,
	          PUPA, MUSSOLINI, DE_GAULE,CHAVEZ, KADAFI, LUKASZENKO, CASTRO, KHAMENEI, AHMADINEJAD, BASHIR, 
				SADDAM, KIM_IR_SONG, E_NAMES_COUNT};

class CFeatures
{
public:
	int sex;
	int age;
	std::string name;
	bool radioactive_mutant_vampire_bunny;
};

class CAdultBunny
{
public:
	CAdultBunny(void);
	bool SetRadioactive_mutant_vampire_bunny(int losowaLiczba);

	void SetSex();
	void SetAge(int runda);
	void SetName();

	
	int GetSex();
	int GetAge();

	std::string GetName();
	

protected:
	CFeatures Features;

public:
	~CAdultBunny(void);
};


kroliki.cpp
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
#include <iostream>
#include <conio.h>
#include <string>
#include "kroliki.h"
using namespace std;

const char* Names[] =
{ "Juliusz Cezar",
"Hitler","Stalin", "Polpot", "Mao", "Edmund", "Egida","Churchil","Roosvelt", "Dmowski",
"Japa","Dupa","Pupa","Mussolini", "De Gaule","Chavez",
"kadafi","Lukaszenko","Castro","Khamenei","Ahmadinejad","Bashir","Saddam","Kim Ir Song"
};

int LosujLiczbe(int min, int maxPlusJeden)
{
  return rand() % (maxPlusJeden - min) + min;
}

CAdultBunny::CAdultBunny(void)
{
}

CAdultBunny::~CAdultBunny(void)
{
}

bool CAdultBunny::SetRadioactive_mutant_vampire_bunny(int losowaLiczba)
{
	if (losowaLiczba <=2)
		return true;
	else 
		return false;
}


void CAdultBunny::SetAge(int runda)
{
	Features.age = 0; 
	if (runda++)
	Features.age++;

}
void CAdultBunny::SetSex()
{

Features.sex = E_SEX(rand() % SEX_COUNT) ;
}
void CAdultBunny::SetName()
{
	E_NAME myName;
	myName = E_NAME(rand() & E_NAMES_COUNT);
	Features.name = Names[myName];	
}

string CAdultBunny::GetName()
{
	return Features.name;
}

main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <conio.h>
#include <string>
#include <ctime>
#include "kroliki.h"

using namespace std;




void main()
{
srand(time(NULL));
CAdultBunny Krolik;
cout << Krolik.GetName();
	getch();
	


}

but when i ask my program to show (just to try it) any name of thise bunny it shows nothing, where i did mistake?
You did not call Krolik.SetName(). Also, the operator "&" is probably not what you intended in this case. This operator does a bitwise AND. rand() & E_NAMES_COUNT will give you, in this case (E_NAMES_COUNT == 24), either 8, 16 or 24 (because 24 only has bits 3 (8) and 4 (16) active). In the case of 24, your program would over-run the Names array by 1.
Last edited on
right, i made a mistakes.
Thx for help, now it works as i want.
Topic archived. No new replies allowed.