I'm trying to implement something similar to this in my program

Can somebody tell me what i did wrong? I am really confused. (I have over 450 lines of code in my main program and because of a similar problem to this example i cant continue.)

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 <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;

int Attack;
int Health;
class Monster
{
      public:
      Monster(){}
      ~Monster(){} 
     
      int GetAttack() { return Attack; }
      void SetAttack(int x) { Attack = x; }
    
      int GetHealth() { return Health; }
      void SetHealth(int x) { Health = x; }
           
      private:
      int Attack;
      int Health;
};

void YourFireType()
{
Monster * YourFireType = new Monster;

YourFireType->SetAttack(8);
Attack = YourFireType->SetAttack;

YourFireType->SetHealth(40);
Health = YourFireType->SetHealth;

Health = Health - Attack;
cout << Health << endl;
}

int main(int argc, char *argv[])
{
void YourFireType()
}
Last edited on
Why are "Attack" and "Health" globals? If you create more than one monster then they will be modifying the same memory causing a runtime error. Secondly you have to return an integer in the "main" statement. Thirdly, lol, what exactly are you trying to do, are you just testing? Fourth, a pointer is not necessary for a class that can be created without allocating memory yourself, via new. Fifth, you are forgetting to delete "YourFireType" and thus you cause a memory leak, which basically is a memory space that is allocated by a program and not being used but still unusable after the program is ended. Sixth your problem is a name clash by the global variables and the member variables of your class.
replace
 
void YourFireType()

with
 
YouFireType();

in the main function

Also you dont delete the monster you created in YouFireType which causes a memory leak

EDIT:Actually now that ive looked at it theres tons of problems in this program
For one:
 
Attack = YourFireType->SetAttack;

isnt valid becuase SetAttack is a void and would return a void plus you dont pass in a variable
same goes with line 35
Last edited on
yes im testing so if my test works i can implement it


UPDATED CODE //still has errors
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 <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;

int Attack;
int Health;
class Monster
{
      public:
      Monster(){}
      ~Monster(){} 
     
      int GetAttack() { return Attack; }
      void SetAttack(int x) { Attack = x; }
    
      int GetHealth() { return Health; }
      void SetHealth(int x) { Health = x; }
           
      private:
      int Attack;
      int Health;
};

void YourFireType()
{
Monster * YourFireType = new Monster;

YourFireType->SetAttack(8);
Attack == Monster.SetAttack();

YourFireType->SetHealth(40);
Health = YourFireType->SetHealth;

Health == Health - Attack;
cout << Health << endl;
}

int main(int argc, char *argv[])
{
YourFireType();
}
Last edited on
You still have a bunch of problems
You have global variables of Attack and Health plus members of Monster class with the same name which could cause confusion
This
 
Attack == Monster.SetAttack();

Should probably be
1
2
 
Attack = Monster.GetAttack();


This
 
Health = YourFireType->SetHealth;

Should probably be
 
Health = YourFireType->GetHealth();


and this
 
Health == Health - Attack;


Should be
 
Health = Health - Attack;

And you still havent deleted the Monster you created

Seems like you have no clue what you are doing, this is simple stuff. I can hardly believe you actually wrote 450 lines of code
Last edited on
thanks the Get instead os Set fixed most of my problems, i only have 1 left

