Not satisfying while loop

I wrote a game that's like the old Final Fantasy 1, 2, etc.
I tried to make the game... like there are two foes and two home units. You can only do damage to one of the enemy units with each turn of one of your players.
If any one of the unit dies, it ends the game.
BTW, b and w are the enemies. Vex and Fel are friendly units. Each unit has a certain amount of health and when it reaches 0 they die.

But for some reason it just goes on and on. And for some reason it displays some random thing instead of the health (something like 0x530c530 or something)

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

int main()
{
    int w_health = 100;
    int b_health = 100;
    int Vex_health = 200;
    int Fel_health = 200;
    
    while ( b_health != 0||Fel_health != 0||Vex_health != 0||w_health != 0 )
    {
    
        cout<<"w";
        cout<<"\thealth ="<<cout<<w_health;
        cout<<"\t\t\tVex";
        cout<<"\thealth="<<cout<<Vex_health;
        cout<<"\nb"<<cout<<" health="<<cout<<b_health;
        cout<<"\t\t\tFel";
        cout<<"\thealth="<<cout<<Fel_health;
        cout<<"\n\nVex's turn. Press 1 to attack w. press 2 to attack b."<<endl;
        int a;
        cin>>a;
        if ( a == 1 )
        {
            w_health = w_health-10;
        }
        
        if ( a == 2)
        {
            b_health = b_health-10;
        }
        if (Vex_health <= Fel_health)
        {
            Vex_health = Vex_health - 10;
        }
        else {
            Fel_health = Fel_health - 20;
        }
        cout<<"w";
        cout<<"\thealth ="<<cout<<w_health;
        cout<<"\t\t\t\t\t\t\t\t\t\t\t\tVex";
        cout<<"\thealth="<<cout<<Vex_health;
        cout<<"\nb"<<cout<<"\t\t"<<cout<<b_health;
        cout<<"\t\t\t\t\t\t\t\t\t\t\t\tFel";
        cout<<"\thealth="<<cout<<Fel_health;
        cout<<"\n\nFel's turn. Press 1 to attack w. press 2 to attack b."<<endl;
        int b;
        cin>>b;
        if (b == 1)
        {
        
            w_health = w_health-10;
        }
        
        if ( b == 2)
        {
            b_health = b_health-10;
        }
    }
    cout<<"Game Over.";
    
   fflush (stdin);
   cin.get();
   return 0;
   
}  
This is in multiple lines. You only need cout at the beginning of the statement.

cout<<"\thealth ="<<cout<<w_health;

cout<<"\thealth ="<<cout<<w_health;



Edit:
while ( b_health != 0||Fel_health != 0||Vex_health != 0||w_health != 0 )

The while loop keeps going as long as all of the healths are not equal to 0. Even if one gets down to zero, this while loop keeps going because the other conditions are true. If the game is supposed to end when the first health gets to zero, I might have a single game over flag and set that to true as soon as one of the healths gets set to 0 during game play. Then run the while loop on that single flag.

Last edited on
@wildblue
Thanks for the tip but
I'll changed it to
 
while (b_health != 0||b_health >= 1) //etc.... 

and its still in the same situation

Last edited on
I'm not sure if you saw my edited post. Re-reading the description of the game, it's supposed to end when just one player hits 0, not all of them hitting zero ? I'd keep the while loop simple and add a single game over flag to check.


Or, if you really want to keep the different conditions in the while, it can be tweaked like this.
while (!(b_health == 0 || Fel_health == 0 || Vex_health == 0 || w_health == 0))
Last edited on
Thanks. I'm a real beginner and I didn't actually see the edit.
I owe you big time!
This is a case of pure logic rules...
If you instead of:

while ( b_health != 0 || Fel_health != 0 || Vex_health != 0 || w_health != 0 )

write:

while ( b_health != 0 && Fel_health != 0 && Vex_health != 0 && w_health != 0 )

It will work the way u want.

This is equivalent to the line wildblue wrote, btw.
Last edited on
DeMorgan's laws can be helpful with this.

http://en.wikipedia.org/wiki/De_Morgan's_laws
Topic archived. No new replies allowed.