Switch statement not working c++ and D:\c++ dev-c\Makefile.win recipe for target 'abc.o' failed

时间:2018-04-18 18:17:33

标签: c++ switch-statement recipe

So, I continued working on my abc formula in c++, and yet again, some errors pop up :D

this is what I have now:

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

class abcformule{
    public:
        int discriminant(double a, double b, double c) {
            return pow(b, 2) - 4 * a *c;
        }

        void answer(double a2, double b2, double c2) {
            int D = discriminant(a2, b2, c2);
            switch(D) {
                case < 0:
                    cout << "Discriminant is lower than 0, no solutions for x.";
                    cout << endl;
                    break;
                case > 0:
                    cout << "Discriminant is bigger than 0, two solutions for x: ";
                    cout << endl; 
                    cout << "x = " << (-b2 + sqrt(D)) / (2 * a2) <<
                            " or " << (-b2 - sqrt(D)) / (2 * a2) << 
                            "." << endl;
                    break;
                case = 0:
                    cout << "Discriminant is 0, only one solution for x: " << endl;
                    cout << (-b2) / (2 * a2) << endl;
            }
        }
};

int main(int argc, char** argv) {
    abcformule abc;
    abc.answer(5, -2, -7);
    return 0;
}

And these are the errors:

D:\c++ dev-c\abc.cpp    In member function 'void abcformule::answer(double, double, double)':
14  10  D:\c++ dev-c\abc.cpp    [Error] 'D' cannot appear in a constant-expression
18  10  D:\c++ dev-c\abc.cpp    [Error] expected primary-expression before '>' token
25  10  D:\c++ dev-c\abc.cpp    [Error] expected primary-expression before '=' token
25  12  D:\c++ dev-c\abc.cpp    [Error] an assignment cannot appear in a constant-expression

How do I fix this? Oh, and I saw this thing pop up in the error log too:

28      D:\c++ dev-c\Makefile.win   recipe for target 'abc.o' failed

1 个答案:

答案 0 :(得分:0)

我更喜欢你使用if else而不是switch,因为你不能在switch

中进行比较
#include <iostream>
#include <cmath>
using namespace std;

class abcformule{
public:
    int discriminant(double a, double b, double c) {
        return pow(b, 2) - 4 * a *c;
    }

    void answer(double a2, double b2, double c2) {
        int D = discriminant(a2, b2, c2);

            if(D<0){
                cout << "Discriminant is lower than 0, no solutions for x.";
                cout << endl;
            } else if(D>0){
                cout << "Discriminant is bigger than 0, two solutions for x:     ";
                cout << endl; 
                cout << "x = " << (-b2 + sqrt(D)) / (2 * a2) <<
                        " or " << (-b2 - sqrt(D)) / (2 * a2) << 
                        "." << endl;
            } else{
                cout << "Discriminant is 0, only one solution for x: " <<          endl;
                cout << (-b2) / (2 * a2) << endl;
            }   
    }
};

int main(int argc, char** argv) {
    abcformule abc;
    abc.answer(5, -2, -7);
    return 0;
}