While statement not working?

前端 未结 2 1437
北恋
北恋 2021-01-29 12:43
#include 
#include 
using namespace std;
int main()
{
    int number;
    float cost;
    char beverage;

    bool validBeverage;

    cou         


        
相关标签:
2条回答
  • 2021-01-29 12:51

    Change

    while(beverage!='e'||beverage!='E'); // Condition becomes true always 
    

    to

    while(beverage!='e'&& beverage!='E');  
    

    And similarly replace all || by && in first while loop conditional expression

    while(beverage!='A' && beverage!='B' &&beverage!='C'&& beverage!='D' && beverage!='E' && beverage!='a' && beverage!='b' && beverage!='c' &&
         beverage!='d' && beverage!='e')
    
    0 讨论(0)
  • 2021-01-29 12:52

    Take this condition as an example, the same problem is in others as well.

    while (beverage!='e'||beverage!='E');
    

    A character is either not equal to 'e', or it is equal to 'e', in which case it's not equal to 'E', so the condition is always true.

    What you want is logical and.

    0 讨论(0)
提交回复
热议问题