Problem with do-while

Hi iam new with c++ i started 2 days ago and in order to learn some basic stuff i decided to make a text game.
My problem is that at (He>0 or Hp>0) the program only uses the one i give the larger value in order to end the loop, the other can go negative and the program will still 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
 #include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

void combat()
{
    char x;
    int Hp=20;
    int He=20;
    srand((unsigned)time(0));
   do
    {cout <<"what is your action??"<< endl;
     cout <<"[A]ttack" << endl;
     cin >> x;
     if (x == 'A')
     {cout << "you attack!"<< endl;
     int Ap=(rand()%10)+1;
     cout <<"You inflict "<< Ap<<" dmg"<< endl;
     He -= Ap;
     cout <<"Enemy has "<< He<<" Hit points"<< endl;}
     cout <<"enemy attacks!"<< endl;
    int e=(rand()%10)+1;
    cout <<"enemy inflicts "<< e <<" damage" << endl;
    Hp -= e;
    cout << "your current HP is " << Hp<< endl;
    }while (He>0 or Hp>0);
}



int main()
{
combat();
}
Last edited on
closed account (Dy7SLyTq)
make it an and
Thank you very much it works!
Question though doesnt 'and' mean both of them have to be true, in this case doesnt it mean both 'Hp' and 'He' have to become 0 or less in order to end the loop?
closed account (Dy7SLyTq)
yes
Im confused when i run the program only one of them have to become 0 or less with 'and' in order to end the loop (thats what i wanted though thanks again!).
Last edited on
Question though doesnt 'and' mean both of them have to be true, in this case doesnt it mean both 'Hp' and 'He' have to become 0 or less in order to end the loop?

No. It means both conditions have to be true for the loop to keep looping.

If either one of those conditions stops being true, then your loop condition (He>0 or Hp>0) stops being true, and the loop will stop looping.
closed account (Dy7SLyTq)
he said and not or so he is actually right
Sure. For an "and" condition to be true, both the conditions need to be true.

The problem with the statement was to do with what happens to the loop when the condition is true, and what happens when it's false. That's what I was correcting.
closed account (Dy7SLyTq)
sorry got confused cause u referenced the and statement so i thought you were talking about that
thank you both :)
Last edited on
You're welcome :)
Topic archived. No new replies allowed.