【问题标题】:Comparing multiple strings in while loop在while循环中比较多个字符串
【发布时间】:2013-11-14 02:50:07
【问题描述】:

我是一个初学者,只是想制作一个简单的计算器,提示用户输入两个值和一个操作数。

string operand;
cin >> operand;
while (operand != "+") || (operand != "-") || (operand !=  "*")|| (operand != "/"))
{
    cout << "operand must be either'+', '-', '*', or '/'." << endl;
    cin >> operand;
}

为什么无论我输入什么操作数它都会一直进入while循环?

【问题讨论】:

    标签: c++ string string-comparison


    【解决方案1】:

    您想使用&amp;&amp; 而不是||

    while ((operand != "+") && (operand != "-") && (operand !=  "*") && (operand != "/"))
    

    【讨论】:

      【解决方案2】:

      使用std::string::find_first_of

      while (operand.find_first_of("+-*/") == std::string::npos)
      {
         //...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-19
        • 1970-01-01
        • 2015-12-15
        • 2014-05-24
        • 1970-01-01
        • 2017-08-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多