My Textbased RPG.

Pages: 12
I'm very new to c++, but I kind of know enough so I started making a text based game just so I could test ideas and see exactly how things work together. Right now though, I am attempting to start making things that attack. This is not a part of my main program, this is just testing how exactly I am going to do it. Problem I am running into is 1. I don't know that much about classes, and 2. I can't figure out what I need to use in order to get a random number between the two data members without it adding 2 to the mix.

When I compile this, I get random numbers between 3 and 8. So I am assuming that it is adding 3 from 0 to 2. My question is how can I get just a random number between m_DmgMin (which is 3) and m_DmgMax (which is 6)? I can't quite figure out what I need to use. I got the idea so far, and I apologize if my code is sloppy to you. Any ideas?

Also one more question I just remembered. I also cannot figure out how to make this number random each time. I started a loop with the line "cout << randomAutoAttack << endl;" and it just gives the same random number as before over and over. Obviously, I want the "damage" to be between 3 and 6. No more than 6, no less than 3.

And I know, I included all those libraries because I use this file to test pretty much every idea before I put it into the main program.



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

class Mob
{
      public:
             int m_DmgMax;
             int m_DmgMin;
             int m_health;
             string m_name;
             
             void Appear();
             };
      void Mob::Appear()
      {
           cout << m_name << " HP: " << m_health << endl;
           }
             
             

int main()
{
    
   Mob pirate;
   
   pirate.m_DmgMin = 3;
   pirate.m_DmgMax = 6;
   pirate.m_name = "Pirate";
   pirate.m_health = 30;
   pirate.Appear();
   srand(time(0));
   int randomAutoAttack = rand() % pirate.m_DmgMax + pirate.m_DmgMin;
   
   
   cout << randomAutoAttack << endl;
    
   
  
    system("PAUSE");
    return 0;
    
}
Last edited on
get the difference between DmgMax and DmgMin use the modulo operator on that and add the min damage
Could you use an example? I mean I understand what you said but I am having trouble putting it in c++.
int randomAutoAttack = rand() % (pirate.m_DmgMax - pirate.m_DmgMin + 1) + pirate.m_DmgMin;
Very nice. Now I will work with this so I can understand it better. But I still don't understand how to have randomAutoAttack be random each time I use it. Say it randomizes to 4, it will stay 4. So I reassign it with the same line "randomAutoAttack = rand() % (pirate.m_DmgMax - pirate.m_DmgMin + 1) + pirate.m_DmgMin;" does it make it randomize the numbers again when I sent randomAutoAttack to cout?
Last edited on
Suppose you have two numbers, min and max. In general rand()%n returns a number from 0 to n-1.
So, rand()%(max-min+1) will return a number from 0 to max-min. If you add min to that, you get a number from min to max.

randomAutoAttack = rand() % (pirate.m_DmgMax - pirate.m_DmgMin + 1) + pirate.m_DmgMin; gives a random number from pirate.m_DmgMin to pirate.m_DmgMax every time you use it because rand() gives a random number every time you use it.

Last edited on
Ok well say I have and int that represents the players health like int health = 100;. How can I deduct the random number given from health? if I can figure that out, I can start toying with stat modifiers that I put in place in the main program. I have ints set up like defense, say defense is 2, and say I want 2 defense points to deflect 1 damage from the mob that attacks the player, once I figure these out I can start putting mobs in the game, randomizing the type of mobs that attack, use my vector I set up for the players inventory and have mobs drop items that go into the vector.
health=health-randomAutoAttack; OR health-=randomAutoAttack;

I think it would be useful to check out this here -> http://cplusplus.com/doc/tutorial/operators/

EDIT:

Also one more question I just remembered. I also cannot figure out how to make this number random each time. I started a loop with the line "cout << randomAutoAttack << endl;" and it just gives the same random number as before over and over. Obviously, I want the "damage" to be between 3 and 6. No more than 6, no less than 3.

I just saw that... Add srand(time(0)); first thing inside your main function.

EDIT 2:

No, forget it... I just saw that you do call srand(time(0));

EDIT 3:

