could someone please explain to me what wrong with my do while loop. i made a pokemon game in visual basic for school. i am teaching my self C++. i just started this weekend and wanted to see if i could covert my code over. everything works perfectly without the loop. except with the loop when charizard or blastoises health go below zero it waits for both of them to go below zero it wont stop when one of them hits zero like i want it to.
i know that && is and, and || is or

so howcome }while(BlastoiseHealth > 0 || CharizardHealth > 0); is being treated as and???

//Danny P
//Sunday January 24, 2010
#include <iostream>

using namespace std;

int main()
{
   int BlastoiseHealth = 300;
   int CharizardHealth = 300;
   int FireBlast = 75;
   int FlameThrower = 55;
   int DragonBreath = 80;
   int Thunder = 150;
   int IceBeam = 75;
   int WaterGun = 85;
   int HydroPump = 150;
   int Tackle = 70;
   string CharizardAttack(" ");
   string BlastoiseAttack(" ");
   
   cout << "Created by: Danny P" << endl;
   cout << "Compliled on: Sunday January 24, 2010" << endl;
   cout << "" << endl;
   cout << "Welcome To Pokemon Stadium Written in C++" << endl;
   cout << "In this text based demo you control Charizard's and Blastoise's Attacks" << endl;
   cout << "At the first prompt type in an attack for Charizard" << endl;
   cout << "and vice-versa for Blastoise" << endl << endl;
   cout << "Please note that typing in an incorrect attack will result in the termination of";
   cout << "this application, the immediate shutdown of your pokedex, and the immediate" << endl;
   cout << "Suspension of your pokemon license!!!" << endl;
   cout << " " << endl;
   cout << "Charizard's health is: 300 ";
   cout << "                   Blastoise's health is: 300 " << endl;
   cout << " " << endl;
   cout << "your available attacks for charizard are: " << endl;
   cout << " " << endl;
   cout << "fireblast	75 damage towards blastoise " << endl;
   cout << "flamethrower    55 damage towards blastoise " << endl;
   cout << "dragonbreath    80 damage towards blastoise " << endl;
   cout << "thunder         150 damage towards blastoise " << endl;
   cout << " " << endl;
   cout << "your available attacks for blastoise are: " << endl;
   cout << " " << endl;
   cout << "icebeam 	75 damage towards charizard " << endl;
   cout << "watergun        55 damage towards charizard " << endl;
   cout << "hydropump       80 damage towards charizard " << endl;
   cout << "tackle          150 damage towards charizard " << endl;
   cout << " " << endl;
   
   do {
   cout << "Please enter an attack for Charizard: " ;
   cin >> CharizardAttack;
   
   
   if ( CharizardAttack == "fireblast")
   {
           BlastoiseHealth = (BlastoiseHealth - FireBlast);
           cout << endl << "Blastoise's health is now down to: " << BlastoiseHealth << endl;
   }
          else
          {
                 if ( CharizardAttack == "flamethrower")
          {
                 BlastoiseHealth = (BlastoiseHealth - FlameThrower);
                 cout << endl << "Blastoise's health is now down to: " << BlastoiseHealth << endl;
       
           }
                 else
                 {
                       if ( CharizardAttack == "dragonbreath")
                 {
                       BlastoiseHealth = (BlastoiseHealth - DragonBreath);
                       cout << endl << "Blastoise's health is now down to: " << BlastoiseHealth << endl;
           
                 }
                       else
                       {
                                if ( CharizardAttack == "thunder")
                       {
                                BlastoiseHealth = (BlastoiseHealth - Thunder);
                                cout << endl << "Blastoise's health is now down to: " << BlastoiseHealth << endl;
                       }
}
}
}
       
       //**************************************************************************************************************************
       cout << " " << endl;
       cout << "Please enter an attack for Blastoise: " ;
       cin >> BlastoiseAttack;
   
       if ( BlastoiseAttack == "watergun")
   {
           CharizardHealth = (CharizardHealth - WaterGun);
           cout << endl << "Charizard's health is now down to: " << CharizardHealth << endl;
   }
          else
          {
                 if ( BlastoiseAttack == "hydropump")
          {
                 CharizardHealth = (CharizardHealth - HydroPump);
                 cout << endl << "Charizard's health is now down to: " << CharizardHealth << endl;
       
           }
                 else
                 {
                       if ( BlastoiseAttack == "tackle")
                 {
                       CharizardHealth = (CharizardHealth - Tackle);
                       cout << endl << "Charizard's health is now down to: " << CharizardHealth << endl;
           
                 }
                       else
                       {
                                if ( BlastoiseAttack == "tackle")
                       {
                                CharizardHealth = (CharizardHealth - Tackle);
                                cout << endl << "Charizard's health is now down to: " << CharizardHealth << endl;
                       }
}
}
}

}while(BlastoiseHealth > 0 || CharizardHealth > 0);


   
   
   
   system("pause");   
   return 0;
}

Recommended Answers

All 3 Replies

While loop runs while the condition is true. Hence, if you want it to stop running when one or the other goes under zero you need an &&. It's counter-intuitive at first but just do a quick truth table

cond1             cond2            &&
    T               T                T    (loop continues)
    F               T                F     (loop stops)
    T               F                F     (loop stops)
    F               F                F     (loop stops)

As opposed to having the or where if only either one is false, the || is still true and the loop continues.

This isn't a code snippet. It's a regular thread. You may have accidentally checked something and called it a snippet.

It's being treated as an OR. Your description of what it is doing makes it SOUND like it IS treating it as an OR. Your description makes it sound like you WANT it to be an AND. The way you have it written (with the ||), the program should continue going through the loop if either character's health is positive. If that's NOT what you want, then you want the AND condition, so change the || to &&.

well thanks to both of you very much i have it working i was getting a little confused

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.