2

It just keeps looping. The numbers continue decreasing until the program is closed. Am I misusing something?

The playerHealth and orcHealth ints are 100.

randomNumber = ("%10d", 1 + (rand() % 100));

This was the way I seen the random number used on an srand() explanation page. If this is wrong, how should it be?

Are there any other problems here?

    switch(charDecision)
{
case 1:
    cout << "FIGHT" << endl;
    do{
        randomNumber = ("%10d", 1 + (rand() % 100));
        if(randomNumber >= 50){
            orcHealth = orcHealth - (randomNumber - (randomNumber / 5));
        cout << "You hit the orc! He now has " << orcHealth << " life left!" << endl;
        }
        else
        {
            playerHealth = playerHealth - (randomNumber - (randomNumber / 5));
            cout << "The orc hit you! You now have " << playerHealth << " life left!" << endl;
        }
    }while(playerHealth || orcHealth >= 0);
    break;

default:
    break;
}
ggurbet
  • 82
  • 12

3 Answers3

17

playerHealth || orcHealth >= 0 does not mean "while playerhealth greater than zero OR orchealth greater than zero". It means "while playerhealth, cast to boolean is true OR orchealth greater than zero".

MSalters
  • 173,980
  • 10
  • 155
  • 350
8

This

}while(playerHealth || orcHealth >= 0);

should probably be

}while(playerHealth > 0 && orcHealth > 0);

I think you want to exit the loop, if either one is 0 or less.

Also, change randomNumber = ("%10d", 1 + (rand() % 100)); to randomNumber = 1 + rand() % 100; The comma operator just obfuscates the code.

Henrik
  • 23,186
  • 6
  • 42
  • 92
2

Your condition for the do...while statement will stop at some point, but only at some point. This means, your condition will be met if either playerHealth is zero or orcHealth is smaller than zero. What if the playerHealth gets below zero? This is very likely since you are always deducting a number from both character's healths. A possibility of playerHealth becoming exactly zero is a very tiny one. And when the playerHealth goes below zero, its possibility of becoming zero is still very unlikely, even due to integer overflow. So, if you want to "kill" the character when one of their health becomes zero or less, you better change that line with something like

while ( playerHealth > 0 && orcHealth > 0 )

On a side note, the || statement works if any one of the statements is true. In C++, for an integer a 0 value is false, all other values -including negative ones- are treated as true. Also, the || checks from left to right and when it finds the first true statement, it stops searching. In your case, it checks playerHealth, which is very likely nonzero. When it sees this expression is true, it decides that the whole statement inside the parantheses is true and skips checking orcHealth >= 0. This results in the infinite loop. You might want to look at the order of evaluation of conditionals in C++, maybe something like this post.

Community
  • 1
  • 1
ggurbet
  • 82
  • 12