C:\Documents and Settings\User\My Documents\Untitled100.cpp In function `void YourFireType()': 32 C:\Documents and Settings\User\My Documents\Untitled100.cpp expected primary-expression before '.' token )

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 <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;

int Attack;
int Health;
class Monster
{
      public:
      Monster(){}
      ~Monster(){} 
     
      int GetAttack() { return Attack; }
      void SetAttack(int x) { Attack = x; }
    
      int GetHealth() { return Health; }
      void SetHealth(int x) { Health = x; }
           
      private:
      int Attack;
      int Health;
};

void YourFireType()
{
Monster * YourFireType = new Monster;

YourFireType->SetAttack(8);
Attack = Monster.GetAttack();

YourFireType->SetHealth(40);
Health = YourFireType->GetHealth();

Health = Health - Attack;
cout << Health << endl;
}

int main(int argc, char *argv[])
{
YourFireType();
}


edit: think i fixed it changing monster to yourfiretype
Last edited on
 
Attack = Monster->GetAttack();

Should fix it.

Why are you allocating memory manually for this class?

I think you should just replace line 29 with
 
Monster YourFireType;


And then replace all -> with just .
Last edited on
still you forget to delete the object. you dont have to allocate memory for the class.. a pointer is completely unnecessary here.
Last edited on
Thanks for all your help, i got this problem fixed, so i can start working on my code.
Angeljruiz, if i replace -> with . , my other functions will create a memory leak when defining, i gave you a testing function with a simplified version of one of mine (out of 6)
You have a memory leak here.
If you wanna try to fix my memory leak problem, go ahead, ill give you my whole program (unfinished) and you fix it for me

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
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
void pause(int dur);//pauses the program for a set number of seconds
//MY VARIABLES
string SetType;       
string PlayerAge;
string PlayerName;
string PlayerGender;
string CharacterDetails;
string OpponentCharacterDetails;
string Continue;
string PlayAgain;

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;
int TypeOfCharacter;
//modefy yourtype
//modefy opponenttype
//modefy typeadvantages
//------------------------------------------------------------------------------
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;
};

//------------------------------------------------------------------------------
void YourFireType()
{
Monster * YourFireType = new Monster;
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 = new Monster;
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 = new Monster;
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 = new Monster;
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 = new Monster;
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 = new Monster;
OpponentNatureType->SetType("nature");
OpponentNatureType->SetAttack(8);
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");
}
//------------------------------------------------------------------------------
void Name()
{
cout << "Welcome to the game!\n\nSince you are starting on your journey,";
cout << "let me know a little bit about you.\n";
cout << "What is your name? ";
getline(cin,PlayerName);
cout << "\nNice to meet you " << PlayerName << ".\n";
pause(5);
system("CLS");
}
Part 2

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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//------------------------------------------------------------------------------
void Age()
{
          cout << "How old are you? ";
          getline(cin,PlayerAge);
          cout << "\nWow your, " << PlayerAge << "!! My guess was way off.\n";
          pause(5);
          system("CLS");
}
//------------------------------------------------------------------------------
void Gender()
{
          cout << "Are you Male, Female, or Alien? ";
          getline(cin,PlayerGender);
          pause(5);
          system("CLS");
}
//------------------------------------------------------------------------------
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);
}
//------------------------------------------------------------------------------
void Program()
{
     cout << "Please note this program will only work if you press ENTER after each responce.\n";
     pause (5);    
     system("CLS"); 
}
//------------------------------------------------------------------------------
void YourType()
{
     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();
     }
     pause(10);

     system("CLS");
}
//------------------------------------------------------------------------------
void OponentType()
{ 
/* rand example: guess the number */
#include <stdio.h>

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

  /* generate secret number: */
  TypeOfCharacter = rand() % 3 + 1;

    if (TypeOfCharacter == 1)
    {
    OpponentFireType();
    }
    else if (TypeOfCharacter == 2)
    {
    OpponentWaterType();
    }
    else
    {
    OpponentNatureType();
    }
}
//------------------------------------------------------------------------------
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 Attack;
}
//------------------------------------------------------------------------------
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[])
{
     Name();
     Age();
     Gender();
     PlayerCard();
     replay:
     Program();
     YourType();
     OponentType();
     TypeAdvantages();//yet to return a value
     Rules();
     Battlephase();
     Earned();
     Level();
     cout << "Would you like to play again? y o n"<< endl;
     if(PlayAgain == "y")
     {
     goto replay;
     }
     system("PAUSE"); 
	 return 0;
}
void pause(int dur)
{
int temp = time(NULL) + dur;

while(temp > time(NULL));
}
If you wanna try to fix my memory leak problem, go ahead, ill give you my whole program (unfinished) and you fix it for me


Listen, young whippersnapper, I have no desire to fix your memory leak problem. I was merely noting that you saying "I can't do this, because I'll have a memory leak" is pretty poor reasoning when what was suggested would actually fix a memory leak.

Now, stay off my lawn.
Topic archived. No new replies allowed.