Problem With Operators?

Here's my code

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
  #include <iostream>
#include <string>
#include <cstdlib>
#include <windows.h>

using namespace std;

int oppHP = 100;
int yourHP = 100;
int dmg = 10;

void attack()
{
    oppHP = oppHP - dmg;
}
void Oppattack()
{
    yourHP = yourHP - dmg;
}

void space()
{
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
    cout << endl;
}

void update()
{
    system("CLS");
}

int main()
{

    while((yourHP > 0) || (oppHP > 0)) // main problem seems to stem from here
    {
        cout << "Your hp: " << yourHP << "                         " << "Enemy hp: " << oppHP;
        space();
        cout << "Attack? (y/n): ";
        char input;
        cin >> input;
        if (input == 'y')
        {
            attack();
            update();
        }
        else if (input == 'n')
        {
            cout << "You get atttacked instead!" << endl;
            Sleep(500); // cin.get not recognized for some reason, used sleep instead
            Oppattack();
            update();
        }
        else
        {
            cout << "Invalid response";
            Sleep(500);
            system("CLS");
        }
    }
    if (yourHP == 0) // works fine
    {
        cout << "Game over. You lose";
        Sleep(500);
        return 0;
    }
    else if (oppHP == 0) //  isn't acknowledged and goes into negative value
    {
        cout << "Game over, you win!";
        Sleep(500);
        return 0;
    }
}


The first thing I did was review the section on operators here and what I notice the description state that the right hand side expression wasn't evaluated. Is there any way to offset this? I'm trying to keep the loop going until either value hits 0
Did you try losing instead? It's the same.

while((yourHP > 0) && (oppHP > 0))

Try that instead. If you use || either the opponents hp or your hp must drop to 0 first for the other to trigger.
Thanks, that was exactly what I was looking for. I don't exactly know what I'm not understanding, but I'm sure I'll figure that out later
 
while((yourHP > 0) || (oppHP > 0)) 

means continue while either HP is > 0. i.e. if only one HP is 0, the while condition is satisfied.

 
while((yourHP > 0) && (oppHP > 0)) 

means both HP have to be > 0 for the game to continue. If either HP reaches 0, the while condition is false and the game ends.

Oh I see lol. I was over thinking it. Thank you both for your help
Topic archived. No new replies allowed.