skips through script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lumpybanana247
    New Member
    • Apr 2007
    • 134

    skips through script

    when i use this (lets say...
    DPOHealth = 1 and
    GPOHealth = 1
    then, i get to this list and it displays both of them. The thing is... if i press 1 at the prompt, it will use DPO AND GPO.
    and if i press 3 (supposed to be GPO) it wont do anything.

    thanks for the help

    Code:
    bool AlchemyList()
    {
         if (DPOHealth>0)
         {
         cout<<"1. Deluted Potion Of Health(";
         cout<<DPOHealth;
         cout<<")\n";
         }
         if (WPOHealth>0)
         {
         cout<<"3. Weak Potion Of Health(";
         cout<<WPOHealth;
         cout<<")\n";                
         }
         if (GPOHealth>0)
         {
         cout<<"4. Good Potion Of Health(";
         cout<<GPOHealth;
         cout<<")\n";
         }
         if (SPOHealth>0)
         {
         cout<<"5. Strong Potion Of Health(";
         cout<<SPOHealth;
         cout<<")\n";
         }
         if (PPOHealth>0)
         {
         cout<<"6. Pure Potion Of Health(";
         cout<<PPOHealth;
         cout<<")\n";                
         }
         
         
         int AlchemyListChoice;
         cin>>AlchemyListChoice;
         switch (AlchemyListChoice)
         {
           case 1:
           {   
             if (DPOHealth>0)
             {
             cout<<"You use your DPO HEALTH\n";
             cout<<"5 health has been restored\n";  
             DPOHealth=DPOHealth-1;
             Restore5PersonsHealth();
             cout<<"You health now is ";
             ShowWhatIsHealth();
             cout<<"\n";
             }
           }
           case 2:
           {
             if (WPOHealth>0)
             {         
             cout<<"You use your WPO HEALTH\n";
             cout<<"10 health has been restored\n";  
             WPOHealth=WPOHealth-1;
             Restore10PersonsHealth();
             cout<<"You health now is ";
             ShowWhatIsHealth();
             cout<<"\n";
             } 
           }
           case 3:
           {        
             if (GPOHealth>0)
             {
             cout<<"You use your GPO HEALTH\n";
             cout<<"25 health has been restored\n";  
             GPOHealth=GPOHealth-1;
             Restore25PersonsHealth();
             cout<<"You health now is ";
             ShowWhatIsHealth();
             cout<<"\n";
             }
           }
           case 4:
           {
             if (SPOHealth>0)
             {
             cout<<"You use your SPO HEALTH\n";
             cout<<"50 health has been restored\n";  
             SPOHealth=SPOHealth-1;
             Restore50PersonsHealth();
             cout<<"You health now is ";
             ShowWhatIsHealth();
             cout<<"\n";
             }
           }
           case 5:
           {
             if (PPOHealth>0)
             { 
             cout<<"You use your DPO HEALTH\n";
             cout<<"Full health has been restored\n";  
             PPOHealth=PPOHealth-1;
             RestoreFullPersonsHealth2();
             cout<<"You health now is ";
             ShowWhatIsHealth();
             cout<<"\n";
             }
           }     
         }
    }
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by lumpybanana247
    when i use this (lets say...
    DPOHealth = 1 and
    GPOHealth = 1
    then, i get to this list and it displays both of them. The thing is... if i press 1 at the prompt, it will use DPO AND GPO.
    and if i press 3 (supposed to be GPO) it wont do anything.

    thanks for the help

    Code:
    bool AlchemyList()
    {
         if (DPOHealth>0)
         {
         cout<<"1. Deluted Potion Of Health(";
         cout<<DPOHealth;
         cout<<")\n";
         }
         if (WPOHealth>0)
         {
         cout<<"3. Weak Potion Of Health(";
         cout<<WPOHealth;
         cout<<")\n";                
         }
         if (GPOHealth>0)
         {
         cout<<"4. Good Potion Of Health(";
         cout<<GPOHealth;
         cout<<")\n";
         }
         if (SPOHealth>0)
         {
         cout<<"5. Strong Potion Of Health(";
         cout<<SPOHealth;
         cout<<")\n";
         }
         if (PPOHealth>0)
         {
         cout<<"6. Pure Potion Of Health(";
         cout<<PPOHealth;
         cout<<")\n";                
         }
         
         
         int AlchemyListChoice;
         cin>>AlchemyListChoice;
         switch (AlchemyListChoice)
         {
           case 1:
           {   
             if (DPOHealth>0)
             {
             cout<<"You use your DPO HEALTH\n";
             cout<<"5 health has been restored\n";  
             DPOHealth=DPOHealth-1;
             Restore5PersonsHealth();
             cout<<"You health now is ";
             ShowWhatIsHealth();
             cout<<"\n";
             }
           }
           case 2:
           {
             if (WPOHealth>0)
             {         
             cout<<"You use your WPO HEALTH\n";
             cout<<"10 health has been restored\n";  
             WPOHealth=WPOHealth-1;
             Restore10PersonsHealth();
             cout<<"You health now is ";
             ShowWhatIsHealth();
             cout<<"\n";
             } 
           }
           case 3:
           {        
             if (GPOHealth>0)
             {
             cout<<"You use your GPO HEALTH\n";
             cout<<"25 health has been restored\n";  
             GPOHealth=GPOHealth-1;
             Restore25PersonsHealth();
             cout<<"You health now is ";
             ShowWhatIsHealth();
             cout<<"\n";
             }
           }
           case 4:
           {
             if (SPOHealth>0)
             {
             cout<<"You use your SPO HEALTH\n";
             cout<<"50 health has been restored\n";  
             SPOHealth=SPOHealth-1;
             Restore50PersonsHealth();
             cout<<"You health now is ";
             ShowWhatIsHealth();
             cout<<"\n";
             }
           }
           case 5:
           {
             if (PPOHealth>0)
             { 
             cout<<"You use your DPO HEALTH\n";
             cout<<"Full health has been restored\n";  
             PPOHealth=PPOHealth-1;
             RestoreFullPersonsHealth2();
             cout<<"You health now is ";
             ShowWhatIsHealth();
             cout<<"\n";
             }
           }     
         }
    }
    Your problem is that you do not have break statements at the end of your switch cases. If there are no brake statements the computer will execute all cases below the chosen case even though the variable does not equal to that case's variable. Add break statements like this:
    Code:
    case 1:
        // do stuff
        break;
    case 2:
        // do stuff
        break;
    Also, I don't think you need curly brackets around the case statements.

    Comment

    • lumpybanana247
      New Member
      • Apr 2007
      • 134

      #3
      correct!
      thanks

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by lumpybanana247
        correct!
        thanks
        You are welcome.

        Comment

        Working...