简体   繁体   English

制作二次根项目,我的第二组嵌套的“IF”语句被忽略

[英]Making a Quadratic Roots project, my second set of nested "IF" statements is being ignored

To quickly explain, I need six different possible cout outputs.为了快速解释,我需要六种不同的可能 cout 输出。 Three for when A = 0 which don't work, and then three more for when it doesn't equal 0 which work flawlessly.当 A = 0 时三个不工作,然后三个当它不等于 0 时完美地工作。 I think I know my problem, the set of if statements under '//A DOES equal 0', can never run.我想我知道我的问题,'//A DOES equal 0' 下的一组 if 语句永远无法运行。 What's wrong with my second set of nested if statements?我的第二组嵌套 if 语句有什么问题?

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

int main() {
    double numA = 0;
    double numB = 0;
    double numC = 0;
    int rootOne = 0;
    int rootTwo = 0;

    // User inputs roots

    cout << "Program computes and prints the real roots of a quadratic polynomial a*x^2 + b*x + c." << endl;
    cout << "Enter three real numbers a, b, c, seperated by spaces:";
    cin >> numA >> numB >> numC;

    //Calculating the Roots; (-b + sqrt(b * b - 4 * a * c)) / (2 *a) and (-b - sqrt(b * b - 4 * a * c)) / (2 *a)
    rootOne = (-numB + sqrt(numB * numB - 4 * numA * numC)) / (2 * numA);
    rootTwo = (-numB - sqrt(numB * numB - 4 * numA * numC)) / (2 * numA);

    // A doesn't equal 0
    if (numA != 0) {
        if ((numB * numB) - (4 * numA * numC) > 0) {
            cout << "The quadratic polynomial " << numA << "*x^2 + " << numB << "*x + " << numC << " has two distinct real roots" << endl;
            cout << "root 1 = " << rootOne << " root2 = " << rootTwo << endl;

        } else if ((numB * numB) - (4 * numA * numC) == 0) {
            cout << "The quadratic polynomial " << numA << "*x^2 + " << numB << "*x + " << numC << " has two equal real roots" << endl;
            cout << "root 1 = root2 = " << rootTwo << endl;

        } else if ((numB * numB) - (4 * numA * numC) < 0) {
            cout << "The quadratic polynomial " << numA << "*x^2 + " << numB << "*x + " << numC << " has two complex roots" << endl;
        }
    }

    //A does equal 0
    if (numA == 0) {
        if ((numB == 0) && (numC != 0)) {
            cout << "No roots for the constand function of " << numC << endl;
        } else if ((numB == 0) && (numC == 0)) {
            cout << "No roots for the degenerate case of 0 = 0." << endl;
        } else {
            cout << "The only root for the linear case of " << numA << "*x^2 + " << numB << "*x + " << numC << "is: " << numB << endl;
        }
    }

    return 0;
}

Whoops:哎呀:

if (numA =! 0) {

Here you're setting numA , to !0 (which is true , then converted to 1 for the assignment).在这里,您numA ,设置!0 (这是true ,然后为分配转换为1 )。

It's the same as:它与以下内容相同:

if (numA = !0) {

So it's no surprise that your subsequent if statement is "being ignored": the condition no longer matches.因此,您随后的if语句“被忽略”也就不足为奇了:条件不再匹配。

If you'd turned your compiler warnings on, you'd have received a message like:如果您打开了编译器警告,您会收到如下消息:

warning: suggest parentheses around assignment used as truth value警告:建议在用作真值的赋值周围加上括号

…and then you wouldn't have to guess what's going on ask us to fix your code for you. ......然后您就不必 猜测发生了什么 要求我们为您修复您的代码。

The "not equal to" operator is != , not =! “不等于”运算符是!= ,而不是=! . .

Okay, so I guess thanks for trying to help...and then insulting me, I never did learn if I nested my statements right or not but whatever.好的,所以我想感谢你试图帮助......然后侮辱我,我从来没有知道我是否正确地嵌套了我的陈述,但无论如何。 I ended up just rewriting my first nested statements and used Logical Operators like I did at the bottom.我最终只是重写了我的第一个嵌套语句并使用了逻辑运算符,就像我在底部所做的那样。 So my new code just has all six in one big nest and it worked!因此,我的新代码将所有六个合二为一,并且奏效了!

The "not equal to" operator is !=, not =! “不等于”运算符是 !=,而不是 =!

I actually thought I was doing it right before I came here so I did learn something anyway so thanks to that person, as such, you win the best answer!我实际上以为我在来这里之前就在做,所以无论如何我确实学到了一些东西,所以感谢那个人,因此,您赢得了最佳答案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM