unwanted numbers in object member

In this code I pass object 'monster' as parameter through function 'connect _frags': connect_frags(monster). There it calls a function 'printMonsterName' which prints the monster name in this monster object, Bones. Prints fine every time correctly except it also prints garbage number values with it: Bones4738944 in this case. I've isolated that the Bones and number are occurring at the same time. Where are the unwanted numbers coming from and how do I program this correctly (no numbers)?
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
#include<iostream>
#include<string>

class Monster
{

public:
		printMonsterType()
		{

		//std::cout << static_cast<string>(Skeleton); //
		}
		printMonsterName()
		{
		std::cout<<m_name;
		}
		printMonsterRoar()
		{
		std::cout<<m_roar;
		}
		printMonsterHitPoints()
		{
		std::cout<<m_hit_points<<std::endl;
		}


	enum MonsterType
	{
		Dragon,
		Goblin,
		Ogre,
		Orc,
		Skeleton,
		Troll,
		Vampire,
		Zombie,
		MAX_MONSTER_TYPES
	};

	Monster(MonsterType _type, std::string name, std::string roar, int hit_points):
			m_type(_type), m_name(name), m_roar(roar), m_hit_points(hit_points)
			{

			}


void print(){std::cout<<"here"<<std::endl;}
private:
	MonsterType m_type;
	std::string m_name;
	std::string m_roar;
	int m_hit_points;


};

void connect_frags(Monster &monster)
{
std::cout<<monster.printMonsterName();
std::cout<<"flag";
}
//Bones the skeleton has 4 hit points and says *rattle*
int main()
{
Monster monster(Monster::Skeleton,"Bones","Rattle",4);		//when you make the constructor you make the object
connect_frags(monster);

    return 0;
}


Bones4738944flag
Last edited on
I want to understand your train of thought.
> std::cout<<monster.printMonsterName();
¿what does `printMonsterName()' return?

1
2
3
4
printMonsterName()
{
	std::cout<<m_name;
}
you didn't even declare a return type (which is an error, btw) and there is no return statement, but yet, you did try to use the returned value.

So, ¿what were you thinking?
All valid points. I've added void return type. I only need the function to return a print-to-screen action:

void printMonsterName()
{
std::cout<<m_name;
}

...
Adding the type to function helped. Removing the 'cout' to cout<<monster.printMOnsterName(); resolved an error in which cout was outputting a cout.
Topic archived. No new replies allowed.