Are you making some kind of text based WoW? :D
Last edited on
To make it simpler I used a function with pointers (Sometimes I don't think I am doing pointers right.) But anyways, I call the function twice below, and both times it gives me the same number. I don't understand why since rand() calls for a random number every time. It seems like it is just initializing the number and staying the original random number generated.




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

void autoAttack(int* pm_DmgMax, int* pm_DmgMin);

class Mob
{
      public:
             int m_DmgMax;
             int m_DmgMin;
             int m_health;
             string m_name;
             
             void Appear();
             };
      void Mob::Appear()
      {
           cout << m_name << " HP: " << m_health << endl;
           }
             
             

int main()
{
    
   Mob pirate;
   
   pirate.m_DmgMin = 3;
   pirate.m_DmgMax = 6;
   pirate.m_name = "Pirate";
   pirate.m_health = 30;
   pirate.Appear();
   srand(time(0));
   autoAttack(&pirate.m_DmgMax, &pirate.m_DmgMin);
   autoAttack(&pirate.m_DmgMax, &pirate.m_DmgMin);
  
    system("PAUSE");
    return 0;
    
}

void autoAttack(int* pm_DmgMax, int* pm_DmgMin)
{
     srand(time(0));
     int randomAutoAttack = rand() % (*pm_DmgMax - *pm_DmgMin + 1) + *pm_DmgMin;
     cout << randomAutoAttack << endl;
}


Edit.
No texted based WoW. I've been an MMO gamer for years, I hate what WoW has done to the MMO market. Funny you mention that. The main reason I started learning c++ is so when I get out of the Army (In Iraq right now :/) I can begin my quest to become a game programmer and already have knowledge of programming games. I plan on learning dirct x and openGL so I can actually use a GUI with my games. I'm not really making anything for anyone. I am just making a texted based game on my ideas of balance and combat. I don't believe in levels, I believe in skill. Needless to say, I got a looooooong road ahead of me.
Last edited on
1
2
3
4
5
6
void autoAttack(int* pm_DmgMax, int* pm_DmgMin)
{
     //call this only once in your program -> srand(time(0));
     int randomAutoAttack = rand() % (*pm_DmgMax - *pm_DmgMin + 1) + *pm_DmgMin;
     cout << randomAutoAttack << endl;
}
Aha! You are awesome, thank you so much for you help. Maybe one day you'll play one of my games, and go, "Wow, I helped that guy a few years back, now look at him, BEST GAME I HAVE EVER PLAYED."

Anyways, thanks a lot. I'm sure I'll be back with more retarded questions for you c++ geniuses.
Already back, confused more than before. My problem is with the same code, except this time it's a little more hard to explain.

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

void autoAttack(string* pm_name, int* pm_DmgMax, int* pm_DmgMin, int* phealth, int* pdefense, int* pblock);

class Mob
{
      public:
             int m_DmgMax;
             int m_DmgMin;
             int m_health;
             string m_name;
             
             void Appear();
             };
      void Mob::Appear()
      {
           cout << m_name << " HP: " << m_health << endl;
           }
             
             

int main()
{
    
    int health = 100;
    int defense = 2;
    int block = 50;
    
    //==============
   Mob pirate;
   
   pirate.m_DmgMin = 3;
   pirate.m_DmgMax = 6;
   pirate.m_name = "Pirate";
   pirate.m_health = 30;
   pirate.Appear();
   srand(time(0));
   autoAttack(&pirate.m_name, &pirate.m_DmgMax, &pirate.m_DmgMin, &health, &defense, &block);
  
    system("PAUSE");
    return 0;
    
}

void autoAttack(string* pm_name, int* pm_DmgMax, int* pm_DmgMin, int* phealth, int* pdefense, int* pblock)
{
     int randomAutoAttack = rand() % (*pm_DmgMax - *pm_DmgMin + 1) + *pm_DmgMin;
     *phealth = *phealth - randomAutoAttack + *pdefense;
     cout << *pm_name << " attacks you for " << randomAutoAttack << " point(s) of damage." << endl;
     cout << "You armor deflected " << *pdefense << " point(s) of damage" << endl;
     cout << "You have " << *phealth << " HP's remaining." << endl;
}


So here is what I have done to it so far. My problem is this. I set up int block = 50; to basically represent 50% chance to block randomAutoAttack. I need to figure out how out of a possible 100% chance of blocking, making maybe an if statement followed by and else statement give it a 50% chance to follow the if statement rather than the else

Something like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
void autoAttack(string* pm_name, int* pm_DmgMax, int* pm_DmgMin, int* phealth, int* pdefense, int* pblock)
{
     int randomAutoAttack = rand() % (*pm_DmgMax - *pm_DmgMin + 1) + *pm_DmgMin;
     *phealth = *phealth - randomAutoAttack + *pdefense;

   if (//some equation for 50% chance of block success)
          cout << "You blocked " << *pm_name << "'s attack" << endl;

   else
     cout << *pm_name << " attacks you for " << randomAutoAttack << " point(s) of damage." << endl;
     cout << "You armor deflected " << *pdefense << " point(s) of damage" << endl;
     cout << "You have " << *phealth << " HP's remaining." << endl;
}


Any idea? I don't mean to have people code stuff for me but I really haven't tapped into the mathematics of c++ yet and what does what to get a certain outcome.
1
2
3
4
if (rand()%100 + 1 > 100-block_chance)
    cout << "blocked!" << endl;
else
    cout << "didn't block!" << endl;
Awesome, looks like that could also work for stat modifiers as well. Thanks for saving me the headache.
Back once again, but this time I tried everything before I came here because I feel I won't learn if I don't figure it out myself, but I can also learn this way. I have tweaked it, I put in block, I think I already have a good plan for putting strength in, and making it add a certain amount of damage depending on how much they have. but right now I am trying to get multiple monsters set as classes to attack in a random order. The program just loops the fight sequences over until the fight is won, and starts all over again. The plan is to get it to be a different monster each time a new fight sequence starts. I tried just two monsters, got it working perfectly. Getting a random number out of just 2 numbers is easy I guess. But for some reason when I add another mob to the mix, well, compile it yourself. You'll see that it picks the same monster first each time for about 5 fight sequences, then the other one for the rest of the fight sequences. Here is the code so far.

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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <ctime>

using namespace std;

void autoAttack(string* pm_name, int* pm_DmgMax, int* pm_DmgMin, int* phealth, int* pdefense, int* pblock, int* pm_health); //This is for the Mobs attack AI

void playerAttack(int* pplayerDmgMax, int* pplayerDmgMin, int* pm_health, string* pm_name, int* pm_healthConst, int* phealth); //This is for player attack sequence

void randomMobGen(int* pm_mobID1, int* pm_mobID2, int* phealth, int* prandomMob);

class Mob
{
      public:
             int m_DmgMax;
             int m_mobID;
             int m_DmgMin;
             int m_health;
             string m_name;
             int m_healthConst;
             
              void Appear();
              void Health(int* phealthConst, int* pm_health);
             };
             
      void Mob::Appear()
      {
           cout <<  m_name <<  " HP: [" << m_health << "]\n\n" << endl;
           }
      void Mob::Health(int* phealthConst, int* pm_health)
      {
           *phealthConst = *pm_health;
           }

int main()
{
    //These variables represent the players stats.
    int health = 100;
    int charDef = 2;
    int charBlock = 10;
    int playerDmgMax = 8;
    int playerDmgMin = 18;
    
   Mob pirate;
   
   pirate.m_mobID = 1;
   pirate.m_DmgMin = 3;
   pirate.m_DmgMax = 6;
   pirate.m_name = "Pirate";
   pirate.m_health = 30;
   pirate.m_healthConst = 30;
   srand(time(0));
   
   Mob bandit;
   
   bandit.m_mobID = 2;
   bandit.m_DmgMin = 10;
   bandit.m_DmgMax = 16;
   bandit.m_name = "Bandit";
   bandit.m_health = 50;
   bandit.m_healthConst = 50;
   
      Mob wildbear;
   
   bandit.m_mobID = 3;
   bandit.m_DmgMin = 11;
   bandit.m_DmgMax = 20;
   bandit.m_name = "Wild Bear";
   bandit.m_health = 70;
   bandit.m_healthConst = 70;
   
   
   
   
   
   
   
   //Random mob battle sequences
   int randomMob = rand() % (wildbear.m_mobID - pirate.m_mobID + 1) + pirate.m_mobID;
   
   //Bandit Mob
   do
   {
  if (randomMob == bandit.m_mobID)
  
                 bandit.Appear();
   autoAttack(&bandit.m_name, &bandit.m_DmgMax, &bandit.m_DmgMin, &health, &charDef, &charBlock, &bandit.m_health);
   system("PAUSE");
   cout << "\n\n" << endl;
   playerAttack(&playerDmgMax, &playerDmgMin, &bandit.m_health, &bandit.m_name, &bandit.m_healthConst, &health);
   system("PAUSE");
   cout << "\n\n" << endl;
}while (bandit.m_health > 1);


       //Pirate Mob
   do
   {
   if (randomMob == pirate.m_mobID)
   
                 pirate.Appear();
   autoAttack(&pirate.m_name, &pirate.m_DmgMax, &pirate.m_DmgMin, &health, &charDef, &charBlock, &pirate.m_health);
   system("PAUSE");
   cout << "\n\n" << endl;
   playerAttack(&playerDmgMax, &playerDmgMin, &pirate.m_health, &pirate.m_name, &pirate.m_healthConst, &health);
   system("PAUSE");
   cout << "\n\n" << endl;
}while (pirate.m_health > 1);
  
     do
   {
   if (randomMob == wildbear.m_mobID)
   
                 wildbear.Appear();
   autoAttack(&wildbear.m_name, &wildbear.m_DmgMax, &wildbear.m_DmgMin, &health, &charDef, &charBlock, &wildbear.m_health);
   system("PAUSE");
   cout << "\n\n" << endl;
   playerAttack(&playerDmgMax, &playerDmgMin, &wildbear.m_health, &wildbear.m_name, &wildbear.m_healthConst, &health);
   system("PAUSE");
   cout << "\n\n" << endl;
}while (wildbear.m_health > 1);
  
 randomMobGen(&wildbear.m_mobID, &pirate.m_mobID, &health, &randomMob);
  
  
  
  
  
  
  
  
  
  
  
  
  
    system("PAUSE");
    return 0;
    
}


//This function is for the mobs attack AI
void autoAttack(string* pm_name, int* pm_DmgMax, int* pm_DmgMin, int* phealth, int* pdefense, int* pblock, int* pm_health)
{
    if (rand()% 100 + 1 > 100 - *pblock)
    {
                cout << "You block " << *pm_name << "'s attack.\n\n\n" << endl;
                }
    else
    {
     int randomAutoAttack = rand() % (*pm_DmgMax - *pm_DmgMin + 1) + *pm_DmgMin;
     *phealth = *phealth - randomAutoAttack + *pdefense;
     cout << *pm_name << " attacks you for " << randomAutoAttack << " point(s) of damage.\n" << endl;
     cout << "You armor deflected " << *pdefense << " point(s) of damage\n" << endl;
     cout << "You have " << *phealth << " HP's remaining.\n\n\n\n\n\n" << endl;
     }
}

//Player attack input

void playerAttack(int* pplayerDmgMax, int* pplayerDmgMin, int* pm_health, string* pm_name, int* pm_healthConst, int* phealth)
{
     
     int randomAutoAttack = rand() % (*pplayerDmgMax - *pplayerDmgMin + 1) + *pplayerDmgMin;  
     *pm_health = *pm_health - randomAutoAttack;
     cout << "You attack " << *pm_name << " for " << randomAutoAttack << " point(s) of damage.\n" << endl;
     cout << *pm_name << " has " << *pm_health << "HP's ramining.\n\n\n" << endl;
     
     if (*pm_health < 1)
     {
       cout << "\n\n\n" << *pm_name << " has been slain!\n" << endl;
       *pm_health = *pm_healthConst;
       *phealth = 100;
       cout << "Your health has been replenished.\n\n\n" << endl;
       }
     }
     
void randomMobGen(int* pm_mobID1, int* pm_mobID2, int* phealth, int* prandomMob)
{
     if (*phealth == 100)
    *prandomMob = rand() % (*pm_mobID1 - *pm_mobID2 + 1) + *pm_mobID2;
}



Now, I think the problem may also lie with the fact that the loop starts with the random number that is picked first and then the random number isn't randomized again. But how can that be because when I had just two Mob classes, Mob pirate, and Mob bandit, it chose those at random each time, but when I add Mob wildbear, all of the sudden it just craps on itself and doesn't work right anymore. You can see what I did here int randomMob = rand() % (wildbear.m_mobID - pirate.m_mobID + 1) + pirate.m_mobID;. Now I assigned a mobID to each Mob class, 1, 2, and 3 so far. I also tried this in a new program with the same idea just using numbers.



1
2
3
4
5
6
7
8
9
10
11
12
13
int main()

{
              srand(time(0));
  int numb1 = 1;
  int numb2 = 2;
  int numb3 = 3;
  int numb4 = 4;
  int numb5 = 5;
  int numb6 = 6;
  
  int randomNumber = rand() % (numb6 - numb1 + 1) + numb1;
    cout << randomNumber << endl;

Works, always gets a number between 1 and 6.
(I know I can just make it give a number between 1 and 6 by using rand() % 6 but I wanted to see if I could get it to choose a loop using if (randNumber == numb#)


I take the biggest number which in this case would be wildbear.m_mobID, and get the difference between it and the smallest number pirate.m_mobID, but doesn't work quite as well. So my question is what did I do wrong? It can't be int randomMob, it must be my loops, which I suck at horribly. Any suggestions?

Also, is there a way I can put my classes into my own library so I don't have to type each one out in the main body?
Last edited on
Take a look here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
   Mob bandit;
   
   bandit.m_mobID = 2;
   bandit.m_DmgMin = 10;
   bandit.m_DmgMax = 16;
   bandit.m_name = "Bandit";
   bandit.m_health = 50;
   bandit.m_healthConst = 50;
   
      Mob wildbear;
   
   //all of these should be wildbear.m_...

   bandit.m_mobID = 3;
   bandit.m_DmgMin = 11;
   bandit.m_DmgMax = 20;
   bandit.m_name = "Wild Bear";
   bandit.m_health = 70;
   bandit.m_healthConst = 70;

But this is not the real problem... Check this out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Bandit Mob
   do
   {
  if (randomMob == bandit.m_mobID) // <- this won't work as you expect it to...
  
                 bandit.Appear();
   autoAttack(&bandit.m_name, &bandit.m_DmgMax, &bandit.m_DmgMin, &health, &charDef,
&charBlock, &bandit.m_health);
   system("PAUSE");
   cout << "\n\n" << endl;
   playerAttack(&playerDmgMax, &playerDmgMin, &bandit.m_health, &bandit.m_name,
&bandit.m_healthConst, &health);
   system("PAUSE");
   cout << "\n\n" << endl;
}while (bandit.m_health > 1); // <- also notice this here, it is important 

if (randomMob == bandit.m_mobID) should be if (randomMob != bandit.m_mobID) break;
Same for the other mobs.

Now look at this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Player attack input

void playerAttack(int* pplayerDmgMax, int* pplayerDmgMin, int* pm_health, string* pm_name,
int* pm_healthConst, int* phealth)
{
     
     int randomAutoAttack = rand() % (*pplayerDmgMax - *pplayerDmgMin + 1) + *pplayerDmgMin;  
     *pm_health = *pm_health - randomAutoAttack;
     cout << "You attack " << *pm_name << " for " << randomAutoAttack << 
" point(s) of damage.\n" << endl;
     cout << *pm_name << " has " << *pm_health << "HP's ramining.\n\n\n" << endl;
     
     if (*pm_health < 1)
     {
       cout << "\n\n\n" << *pm_name << " has been slain!\n" << endl;
       *pm_health = *pm_healthConst; // <- this here causes the problem!!!
       *phealth = 100;
       cout << "Your health has been replenished.\n\n\n" << endl;
       }
     }

You see, when you win the mob, its health is set to maximum, so the do-while loop never ends... I suggest deleting this line. Heal the mobs somewhere else. Oh, and in the do-while loop make the condition
while (bandit.m_health > 0); instead of while (bandit.m_health > 1);

That's all I can see for now...
Last edited on
Wow, I never changed mob wildbear's data members, I am a complete moron. Then again, my organization skills suck so bad that it takes me a while to find a certain line of code, I am going to have to work on that. I will work on it a bit before I have any other questions, and try not to annoy people with ridiculous and obvious errors in my code. I'm getting better though!

I am a complete moron.
Please, don't talk about yourself like that...

I will work on it a bit before I have any other questions, and try not to annoy people with ridiculous and obvious errors in my code.
You don't annoy anyone.

I'm getting better though!
I don't doubt it!
Finally got it down, but now that I am trying to implement different attacks for the player, I run into a compile error that I don't know how to fix with this

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
void playerAttack(int* pplayerDmgMax, int* pplayerDmgMin, int* pm_health, string* pm_name, int* pm_healthConst, int* phealth, int* pattackChoice,
 int* pspcThrust, int* pspcLegSweepMin, int* pspcLegSweepMax, int* pspcLegSweep)
{
     cout << "[1] Auto Attack " << "   [2] Thrust " << "   [3] Leg Sweep" << endl;
     cin >> *pattackChoice;
     
     switch (*pattackChoice)
     {
           case 1:
                int randomAutoAttack = rand() % (*pplayerDmgMax - *pplayerDmgMin + 1) + *pplayerDmgMin;  
     *pm_health = *pm_health - randomAutoAttack;
     cout << "You attack " << *pm_name << " for " << randomAutoAttack << " point(s) of damage.\n" << endl;
     cout << *pm_name << " has " << *pm_health << "HP's remaining.\n\n\n" << endl;
     break;
           
           case 2:
                int* pspcThrust = rand() % (*pplayerDmgMax - *pplayerDmgMin) + 1 + *pplayerDmgMin + 8;
                *pm_health = *pm_health - *pspcThrust;
                cout << "You use Thrust on " << *pm_name << " for " << *pspcThrust << " point(s) of damage.\n" << endl;
                cout << *pm_name << " has " << *pm_health << "HP's remaining.\n\n\n" << endl;
     break;
           
           case 3:
                int* pspcLegSweep = rand() % (*pspcLegSweepMax - *pspcLegSweepMin) + 1 + *pspcLegSweepMin;
                *pm_health = *pm_health - *pspcLegSweep;
                cout << "You use Leg Sweep on " << *pm_name << " for " << *pspcLegSweep << " point(s) of damage.\n" << endl;
                cout << *pm_name << " has " << *pm_health << "HP's remaining.\n\n\n" << endl;
     break;
           
           default:
                   cout << "Not one of your attacks." << endl;
                }
     
     
   
     }


It is telling me
In function `void playerAttack(int*, int*, int*, std::string*, int*, int*, int*, int*, int*, int*, int*)':
jump to case label
crosses initialization of `int randomAutoAttack'
invalid conversion from `int' to `int*'
jump to case label
crosses initialization of `int*pspcThrust'
crosses initialization of `int randomAutoAttack'
invalid conversion from `int' to `int*'

I guess there is a issue with ints trying to reassign to other ints? I have no idea what I did wrong, probably another obvious typo or something. Anyone?
Yes, this is a common problem when you want to declare variables inside a switch statement. Solve it by placing the content of your cases inside brackets {,} like this:

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
void playerAttack(int* pplayerDmgMax, int* pplayerDmgMin, int* pm_health, string* pm_name, int* pm_healthConst, int* phealth, int* pattackChoice,
 int* pspcThrust, int* pspcLegSweepMin, int* pspcLegSweepMax, int* pspcLegSweep)
{
     cout << "[1] Auto Attack " << "   [2] Thrust " << "   [3] Leg Sweep" << endl;
     cin >> *pattackChoice;
     
     switch (*pattackChoice)
     {
           case 1:{
                int randomAutoAttack = rand() % (*pplayerDmgMax - *pplayerDmgMin + 1) + *pplayerDmgMin;  
     *pm_health = *pm_health - randomAutoAttack;
     cout << "You attack " << *pm_name << " for " << randomAutoAttack << " point(s) of damage.\n" << endl;
     cout << *pm_name << " has " << *pm_health << "HP's remaining.\n\n\n" << endl;
     }break;
           
           case 2:{
                int* pspcThrust = rand() % (*pplayerDmgMax - *pplayerDmgMin) + 1 + *pplayerDmgMin + 8;
                *pm_health = *pm_health - *pspcThrust;
                cout << "You use Thrust on " << *pm_name << " for " << *pspcThrust << " point(s) of damage.\n" << endl;
                cout << *pm_name << " has " << *pm_health << "HP's remaining.\n\n\n" << endl;
     }break;
           
           case 3:{
                int* pspcLegSweep = rand() % (*pspcLegSweepMax - *pspcLegSweepMin) + 1 + *pspcLegSweepMin;
                *pm_health = *pm_health - *pspcLegSweep;
                cout << "You use Leg Sweep on " << *pm_name << " for " << *pspcLegSweep << " point(s) of damage.\n" << endl;
                cout << *pm_name << " has " << *pm_health << "HP's remaining.\n\n\n" << endl;
     }break;
           
           default:
                   cout << "Not one of your attacks." << endl;
                }
     
     
   
     }


EDIT: There are some syntax errors though...
Last edited on
Pages: 12