Reputation: 383
do{
printf("Monster name:%s\n",monsternames[globalvar.monstercatego][globalvar.monsternivel]);
printf("Monster Life:%d\n",globalvar.monsterhp);
printf("------------------------------------------\n");
printf("----------------BattleGround--------------\n");
printf("------------------------------------------\n");
printf("Player name:%s\n", nomeheroi);
printf("Player life:%d\n", globalvar.playerhp);
printf("----------------------------------------\n");
printf("------------------Menu------------------\n");
printf("----------------------------------------\n");
printf("A - Attack\n");
printf("D - Defend\n");
scanf(" %c",&opcaobattle);
switch(opcaobattle)
{
case 'a':
danoPMonster();
break;
case 'd':
break;
}
}while((globalvar.monsterhp >= 0) || (globalvar.playerhp >= 0));
My console shows:
Monster name:Rat
Monster Life:2
----------------------------------------
----------------BattleGround------------
----------------------------------------
Player name:Test
Player life:10
----------------------------------------
------------------Menu------------------
----------------------------------------
A - Attack
D - Defend
a
Monster name:Rat
Monster Life:-1
----------------------------------------
----------------BattleGround------------
----------------------------------------
Player name:Test
Player life:10
----------------------------------------
------------------Menu------------------
----------------------------------------
A - Attack
D - Defend
It clearly show that globalvar.monsterhp < 0
and didn't leave the loop any ideas?
Upvotes: 0
Views: 57
Reputation: 6686
It's because you are using ||
logical OR operator means is any one of the condition will be true then loop will continue. and Player life is >0
, So it is not exiting loop
Upvotes: 5
Reputation: 754790
The 'or' ||
condition means that the loop will not terminate until both globalvar.monsterhp
is negative and globalvar.playerhp
is negative; if either or both values are positive, the loop will continue.
If you want the loop to terminate when either value is negative, you need to use 'and' &&
in the condition.
Upvotes: 0
Reputation: 359986
Presumably you only want to iterate while both the monster and the player are alive. Your code iterates while at least one is alive. Change the condition from "or" to "and."
while((globalvar.monsterhp >= 0) || (globalvar.playerhp >= 0));
// to
while((globalvar.monsterhp >= 0) && (globalvar.playerhp >= 0));
Upvotes: 1