【问题标题】:If statement runs through whether conditions are met or notif语句贯穿是否满足条件
【发布时间】:2023-03-25 10:34:01
【问题描述】:

我的 if 语句一直运行,就好像条件已经满足,即使它们没有满足。我尝试过移动一些代码,甚至以不同的方式重写 if 语句,但它并没有改变结果。有谁知道我做错了什么?

#include <iostream>
#include <string>
using namespace std;

double num, num2, num3, num4, num5, num6, sum;
char input;
bool continueBool = true;
string bob;

void math()
{


    cout << "Please enter your first number" << endl;
    cin >> num;

    cout << "Please enter your second number?" << endl;
    cin >> num2;

    cout << "Please enter your third number?" << endl;
    cin >> num3;

    cout << "Please enter your fourth number" << endl;
    cin >> num4;

    cout << "Please enter your fith number?" << endl;
    cin >> num5;

    cout << "Please enter your sixth number?" << endl;
    cin >> num6;

    sum = num + num2 + num3 + num4 + num5 + num6;


}

void ifStatement() {

    if (bob == "no", "No", "NO", "nO") {

        continueBool = false;

        cout << "Good bye!" << endl;

    }
}


int main()
{
    while (continueBool = true) {


        math();

        cout << "The sum of your numbers is: " << sum << endl;

        cout << "Would you like to add any more numbers together?" << endl;

        cin >> bob;

        ifStatement();

        return 0;


    }



}

【问题讨论】:

  • 警告:while (continueBool = true) --> while (continueBool) ..... 比while (continueBool == false == false) 好。底线:检查布尔值 ==true 是否毫无意义,只需检查布尔值本身。
  • 总是return从循环内部生成循环:not-a-loop。虽然当您遇到无限循环错误时,也许这不是一件坏事。

标签: c++


【解决方案1】:

这真的是假的

if (bob == "no", "No", "NO", "nO")

你需要用逻辑或来打破它

if (bob == "no" || bob == "No" || bob == "NO" || bob == "nO")

就目前而言,这个if (bob == "no", "No", "NO", "nO") 将等同于if("nO") 作为逗号运算符的效果。

【讨论】:

  • 这已经修复了它的运行,但现在它没有继续 while 循环?
  • @ScottMcgonigle 但它解决了你问的问题?仅仅因为你的代码中除了你遇到的第一个错误之外还有更多错误,而不赞成一个答案,这似乎有点不受欢迎。
【解决方案2】:
bob == "no", "No", "NO", "nO"

没有做你认为它正在做的事情。你的意思是:

bob == "no" ||
bob == "No" ||
bob == "NO" ||
bob == "nO"

【讨论】:

    【解决方案3】:

    这是对您问题的一点补充说明,但在这种情况下,您可能需要考虑在比较之前将答案转换为小写(或大写)。

    这样,你就可以使用if (tolower(bob) == "no")


    这是一个如何使用tolower函数的示例

    http://www.cplusplus.com/reference/cctype/tolower/

    /* tolower example */
    #include <stdio.h>
    #include <ctype.h>
    int main ()
    {
      int i=0;
      char str[]="Test String.\n";
      char c;
      while (str[i])
      {
        c=str[i];
        putchar (tolower(c));
        i++;
      }
      return 0;
    }
    

    【讨论】:

      【解决方案4】:

      你的循环问题可以解释:

      while (continueBool = true)

      应该阅读

      while (continueBool == true).

      就目前的代码而言,您将其设置为 true 而不是检查值,因此它永远不会退出。

      【讨论】:

      • 更好:while (continueBool).
      • @PeteBecker 我认为这是一个偏好问题。我怀疑现代编译器会以两种形式输出相同的结果。
      • 嗯,是的,它们会产生相同的代码。但是if 语句需要一个布尔值,因此通过比较将continueBool 转换为布尔值是没有意义的,并且在本例中使用一个简单的错字是错误的。
      猜你喜欢
      • 2016-02-15
      • 1970-01-01
      • 2012-12-17
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多