monster1 & monster2 undeclared in battlephase function

These are the error messages i get when i run this program, and to those who will critisize me, i know my program will not currently display something yet, just redoing it to better structure it.

you don't have to read the whole program, i just included it so you can easily find the error messages or better fix my issue

164 C:\Dev-Cpp\Untitled1.cpp `monster1' undeclared (first use this function)
164 C:\Dev-Cpp\Untitled1.cpp `monster2' undeclared (first use this function)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
#include <vector>
#include <fstream>
using namespace std;
class Monster
{
      public:
      Monster(){}
      ~Monster(){}     
      void DisplayStats()
      {
           cout << "--------Monster Stats--------";
           cout << "\nType:" << Type;
           cout << "\nAttack:" << Attack;
           cout << "\nDefense:" << Defense;
           cout << "\nHealth:" << Health;
           cout << "\nEvade:" << Evade;
           cout << "\nLuck:" << Luck;
           cout << "\nSpeed:" << Speed;
           cout << "\nAdvantage:" << Advantage;
           cout << "\nDisadvantage:" << Disadvantage;
           cout <<"\nLevel:" << Level;
           cout <<"\nExperience:" << Experience;
           cout <<"\nSkillPoints:" << SkillPoints;
           cout << "\n-------------------------------";
      } 
      string GetType() { return Type; }
      void SetType(string x) { Type = x; }
      
      int GetAttack() { return Attack; }
      void SetAttack(int x) { Attack = x; }
           
      int GetDefense() { return Defense; }
      void SetDefense(int x) { Defense = x; }
      
      int GetHealth() { return Health; }
      void SetHealth(int x) { Health = x; }
      
      int GetSpeed() { return Speed; }
      void SetSpeed(int x) { Speed = x; }
      
      int GetEvade() { return Evade; }
      void SetEvade(int x) { Evade = x; }
      
      int GetLuck() { return Luck; }
      void SetLuck(int x) { Luck = x; }
      
      string GetAdvantage() { return Advantage; }
      void SetAdvantage(string x) { Advantage = x; }
      
      string GetDisadvantage() { return Disadvantage; }
      void SetDisadvantage(string x) { Disadvantage = x; }
      
      string GetMonsterName() { return MonsterName; }
      void SetMonsterName(string x) { MonsterName = x; }
      
      int GetLevel() { return Level; }
      void SetLevel(int x) { Level = x; }
      
      int GetExperience() { return Experience; }
      void SetExperience(int x) { Experience = x; }
      
      int GetSkillPoints() { return SkillPoints; }
      void SetSkillPoints(int x) { SkillPoints = x; }
      
      
      
      private:
      string Type;
      int Attack;
      int Defense;
      int Health;
      string Advantage;
      string Disadvantage;
      string MonsterName;
      int Evade;
      int Luck;
      int Speed;
      int Level;
      int Experience;
      int SkillPoints;
};

class Player
{
public:
void PlayerCard()
{
cout << "Here " << Name << " This is your I'D card I have created for you.\n";
cout << "-----------------------------\n";
cout << "| ID CARD |                 |\n";
cout << "-----------------------------\n";
cout << "| NAME:   " << Name << "\n";
cout << "| AGE:    " << Age << "\n";
cout << "| GENDER: " << Gender << "\n";
cout << "=============================\n\n\n";
cin.get();
}
string GetName() { return Name; }
void SetName(string x) { Name = x; }

string GetAge() { return Age; }
void SetAge(string x) { Age = x; }

string GetGender() { return Gender; }
void SetGender(string x) { Gender = x; }

int GetMoney() { return Money; }
void SetMoney(int x) { Money = x; }

private:       
string Name;
string Age;
string Gender;
int Money;
};
void YourMonster()
{
Monster monster1;
monster1.SetType("fire");//type
monster1.GetType();
monster1.SetAttack(8);//attack
monster1.GetAttack();
monster1.SetDefense(1);//defense
monster1.GetDefense();
monster1.SetHealth(40);//health
monster1.GetHealth();
monster1.SetAdvantage("Nature");//advantage
monster1.GetAdvantage();
monster1.SetDisadvantage("Water");//disadvantage
monster1.GetDisadvantage();
monster1.SetMonsterName("monster1");//name
monster1.GetMonsterName();
monster1.DisplayStats();
cin.get();
}

