How to store/call an object through a variable?

Hello! I am trying to write a Pokemon Battle program, where I am having trouble. I am trying to get the user's input of a Pokemon (object) and store it in a variable (YourPKMN) so when I want to call functions like statPg, I can do so through the variable. How would I do that?

Here is the code:

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
163
164
165
166
 #include"stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;




class Attack
{
public:
	//constructor for attack taking all the information
	Attack(string atkName, int atkPower, int recoil, int atkPP, int atkAcc, string atkType) : //this is used post c++ 11 to initialize the member variable and behaves identical to 
		name(atkName),//name == atkName
		Recoil(recoil),
		power(atkPower), //power == atkPower
		pp(atkPP),
		accuracy(atkAcc),
		type(atkType)
	{
	}

	//accessor methods. Returns the value of the member variable. 
	string getName()
	{
		return name;
	}

	//if you need a public way to change a member variable create the setter
	//void setName(string newName)
	//{
	//  name  = newName;
	//}
	int getPower()
	{
		return power;
	}

	int getRecoil()
	{
		return Recoil;
	}

	int getPP()
	{
		return pp;
	}

	int getAccuray()
	{
		return accuracy;
	}

	string getType()
	{
		return type;
	}

private:
	string name;
	int power;
	int Recoil;
	int pp;
	int accuracy;
	string type;
};
 
class Pokemon
{
public:
	//constructor with all the needed information to create a pokemon
	Pokemon(string pokeName, string type1, string type2, int HPstat, int ATKstat, int SpATKstat, int DEFstat, int SpDEFstat, int Speedstat, vector<Attack> &attacks) :
		name(pokeName),
		type1(type1),
		type2(type2),
		HP(HPstat),
		Atk(ATKstat),
		SpAtk(SpATKstat),
		Def(DEFstat),
		SpDEF(SpDEFstat),
		Speed(Speedstat),
		attackList(attacks)
	{}

	//prints out the stats for your pokemon
	void statPg()
	{
		cout << "#################################################################\n";
		cout << name << ": " << type1 << " " << type2 << " Type\n"
			<< "\t" << HP << " HP\n"
			<< "\t" << Atk << " ATK\n"
			<< "\t" << Def << " DEF\n"
			<< "\t" << Speed << " Speed\n\n";
		//loops through the vector and prints out each attack information
		for (auto iter = attackList.begin(); iter != attackList.end(); iter++)
		{
			cout << iter->getName() << " | " << iter->getPP() << " PP\n";
		}
		cout << "#################################################################\n";
	}
public:
	string name;
	string type1;
	string type2;
	int HP;
	int Atk;
	int SpAtk;
	int Def;
	int SpDEF;
	int Speed;
	vector<Attack> attackList; //vector of attacks

}; 



int main()
{
	//Attacks! (no need to inherit and create a new struct)
	Attack HydroPump("Hydro Pump", 120, 0, 5, 75, "Water");
	Attack TBolt("Thunderbolt", 100, 0, 10, 100, "Electric");
	Attack ElectroBall("Electro Ball", 150, 50, 10, 100, "Elecric");
	Attack FireBlast("Fire Blast", 150, 0, 5, 80, "Fire");
	Attack Crunch("Crunch", 90, 0, 30, 100, "Dark");
	Attack Superpower("Superpower", 30, 120, 10, 90, "Fighting");
	Attack Earthquake("Earthquake", 100, 0, 10, 100, "Ground");
	Attack Psychic("Psychic", 90, 0, 25, 100, "Psychic");
	Attack ShadowBall("Shadow Ball", 90, 0, 30, 100, "Ghost");
	Attack RockSlide("Rock Slide", 100, 0, 10, 90, "Rock");
	Attack Explosion("Explosion", 300, 999999, 5, 100, "Normal");
	Attack StoneEdge("Stone Edge", 120, 30, 10, 100, "Ground");
	Attack Recover("Recover", 0, 0, 30, 100, "Normal");
	Attack FlashCannon("Flash Cannon", 100, 0, 20, 100, "Steel");
	Attack Scald("Scald", 100, 0, 30, 100, "Water");
	Attack IceBeam("Ice Beam", 100, 0, 20, 100, "Ice");

	//Attack lists for Pokemon
	vector<Attack> GolemAttack{ Earthquake, RockSlide, Explosion, StoneEdge };
	vector<Attack> ShedinjaAttack{ HydroPump, TBolt, Psychic, FireBlast };
	vector<Attack> LuxrayAttack{ TBolt, ElectroBall, Superpower, Crunch };
	vector<Attack> StarmieAttack{ TBolt, HydroPump, Recover, Psychic };
	vector<Attack> EmpoleonAttack{ Scald, FlashCannon, HydroPump, IceBeam };

	//Pokemon!
	Pokemon Golem("Golem", "Rock", "Ground", 80, 120, 130, 55, 65, 45, GolemAttack);
	Pokemon Luxray("Luxray", "Electric", "\0", 80, 120, 79, 95, 79, 70, LuxrayAttack);
	Pokemon Starmie("Starmie", "Water", "Psychic", 60, 75, 85, 100, 85, 115, StarmieAttack);
	Pokemon Empoleon("Empoleon", "Steel", "Water", 84, 86, 88, 111, 101, 60, EmpoleonAttack);
	Pokemon Shedinja("Shedinja", "Ghost", "Bug", 1, 90, 45, 30, 30, 40, ShedinjaAttack);


	// Actual stuff below
	 
	string input;
	string* YourPKMN;
	YourPKMN = &input;
	cout << "Welcome to the Pokemon Battle Simulator! Choose your Pokemon!\n";
	cin >> input;
	cout << endl;

	if (auto input = "Golem")
	{
		auto YourPKMN = &Golem;
	}
} 
While not totally clear on what you are asking, it looks as if you are almost there. You can call

Line 156: Pokemon* YourPKMN = nullptr;

Line 162:
1
2
3
4
5
6
7
if (input == "Golem") {
    YourPKMN = &Golem;
}

if (YourPKMN != nullptr) {
  YourPKMN->statPg();
}


Does that help?
Last edited on
Let me check that out.

[EDIT]

It worked! Thank you for the help!
Last edited on
Great! Please mark this thread as "solved"
Oops, sorry!
Topic archived. No new replies allowed.