Help with array output

Hello, I'm new to coding in c++ and I need to output the sound the mammal makes through void speak when I output the array at the end of code. So when the loop breaks at the end of pig it will go through the list listing the animal, the name of the animal, the weight of the animal, and the sound the animal makes.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "pch.h"
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

class Mammal {
protected:
	string Name;
	int Weight;

public:

	Mammal() {
		Name = "";
		Weight = 0;
		cout << "Invoking Mammal default constructor" << endl;
	}

	Mammal(string name, int weight) {
		Name = name;
		Weight = weight;
		cout << "Invoking Mammal parameterized constructor" << endl;
	}

	void speak() {
		cout << "Mammal sound" << endl;
	}

	string getName() const {
		return Name;
	}

	void setName(string Name) {
		this->Name = Name;
	}

	int getWeight() const {
		return Weight;
	}

	void setWeight(int Weight) {
		this->Weight = Weight;
	}

};

class Dog : public Mammal {
public:

	Dog(string Name, int Weight) : Mammal(Name, Weight) {
		cout << "Invoking Dog constructor" << endl;
	}

	~Dog() {
		cout << "Invoking Dog destructor" << endl;
	}

	void speak() {
		cout << "Woof Woof" << endl;
	}
};

class Cat : public Mammal {
public:

	Cat(string Name, int Weight) : Mammal(Name, Weight) {
		cout << "Invoking Cat constructor" << endl;
	}

	~Cat() {
		cout << "Invoking Cat destructor" << endl;
	}

	void speak() {
		cout << "Meow Meow" << endl;
	}
};

class Pig : public Mammal {
public:

	Pig(string Name, int Weight) : Mammal(Name, Weight) {
		cout << "Invoking Pig constructor" << endl;
	}

	~Pig() {
		cout << "Invoking Pig destructor" << endl;
	}

	void speak() {
		cout << "Oink Oink" << endl;
	}
};

class Horse : public Mammal {
public:

	Horse(string Name, int Weight) : Mammal(Name, Weight) {
		cout << "Invoking Horse constructor" << endl;
	}

	~Horse() {
		cout << "Invoking Horse destructor" << endl;
	}

	void speak() {
		cout << "I’m Mr. Ed" << endl;
	}
};



int main() {
	srand(100);
	string name;
	Mammal *m[4];
	int ch;
	int j = 0;
	while (j < 5) {
		int w = rand() % 150;
		cout << "Choose a Mammal\n1. Cat\n2. Dog\n3. Horse\n4. Pig\nEnter your choice by typing the number: ";
		cin >> ch;
		cout << "\nEnter a name: ";

		cin >> name;

		if (ch == 1) {
			Cat catto(name, w);
			m[j] = &catto;
			j++;
		}
		else if (ch == 2) {
			Dog doggo(name, w);
			m[j] = &doggo;
			j++;
		}
		else if (ch == 3) {
			Horse horsey(name, w);
			m[j] = &horsey;
			j++;
		}
		else if (ch == 4) {
			Pig piggo(name, w);
			m[j] = &piggo;
			j++;
			break;
		}
		else {
			cout << "invalid entry:" << endl;
		}

	}

	for (int i = 0; i < 5; i++) {
		m[i]->speak();                       
	}
	return 0;
}

Last edited on
1
2
Cat catto(name, w);
m[j] = &catto;

catto is a local variable which goes out of scope at the following }

You need more persistent objects.

Eg
m[j] = new Cat(name,w);

> while (j < 5)
You only have 4 choices, not 5.

You'll also need to do this to balance all the new calls you did earlier.
1
2
3
for (int i = 0; i < 5; i++) {
  delete m[i];
}


Topic archived. No new replies allowed.