如果语句遍历是否满足条件 [英] If statement runs through whether conditions are met or not

查看:56
本文介绍了如果语句遍历是否满足条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使条件没有满足,我的if语句也会一直运行.我尝试过移动一些代码,甚至以不同的方式重写if语句,但是它并没有改变结果.有人知道我在做什么错吗?

My if statement runs through as if the conditions have been met even when they haven't. I have tried moving bits of code about and even rewriting the if statement differently but it has not changed the outcome. Does anyone know what I'm doing wrong?

#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;


    }



}

推荐答案

这真的是伪造的

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

您需要改为使用逻辑OR

You need to break it out with logical OR instead

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


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


As it stand, this if (bob == "no", "No", "NO", "nO") would be equivalent to if("nO") as the effect of the comma operator.

这篇关于如果语句遍历是否满足条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