void OpponentMonster()
{
Monster monster2;
monster2.SetType("fire");//type
monster2.GetType();
monster2.SetAttack(8);//attack
monster2.GetAttack();
monster2.SetDefense(1);//defense
monster2.GetDefense();
monster2.SetHealth(40);//health
monster2.GetHealth();
monster2.SetAdvantage("Nature");//advantage
monster2.GetAdvantage();
monster2.SetDisadvantage("Water");//disadvantage
monster2.GetDisadvantage();
monster2.SetMonsterName("monster2");//name
monster2.GetMonsterName();
monster2.DisplayStats();
cin.get();
}
void Battlephase()
{
     while (monster1.Health > 0 && monster2.Health > 0)
     {   
         if(monster1.Health > 0)
         {
              monster1.Damage = monster1.Attack - monster2.Defense;
              if(monster1.Damage <=1)
              {
              monster1.Damage = 1;
              }
              cout << "You attacked and dealt " << monster1.Damage <<" Damage\n";
              monster2.Health = monster2.Health - monster1.Damage; 
              cout << "Your opponents health is " << monster2.Health << "\n\n";
              if(monster2.Health <= 0) 
              { 
              cout <<"You Win!"<< "\n\n";
              }
         }
         if(monster2.Health > 0)
         {          
              monster2.Damage = monster2.Attack - monster1.Defense;
              if(monster2.Damage <=1)
              {
              monster2.Damage = 1;
              }
              cout << "Your opponent attacked and dealt " << monster2.Damage <<" Damage\n"; 
              monster1.Health = monster1.Health - monster2.Damage; 
              cout << "Your health is " << monster1.Health << "\n\n";   
              if(monster1.Health <= 0) 
              {  
              cout <<"You Lose :(\n\n";
              }
         } 
     }
}
int main(int argc, char *argv[])
{  
cin.get();
return 0;
}
Last edited on
The function void Battlephase() tries to use an object named monster1, but no such object exists inside that function. Either pass it in, or create it in the function.
i tried to pass it on but i don't know how, i cant simply return monster1 because of all the different types of variables there are, i don't know how to return all of them ect.
You're looking for something called pass-by-reference.

When you have questions, please ask all of them. Please don't finish by saying "ect" - this indicates there is more you would like to ask but choose to just let us guess what it is that you don't understand.
Well can you please tell me how to do this, and btw theres gonna be hundreds of monters in the bigger version so it needs to be compatable with that too.
You're making progress.

Here are some issues that i see:
Line 123: monster1 is local in scope to YourMonster. It goes out of scope when YourMonster exits.
Lines 124-137. You call your various get function, but don't do anything with the result. e.g. GetType() returns a string, but you don't do anything with it.
Line 144: Same problem as line 123.
Lines 145-158: Same problem as 124-137.

To pass by reference do the following:
1
2
3
4
5
void YourMonster (Monster & monster)
{ Monster monster;
   monster.SetType("fire");//type
...etc
}

When you call YourMonster, you will pass an instance of Monster:
1
2
  Monster monster1;  //  Define the instance
  YourMonster (monster1);  // Initialize the instance 


You will also need to pass monster1 and monster2 to BattlePhase.
1
2
void BattlePhase (Monster & monster1, Monster Monster2)
{...}


Issues in BattlePhase:
Numerous places: You are trying to reference private members of Monster. e.g.
 
while (monster1.Health > 0 && monster2.Health > 0)


You should be calling your GetHealth functions:
 
while (monster1.GetHealth() > 0 && monster2.GetHealth() > 0)


Likewise, when you try to change private members of Monster, you need to all the appropriate set function.
 
monster1.Damage = 1;


Should be:
 
monster1.SetDamage(1);


Your main function should declare monster1 and monster2, initialize them and then call BattlePhase.

1
2
3
4
5
6
7
8
9
10
int main(int argc, char *argv[])
{  Monster  monster1;
    Monster  monster2;

    YourMonster (monster1);
    OpponentMonster (monster2);
    BattlePhase (monster1, monster2);
    cin.get();
    return 0;
}






Took the advice you gave and there's still a few errors, can you help me fix them? (errors stated at bottom)

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
#include <vector>
#include <fstream>
using namespace std;
class Monster
{
      public:
      Monster(){}
      ~Monster(){}     
      void DisplayStats()
      {
           cout << "--------Monster Stats--------";
           cout << "\nType:" << Type;
           cout << "\nAttack:" << Attack;
           cout << "\nDefense:" << Defense;
           cout << "\nHealth:" << Health;
           cout << "\nEvade:" << Evade;
           cout << "\nLuck:" << Luck;
           cout << "\nSpeed:" << Speed;
           cout << "\nAdvantage:" << Advantage;
           cout << "\nDisadvantage:" << Disadvantage;
           cout <<"\nLevel:" << Level;
           cout <<"\nExperience:" << Experience;
           cout <<"\nSkillPoints:" << SkillPoints;
           cout << "\n-------------------------------";
      } 
      string GetType() { return Type; }
      void SetType(string x) { Type = x; }
      
      int GetAttack() { return Attack; }
      void SetAttack(int x) { Attack = x; }
           
      int GetDefense() { return Defense; }
      void SetDefense(int x) { Defense = x; }
      
      int GetHealth() { return Health; }
      void SetHealth(int x) { Health = x; }
      
      int GetSpeed() { return Speed; }
      void SetSpeed(int x) { Speed = x; }
      
      int GetEvade() { return Evade; }
      void SetEvade(int x) { Evade = x; }
      
      int GetLuck() { return Luck; }
      void SetLuck(int x) { Luck = x; }
      
      string GetAdvantage() { return Advantage; }
      void SetAdvantage(string x) { Advantage = x; }
      
      string GetDisadvantage() { return Disadvantage; }
      void SetDisadvantage(string x) { Disadvantage = x; }
      
      string GetMonsterName() { return MonsterName; }
      void SetMonsterName(string x) { MonsterName = x; }
      
