why does my while statement return a infinite loop

My while statement returns a infinite loop while I want the variables to go down until creatures hp hits 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>
#include <string>

using namespace std;

      string x;
      int herolvl = 1;
      int herohp = herolvl + 19;
      int herostr = herolvl + 4;
      int creaturehp = (herolvl + 4);
      int creaturestr = (herolvl + 4);

    class attack{
          public:
          void match(){
              while(creaturehp > 0){
                  creaturehp - herostr;
                  herohp - creaturestr;
                  cout<<creaturehp << herohp; // just spams a number.
              }
        }
};
You're not changing the value of creaturehp. If you want to subtract the value of one variable from another you do this:
1
2
3
4
5
creaturehp = creaturehp - herostr;

//or use the shorthand way

creaturehp -= herostr;


You should probably avoid making all your variables global too.
Last edited on
1
2
creaturehp - herostr;
herohp - creaturestr;

these two statements is just a anonymous statements, and never modify the creaturehp and herohp.
Topic archived. No new replies allowed.