Why is loop not ending when value reaches 0?

I'm making a quick game where a player can select a move to attack an enemy, and depending on who's health reaches 0 first, wins.

I'm not sure why, but when the player's or enemy's health reaches 0, the loop doesn't immediately end as it should. It gives them one more round despite having 0 health. I have (health > 0) and I tried (health >= 0) but I still get one more loop.

I think changing min health to 20 might resolve this, but then I won't get the health status line to display. Where should I put it so that it shows up one last time when some's health reaches 0? (This line:)
"cout << "Player Health: " << phealth << " ===== VS ===== Opponent Health: " << ophealth << endl;"



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
#include <iostream>
using namespace std;


int main() {

	char move;
	bool quit = false;
	int phealth;
	int ophealth;

	cout << "===========================================" << endl;
	cout << "Welcome to the Street Fighter Tutorial Game" << endl;
	cout << "Attack the opponent until the healbar is 0!" << endl;
	cout << "===========================================" << endl;
	cout << " " << endl;

	phealth = 100;
	ophealth = 100;

	while (quit == false) {
		cout << "Player Health: " << phealth << " ===== VS ===== Opponent Health: " << ophealth << endl;
		cout << " " << endl;
		cout << "Chose one of the following moves:" << endl;
		cout << "(1) Hadouken:                          10 HP" << endl;
		cout << "(2) Shoryuken:                         15 HP" << endl;
		cout << "(3) Hurrican Kick:                     20 HP" << endl;
		cin >> move;

		if ((move == '1') && phealth >= 20) {
			cout << " " << endl;
			cout << "You did a HADOUKEN attack!         -(10HP)" << endl;
			ophealth = (ophealth - 10);
			cout << "The opponent attacked you!         -(20HP)" << endl;
			phealth = (phealth - 20);


		}
		else if ((move == '2') && phealth >= 20) {
			cout << "You did a SHORYUKEN attack!        -(15HP)" << endl;
			ophealth = (ophealth - 15);
			cout << "The opponent attacked you!         -(20HP)" << endl;
			phealth = (phealth - 20);

		}
		else if ((move == '3') && phealth >= 20) {
			cout << "You did a HURRICAN KICK attack!    -(20HP)" << endl;
			ophealth = (ophealth - 20);
			cout << "The opponent attacked you!         -(20HP)" << endl;
			phealth = (phealth - 20);

		}
		else if (phealth >= 20) {
			cout << "Please enter a valid number!" << endl;

		}
		else if (ophealth <= 0) {
			cout << "=======================================" << endl;
			cout << "================YOU WIN================" << endl;
			cout << "=======================================" << endl;
			quit = true;
		}
		else {
			cout << "=======================================" << endl;
			cout << "================YOU LOSE===============" << endl;
			cout << "=======================================" << endl;
			quit = true;

		}

	}

}

You have essentially:
IF player is alive
THEN damage opponent
ELSE check if opponent is dead

In other words you do not check after attack resolution whether you did win. You check during the next round ...
https://pastebin.com/fdD0zYKw
https://pastebin.com/mYxLfcYb
https://pastebin.com/QWM63Ncc

I tried again, but am still running into issues. So lost, have no idea what else to try.
Can you advise me on which one of these I got the closest to it working right?

Topic archived. No new replies allowed.