      int GetLevel() { return Level; }
      void SetLevel(int x) { Level = x; }
      
      int GetExperience() { return Experience; }
      void SetExperience(int x) { Experience = x; }
      
      int GetSkillPoints() { return SkillPoints; }
      void SetSkillPoints(int x) { SkillPoints = x; }
      
      int GetDamage() { return Damage; }
      void SetDamage(int x) { Damage = x; }
      
      
      
      private:
      string Type;
      int Attack;
      int Defense;
      int Health;
      string Advantage;
      string Disadvantage;
      string MonsterName;
      int Evade;
      int Luck;
      int Speed;
      int Level;
      int Experience;
      int SkillPoints;
      int Damage;
};

class Player
{
public:
void PlayerCard()
{
cout << "Here " << Name << " This is your I'D card I have created for you.\n";
cout << "-----------------------------\n";
cout << "| ID CARD |                 |\n";
cout << "-----------------------------\n";
cout << "| NAME:   " << Name << "\n";
cout << "| AGE:    " << Age << "\n";
cout << "| GENDER: " << Gender << "\n";
cout << "=============================\n\n\n";
cin.get();
}
string GetName() { return Name; }
void SetName(string x) { Name = x; }

string GetAge() { return Age; }
void SetAge(string x) { Age = x; }

string GetGender() { return Gender; }
void SetGender(string x) { Gender = x; }

int GetMoney() { return Money; }
void SetMoney(int x) { Money = x; }

private:       
string Name;
string Age;
string Gender;
int Money;
};
void YourMonster(Monster & monster)
{
Monster monster1;
monster1.SetType("fire");//type
monster1.GetType();
monster1.SetAttack(8);//attack
monster1.GetAttack();
monster1.SetDefense(1);//defense
monster1.GetDefense();
monster1.SetHealth(40);//health
monster1.GetHealth();
monster1.SetAdvantage("Nature");//advantage
monster1.GetAdvantage();
monster1.SetDisadvantage("Water");//disadvantage
monster1.GetDisadvantage();
monster1.SetMonsterName("monster1");//name
monster1.GetMonsterName();
monster1.DisplayStats();
cin.get();
}

void OpponentMonster(Monster & monster)
{
Monster monster2;
monster2.SetType("fire");//type
monster2.GetType();
monster2.SetAttack(8);//attack
monster2.GetAttack();
monster2.SetDefense(1);//defense
monster2.GetDefense();
monster2.SetHealth(40);//health
monster2.GetHealth();
monster2.SetAdvantage("Nature");//advantage
monster2.GetAdvantage();
monster2.SetDisadvantage("Water");//disadvantage
monster2.GetDisadvantage();
monster2.SetMonsterName("monster2");//name
monster2.GetMonsterName();
monster2.DisplayStats();
cin.get();
}
void BattlePhase (Monster & monster1, Monster & monster2)
{
     while (monster1.GetHealth() > 0 && monster2.GetHealth() > 0)
     {   
         if(monster1.GetHealth() > 0)
         {
              monster1.SetDamage() = monster1.GetAttack() - monster2.GetDefense();
              if(monster1.GetDamage() <=1)
              {
              monster1.SetDamage(1);
              }
              cout << "You attacked and dealt " << monster1.GetDamage() <<" Damage\n";
              monster2.SetHealth() = monster2.GetHealth() - monster1.GetDamage(); 
              cout << "Your opponents health is " << monster2.GetHealth() << "\n\n";
              if(monster2.GetHealth() <= 0) 
              { 
              cout <<"You Win!"<< "\n\n";
              }
         }
         if(monster2.GetHealth() > 0)
         {          
              monster2.SetDamage() = monster2.GetAttack() - monster1.GetDefense();
              if(monster2.GetDamage() <=1)
              {
              monster2.SetDamage(1);
              }
              cout << "Your opponent attacked and dealt " << monster2.GetDamage() <<" Damage\n"; 
              monster1.SetHealth() = monster1.GetHealth() - monster2.GetDamage(); 
              cout << "Your health is " << monster1.GetHealth() << "\n\n";   
              if(monster1.GetHealth() <= 0) 
              {  
              cout <<"You Lose :(\n\n";
              }
         } 
     }
}
int main(int argc, char *argv[])
{  Monster  monster1;
    Monster  monster2;

    YourMonster (monster1);
    OpponentMonster (monster2);
    BattlePhase (monster1, monster2);
    cin.get();
    return 0;
}
//172 C:\Dev-Cpp\Untitled1.cpp no matching function for call to `Monster::SetDamage()' 
//178 C:\Dev-Cpp\Untitled1.cpp no matching function for call to `Monster::SetHealth()' 
//187 C:\Dev-Cpp\Untitled1.cpp no matching function for call to `Monster::SetDamage()' 
//193 C:\Dev-Cpp\Untitled1.cpp no matching function for call to `Monster::SetHealth()'  
Last edited on
Delete line 127.
Lines 128-142 change references from monster1 to just monster (the argument that was passed in). I changed the name of the argument here intentionally to make the poiint that the name of the argument does not need to be the same as the variable in the calling function.
Delete line 148.
Lines 149-149-163 changes references from monster2 to monster.
Because you passed in monster1 or monster2 by reference, you're referring to the respective instance of monster in the calling function.
Line 172: You can't set a function equal to a number. You have to pass the new value as the argument. See the example I gave you earlier.
 
