0

Hey basically i want both the player and the wolves to attack each other until one another are dead. But the while loop is infinite so obviously the condition is not met. But i cant see where i am going wrong with this if ( choice1 == 1) // if statement is used throughout the game to allow the user to interact through the game with choices.

while((Status.health != 0) && (Wolves.health != 0) )
        {
        int playerAttack = Status.strength + hitPoints() +  Rock.attack;
        cout<< "This is the player attack" << playerAttack;
        Wolves.health = Wolves.health - playerAttack;
        cout << "This is the wolves health" << Wolves.health;
        if (Wolves.health <= 0)
        {
            cout << "\nThe wolves are dead\n ";
        }
        int wolfAttack = Wolves.attack + hitPoints();
        Status.health - wolfAttack;
        if(Status.health <= 0)
        {
            gameOver();
        }// print out of object health.
        }

Can anybody help ?

Pendo826
  • 1,002
  • 18
  • 47

6 Answers6

3

Compare:

Wolves.health = Wolves.health - playerAttack;

vs

Status.health - wolfAttack;

Notice any difference?

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
2

Are you sure that this is correct:

Status.health - wolfAttack;

This actually is a no-operation. Perhaps you meant:

Status.health -= wolfAttack;
John Dibling
  • 99,718
  • 31
  • 186
  • 324
2

Well, i think the health was not exact 0 - because your condition looks only for != 0 it should be bigger than 0

while((Status.health > 0) && (Wolves.health > 0)) ...

edit: also the missing = John Dibling found first

Thomas
  • 689
  • 3
  • 8
  • 14
2

in computer some of numbers cannot be represented for example 0.1, so if you calculate 1 - (0.1 * 10) its result not equal to zero. You checked only !=0 and ==0 condition. Try this:

=0 or <=0 also if your "health" variable is integer you give a error tolerance such as: =(0.5) or <=(0.5) etc...

Polymorphism
  • 249
  • 1
  • 9
1

Most likely both values are never zero. That sounds likely, because you yourself use the <= condition.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
1

I think your are getting negative values, I recomend to use

while((Status.health > 0) && (Wolves.health > 0) )