monster1.SetDamage(monster1.GetAttack() - monster2.GetDefense());

Lines 178, 187, 193: same problem.

Lines 129-141, 150-162: What's the point of calling the various get routines? You're not doing anything with the result.
the get routines are an alternative way to pause the program, rather than using system("PAUSE"), which is evil.
Last edited on
I'm not referring to cin.get(), I know what that does.
I'm referring to why you're calling the getter functions in your class and not doing anything with the result.

Lets look specifically at line 129:
 
monster1.GetType();

and the implementation of it at line 31:
 
string GetType() { return Type; }

GetType returns the current value of Type (a string). It does not pause the program.
But when you call GetType at line 129, you don't do anything with the string that is returned, so what was the point of calling it?

Last edited on
For type advantages function that has yet to been implemented due to me restructuring my whole entire program, dont worry it will have a use
Last edited on
Code works and is currently free from errors, thanks for the help, also if you could, any advice that would improve the quality of the code, or help keep main, as free of as much code as possible would be helpful. I want to do something similar to this code, where this function calls the already created monster, thats in in a seperate function to be used in battlephase i know it is outdated due to the modefications but any help would be great
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
void YourType()
{
     string CharacterDetails;

     cout << "What character do you want?\n\n";
     cout << "There is a fire type who specializes with fire attacks. (fire)\n";
     cout << "There is a water type who specializes with water attacks. (water)\n";
     cout << "There is a nature type who specializes with nature attacks. (nature)\n\n";
     cout << "Please enter the type of the monster you would like:";
     cin >> CharacterDetails;     
     system("CLS");
     if(CharacterDetails == "fire")
     {
     YourFireType();
     }
     else if(CharacterDetails == "water")
     {
     YourWaterType();
     }
     else{ 
     YourNatureType();
     }

     system("CLS");
}



fixed code below for reference, also changed function names back to YourFireType, and OpponentFireType
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
#include <vector>
#include <fstream>
using namespace std;
class Monster
{
      public:
      Monster(){}
      ~Monster(){}     
      void DisplayStats()
      {
           cout << "--------Monster Stats--------";
           cout << "\nType:" << Type;
           cout << "\nAttack:" << Attack;
           cout << "\nDefense:" << Defense;
           cout << "\nHealth:" << Health;
           cout << "\nEvade:" << Evade;
           cout << "\nLuck:" << Luck;
           cout << "\nSpeed:" << Speed;
           cout << "\nAdvantage:" << Advantage;
           cout << "\nDisadvantage:" << Disadvantage;
           cout <<"\nLevel:" << Level;
           cout <<"\nExperience:" << Experience;
           cout <<"\nSkillPoints:" << SkillPoints;
           cout << "\n-------------------------------";
      } 
      string GetType() { return Type; }
      void SetType(string x) { Type = x; }
      
      int GetAttack() { return Attack; }
      void SetAttack(int x) { Attack = x; }
           
      int GetDefense() { return Defense; }
      void SetDefense(int x) { Defense = x; }
      
      int GetHealth() { return Health; }
      void SetHealth(int x) { Health = x; }
      
      int GetSpeed() { return Speed; }
      void SetSpeed(int x) { Speed = x; }
      
      int GetEvade() { return Evade; }
      void SetEvade(int x) { Evade = x; }
      
      int GetLuck() { return Luck; }
      void SetLuck(int x) { Luck = x; }
      
      string GetAdvantage() { return Advantage; }
      void SetAdvantage(string x) { Advantage = x; }
      
      string GetDisadvantage() { return Disadvantage; }
      void SetDisadvantage(string x) { Disadvantage = x; }
      
      string GetMonsterName() { return MonsterName; }
      void SetMonsterName(string x) { MonsterName = x; }
      
      int GetLevel() { return Level; }
      void SetLevel(int x) { Level = x; }
      
      int GetExperience() { return Experience; }
      void SetExperience(int x) { Experience = x; }
      
      int GetSkillPoints() { return SkillPoints; }
      void SetSkillPoints(int x) { SkillPoints = x; }
      
      int GetDamage() { return Damage; }
      void SetDamage(int x) { Damage = x; }
      
      
      
      private:
      string Type;
      int Attack;
      int Defense;
      int Health;
      string Advantage;
      string Disadvantage;
      string MonsterName;
      int Evade;
      int Luck;
      int Speed;
      int Level;
      int Experience;
      int SkillPoints;
      int Damage;
};

class Player
{
public:
void PlayerCard()
{
cout << "Here " << Name << " This is your I'D card I have created for you.\n";
cout << "-----------------------------\n";
cout << "| ID CARD |                 |\n";
cout << "-----------------------------\n";
cout << "| NAME:   " << Name << "\n";
cout << "| AGE:    " << Age << "\n";
cout << "| GENDER: " << Gender << "\n";
cout << "=============================\n\n\n";
cin.get();
}
string GetName() { return Name; }
void SetName(string x) { Name = x; }

string GetAge() { return Age; }
void SetAge(string x) { Age = x; }

string GetGender() { return Gender; }
void SetGender(string x) { Gender = x; }

int GetMoney() { return Money; }
void SetMoney(int x) { Money = x; }

private:       
string Name;
string Age;
string Gender;
int Money;
};
void YourFireType(Monster & monster)
{
monster.SetType("fire");//type
monster.GetType();
monster.SetAttack(8);//attack
monster.GetAttack();
monster.SetDefense(1);//defense
monster.GetDefense();
monster.SetHealth(40);//health
monster.GetHealth();
monster.SetAdvantage("Nature");//advantage
monster.GetAdvantage();
monster.SetDisadvantage("Water");//disadvantage
monster.GetDisadvantage();
monster.SetMonsterName("YourFireType");//name
monster.GetMonsterName();
monster.SetEvade(10);
monster.GetEvade();
monster.SetLuck(10);
monster.GetLuck();
monster.SetSpeed(10);
monster.GetSpeed();
monster.SetLevel(5);
monster.GetLevel();
monster.SetExperience(1000);
monster.GetExperience();
monster.SetSkillPoints(0);
monster.GetSkillPoints();
monster.DisplayStats();
cin.get();
}

void OpponentFireType(Monster & monster)
{
monster.SetType("fire");//type
monster.GetType();
monster.SetAttack(9);//attack
monster.GetAttack();
monster.SetDefense(1);//defense
monster.GetDefense();
monster.SetHealth(40);//health
monster.GetHealth();
monster.SetAdvantage("Nature");//advantage
monster.GetAdvantage();
monster.SetDisadvantage("Water");//disadvantage
monster.GetDisadvantage();
monster.SetMonsterName("OpponentFireType");//name
monster.GetMonsterName();
monster.SetEvade(10);
monster.GetEvade();
monster.SetLuck(10);
monster.GetLuck();
monster.SetSpeed(10);
monster.GetSpeed();
monster.SetLevel(5);
monster.GetLevel();
monster.SetExperience(1000);
monster.GetExperience();
monster.SetSkillPoints(0);
monster.GetSkillPoints();
monster.DisplayStats();
cin.get();
}
void BattlePhase (Monster & monster1, Monster & monster2)
{
     while (monster1.GetHealth() > 0 && monster2.GetHealth() > 0)
     {   
         if(monster1.GetHealth() > 0)
         {
              monster1.SetDamage(monster1.GetAttack() - monster2.GetDefense());
              if(monster1.GetDamage() <=1)
              {
              monster1.SetDamage(1);
              }
              cout << "You attacked and dealt " << monster1.GetDamage() <<" Damage\n";
              monster2.SetHealth(monster2.GetHealth() - monster1.GetDamage()); 
              cout << "Your opponents health is " << monster2.GetHealth() << "\n\n";
              if(monster2.GetHealth() <= 0) 
              { 
              cout <<"You Win!"<< "\n\n";
              }
              cin.get();
         }
         if(monster2.GetHealth() > 0)
         {          
              monster2.SetDamage(monster2.GetAttack() - monster1.GetDefense());
              if(monster2.GetDamage() <=1)
              {
              monster2.SetDamage(1);
              }
              cout << "Your opponent attacked and dealt " << monster2.GetDamage() <<" Damage\n"; 
              monster1.SetHealth(monster1.GetHealth() - monster2.GetDamage()); 
              cout << "Your health is " << monster1.GetHealth() << "\n\n";   
              if(monster1.GetHealth() <= 0) 
              {  
              cout <<"You Lose :(\n\n";
              }
              cin.get();
         } 
     }
}
int main(int argc, char *argv[])
{  Monster  monster1;
    Monster  monster2;

    YourFireType (monster1);
    OpponentFireType (monster2);
    BattlePhase (monster1, monster2);
    cin.get();
    return 0;
}
Last edited on
so any help integrating that into my code, while keeping main as clear from as much code as you can would be highly apprecieated (i want to be able to choose my monsters (YourFireType, YourWaterType,YourNatureType) from a seperate void function, and maybe clear up main a bit
Last edited on
Gald to see you've got it to compile and execute.

There's a simpler way to write YourFireType and OpponentFireType. That is to make them member functions of Monster. As a member function, each has access to Monster's private variables.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Monster::YourFireType ()
{  Type = "fire";
    Attack = 8;
    Defense = 1;
    Health = 40;
    Advantage = "Nature";
    Disadvantage = "Water";
    MonsterName = "YourFireType";
    Evade = 10;
    Luck = 10;
    Speed = 10;
    Level = 5; 
    Experience = 1000;
    SkillPoints = 0;
    DisplayStats();
    cin.get();
}

You would call it as follows:
 
  monster1.YourFireType();




why the :: i have namespace std included at top?

and how would i integrate void YourChoice function? because im gonna have more than just YourFireType (YourWaterType,YourNatureType, same for 'opponent') and don't know if the function will get rid of all of the choosing monster data when it ends, which could make it undefined in BattlePhase Function
Last edited on

why the :: i have namespace std included at top?

Because that is how you implement a member function. This has nothing to do with namespace std.

You will also need to add
 
void YourFireType ();

in your class declaration.

You have all you getter and setter functions implemented in your header file. That's okay, but as your class gets larger you'll want to put your class function implementations in their own .cpp file (e.g. monster.cpp).

So for example, if you implement GetType outside of the class declaration, it would look like this:
1
2
string Monster::GetType() 
{ return Type; }


:: is the scope resolution operator. That says that GetType is "within the scope of" the Monster class.

right now my main priority is to get it to run like my old code, without all the global variables... ill show you both parts so you can see how its supposed to play out, (the player,level and earned part isn't important, but implementing everything else is right now, any help is much apreciated)

PART 1

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
#include <fstream>
using namespace std;
void pause(int dur);//pauses the program for a set number of seconds    
string Type;
int Attack;
int Defense;
int Health;
string Advantage;
string Disadvantage;
string MonsterName;
int Damage;
string OType;
int OAttack;
int ODefense;
int OHealth;
string OAdvantage;
string ODisadvantage;
string OMonsterName;
int ODamage;
long Earnings = 10;
long Money = 0;
long AmountExp = 10;
long TotalExp = 0;
long NewLevel = 0;
long LevelUp = 10;
long SkillPoints = 3;
long TotalSkillPoints = 0;
//------------------------------------------------------------------------------
class Monster
{
      public:
      // constructor
      Monster(){}
      ~Monster(){}
      //Memeber Methods      
      void DisplayStats()
      {
           cout << "\n\n\t--------Monster Stats--------";
           cout << "\n\tYou sent out a " << Type << " type.\n";
           cout << "\n\tAttack:" << Attack;
           cout << "\n\tDefense:" << Defense;
           cout << "\n\tHealth:" << Health;
           cout << "\n\tAdvantage:" << Advantage;
           cout << "\n\tDisadvantage:" << Disadvantage;
           cout << "\n\t-------------------------------\n\t";
      } 
      //Accessor Methods
      string GetType() { return Type; }
      void SetType(string x) { Type = x; }
      
      int GetAttack() { return Attack; }
      void SetAttack(int x) { Attack = x; }
      
      int GetDefense() { return Defense; }
      void SetDefense(int x) { Defense = x; }
      
      int GetHealth() { return Health; }
      void SetHealth(int x) { Health = x; }
      
      string GetAdvantage() { return Advantage; }
      void SetAdvantage(string x) { Advantage = x; }
      
      string GetDisadvantage() { return Disadvantage; }
      void SetDisadvantage(string x) { Disadvantage = x; }
      
      string GetMonsterName() { return MonsterName; }
      void SetMonsterName(string x) { MonsterName = x; }
      
      private:
      string Type;
      int Attack;
      int Defense;
      int Health;
      string Advantage;
      string Disadvantage;
      string MonsterName;
};
//------------------------------------------------------------------------------
class Player
{
public:
       
Player(){}

void PlayerCard()
{
cout << "Here " << PlayerName << " This is your I'D card I have created for you.\n";
cout << "-----------------------------\n";
cout << "| ID CARD |                 |\n";
cout << "-----------------------------\n";
cout << "| NAME:   " << PlayerName << "\n";
cout << "| AGE:    " << PlayerAge << "\n";
cout << "| GENDER: " << PlayerGender << "\n";
cout << "=============================" << "\n\n\n";
pause(8);
}

string GetPlayerName() { return PlayerName; }
void SetPlayerName(string x) { PlayerName = x; }

string GetPlayerAge() { return PlayerAge; }
void SetPlayerAge(string x) { PlayerAge = x; }

string GetPlayerGender() { return PlayerGender; }
void SetPlayerGender(string x) { PlayerGender = x; }
private:
        
string PlayerName;
string PlayerAge;
string PlayerGender;
};
//------------------------------------------------------------------------------
void YourFireType()
{
Monster YourFireType;
YourFireType.SetType("fire");
YourFireType.SetAttack(8);
YourFireType.SetDefense(1);
YourFireType.SetHealth(40);
YourFireType.SetAdvantage("Nature");
YourFireType.SetDisadvantage("Water");
YourFireType.SetMonsterName("YourFireType");
YourFireType.DisplayStats();

Type = YourFireType.GetType();
Attack = YourFireType.GetAttack();
Defense = YourFireType.GetDefense();
Health = YourFireType.GetHealth();
Advantage = YourFireType.GetAdvantage();
Disadvantage = YourFireType.GetDisadvantage();
MonsterName = YourFireType.GetMonsterName();
system("PAUSE");
}
//------------------------------------------------------------------------------
void YourWaterType()
{
Monster YourWaterType;
YourWaterType.SetType("water");
YourWaterType.SetAttack(8);
YourWaterType.SetDefense(1);
YourWaterType.SetHealth(40);
YourWaterType.SetAdvantage("Fire");
YourWaterType.SetDisadvantage("Nature");
YourWaterType.SetMonsterName("YourWaterType");
YourWaterType.DisplayStats();

Type = YourWaterType.GetType();
Attack = YourWaterType.GetAttack();
Defense = YourWaterType.GetDefense();
Health = YourWaterType.GetHealth();
Advantage = YourWaterType.GetAdvantage();
Disadvantage = YourWaterType.GetDisadvantage();
MonsterName = YourWaterType.GetMonsterName();
system("PAUSE");
}
//------------------------------------------------------------------------------
void YourNatureType()
{
Monster YourNatureType;
YourNatureType.SetType("nature");
YourNatureType.SetAttack(8);
YourNatureType.SetDefense(1);
YourNatureType.SetHealth(40);
YourNatureType.SetAdvantage("Water");
YourNatureType.SetDisadvantage("Fire");
YourNatureType.SetMonsterName("YourNatureType");
YourNatureType.DisplayStats();

Type = YourNatureType.GetType();
Attack = YourNatureType.GetAttack();
Defense = YourNatureType.GetDefense();
Health = YourNatureType.GetHealth();
Advantage = YourNatureType.GetAdvantage();
Disadvantage = YourNatureType.GetDisadvantage();
MonsterName = YourNatureType.GetMonsterName();
system("PAUSE");
}
//------------------------------------------------------------------------------
void OpponentFireType()
{
Monster OpponentFireType;
OpponentFireType.SetType("fire");
OpponentFireType.SetAttack(8);
OpponentFireType.SetDefense(1);
OpponentFireType.SetHealth(40);
OpponentFireType.SetAdvantage("Nature");
OpponentFireType.SetDisadvantage("Water");
OpponentFireType.SetMonsterName("OpponentFireType");
OpponentFireType.DisplayStats();

OType = OpponentFireType.GetType();
OAttack = OpponentFireType.GetAttack();
ODefense = OpponentFireType.GetDefense();
OHealth = OpponentFireType.GetHealth();
OAdvantage = OpponentFireType.GetAdvantage();
ODisadvantage = OpponentFireType.GetDisadvantage();
OMonsterName = OpponentFireType.GetMonsterName();
system("PAUSE");
}
//------------------------------------------------------------------------------
void OpponentWaterType()
{
Monster OpponentWaterType;
OpponentWaterType.SetType("water");
OpponentWaterType.SetAttack(8);
OpponentWaterType.SetDefense(1);
OpponentWaterType.SetHealth(40);
OpponentWaterType.SetAdvantage("Fire");
OpponentWaterType.SetDisadvantage("Nature");
OpponentWaterType.SetMonsterName("OpponentWaterType");
OpponentWaterType.DisplayStats();

OType = OpponentWaterType.GetType();
OAttack = OpponentWaterType.GetAttack();
ODefense = OpponentWaterType.GetDefense();
OHealth = OpponentWaterType.GetHealth();
OAdvantage = OpponentWaterType.GetAdvantage();
ODisadvantage = OpponentWaterType.GetDisadvantage();
OMonsterName = OpponentWaterType.GetMonsterName();
system("PAUSE");
}
//------------------------------------------------------------------------------
void OpponentNatureType()
{
Monster OpponentNatureType;
OpponentNatureType.SetType("nature");
OpponentNatureType.SetAttack(9);
OpponentNatureType.SetDefense(1);
OpponentNatureType.SetHealth(40);
OpponentNatureType.SetAdvantage("Water");
OpponentNatureType.SetDisadvantage("Fire");
OpponentNatureType.SetMonsterName("OpponentNatureType");
OpponentNatureType.DisplayStats();

OType = OpponentNatureType.GetType();
OAttack = OpponentNatureType.GetAttack();
ODefense = OpponentNatureType.GetDefense();
OHealth = OpponentNatureType.GetHealth();
OAdvantage = OpponentNatureType.GetAdvantage();
ODisadvantage = OpponentNatureType.GetDisadvantage();
OMonsterName = OpponentNatureType.GetMonsterName();
system("PAUSE");
}
//------------------------------------------------------------------------------ 
Last edited on
PART 2 (comments above part 1)

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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
void Player1()
{
Player Player1;

string string1;
cout << "What is your name? ";
cin >> string1;
Player1.SetPlayerName(string1);

cout << "What is your age? ";
cin >> string1; 
Player1.SetPlayerAge(string1);

cout << "What is your gender? ";
cin >> string1;
Player1.SetPlayerGender(string1);
Player1.PlayerCard();

string1.clear();
}
//------------------------------------------------------------------------------
void YourType()
{
     string CharacterDetails;

     cout << "What character do you want?\n\n";
     cout << "There is a fire type who specializes with fire attacks. (fire)\n";
     cout << "There is a water type who specializes with water attacks. (water)\n";
     cout << "There is a nature type who specializes with nature attacks. (nature)\n\n";
     cout << "Please enter the type of the monster you would like:";
     cin >> CharacterDetails;     
     system("CLS");
     if(CharacterDetails == "fire")
     {
     YourFireType();
     }
     else if(CharacterDetails == "water")
     {
     YourWaterType();
     }
     else{ 
     YourNatureType();
     }
     system("CLS");
}
//------------------------------------------------------------------------------
void OponentType()
{
  int TypeOfCharacter;
 
/* rand example: guess the number */
#include <stdio.h>

  /* initialize random seed: */
  srand ( time(NULL) );

  TypeOfCharacter = rand() % 3 + 1;

    if (TypeOfCharacter == 1)
    {
    OpponentFireType();
    }
    else if (TypeOfCharacter == 2)
    {
    OpponentWaterType();
    }
    else
    {
    OpponentNatureType();
    }
}
//------------------------------------------------------------------------------
pair<int,int> TypeAdvantages()
{
//Fire vs Fire
     if(Type == "fire" && OType == "fire") 
     { 
     Attack = Attack + 0;
     OAttack = OAttack + 0;
     } 
//Fire vs Water
     if(Type == "fire" && OType == "water") 
     { 
     Attack = Attack / 2;
     OAttack = OAttack * 2;
     }
//Fire vs Nature
     if(Type == "fire" && OType == "nature")
     { 
     Attack = Attack * 2;
     OAttack = OAttack / 2;
     }
//Water vs Fire
     if(Type == "water" && OType == "fire") 
     { 
     Attack = Attack * 2;
     OAttack = OAttack / 2; 
     } 
//Water vs Water
     if(Type == "water" && OType == "water") 
     { 
     Attack = Attack + 0;
     OAttack = OAttack + 0;
     }
//Water vs Nature
     if(Type == "water" && OType == "nature")
     { 
     Attack = Attack / 2;
     OAttack = OAttack * 2;
     }
//Nature vs Fire
     if(Type == "nature" && OType == "fire") 
     { 
     Attack = Attack / 2;
     OAttack = OAttack * 2; 
     } 
//Nature vs Water
     if(Type == "nature" && OType == "water") 
     { 
     Attack = Attack * 2;
     OAttack = OAttack /2;
     }
//Nature vs Nature
     if (Type == "nature" && OType == "nature")
     { 
     Attack = Attack + 0;
     OAttack = OAttack + 0;
     }
     return make_pair (Attack,OAttack);
}
//------------------------------------------------------------------------------
void Rules()
{ 
     cout << "Each of your characters battle.\n";
     cout << "You each alternate by taking turns. \n";
     cout << "Since you are a beginner you will go first.\n";
     pause(5);
     system("CLS");
}
//------------------------------------------------------------------------------
void Battlephase()
{ 
     cout <<"Let the battle begin!\n";
     pause(5); 
     YouAttack:
     system("CLS");
     { 
          if(Health > 0)
          {
                    {
                                Damage = Attack - ODefense;
                                if(Damage <=1)
                                {
                                Damage = 1;
                                }
                                cout << "You attacked and dealt " << Damage <<" Damage\n";
                                OHealth = OHealth - Damage; 
                                cout << "Your opponents health is " << OHealth << "\n\n";
                    }
          if(OHealth <= 0) 
                    { 
                                cout <<"You Win!"<< "\n\n";
                    }
                    pause(5);
          if (OHealth > 0)goto OpponentAttack;
          }
     }
     OpponentAttack:
     system("CLS");                    
     {
          if(OHealth > 0)
          {
                     {          
                                ODamage = OAttack - Defense;
                                if(ODamage <=1)
                                {
                                ODamage = 1;
                                }
                                cout << "Your opponent attacked and dealt " << ODamage <<" Damage\n"; 
                                Health = Health - ODamage; 
                                cout << "Your health is " << Health << "\n\n";
                     }     
          if(Health <= 0) 
                     {  
                     cout <<"You Lose :(\n\n";
                     }
                     pause(5); 
          if (Health > 0)goto YouAttack;
          }
     }     
     system("CLS");
}
//------------------------------------------------------------------------------
void Earned()
{
     Money += Earnings;
     cout << "You earned $" << Earnings << " and " << AmountExp << " exp.\n";
     TotalExp += AmountExp;
     pause(3);
     cout << "You now have $" << Money << " and " << TotalExp << " exp.\n";
     pause(5);
     system("CLS");
}
//------------------------------------------------------------------------------
void Level()
{
            if(TotalExp >= LevelUp)
            {
LevelUp: 
            NewLevel += + 1;
            LevelUp = LevelUp * 2 + 10;
            TotalSkillPoints += + SkillPoints;
            cout << "You leveled up to level " << NewLevel << " and gained " << SkillPoints << " skill points.\n";
            pause(5);
            cout << "You now have " << TotalSkillPoints << " unused skill points.\n";

            pause(5);
            }
            if(TotalExp >= LevelUp)
            {
            cout << "...\n";
            pause(1);
            goto LevelUp;
            }
            pause(5);
            system("CLS");
}
//------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
string PlayAgain;
     Player1();
     replay:
     YourType();
     OponentType();
     TypeAdvantages();//yet to return a value
     Rules();
     Battlephase();
     Earned();
     Level();
     cout << "Would you like to play again? y o n"<< endl;
     cin >> PlayAgain;
     if(PlayAgain == "y")
     {
     goto replay;
     }
     system("PAUSE");
	 return 0;
}
//-----------------------------------------------------
void pause(int dur)
{
int temp = time(NULL) + dur;

while(temp > time(NULL));
}
Last edited on
I've already shown you how to change your program to use classes and eliminate globals. The code you posted has all of the problems that we've worked through over the past few days. You need to take what you've learned and apply it to your code.
ill try, ill get back to you after my attempt, btw thanks for helping me
Topic archived. No new replies allowed.