C ++:处理错误,无法理解我的错误

时间:2018-09-18 10:41:41

标签: c++

1。检查用户输入。如果输入不匹配三个浮点数,则输出错误消息,并且不开始计算。 2.检查a == 0。如果是这样,则抛出runtime_error并将其捕获到main中,并显示一条消息,指出a不能为0。

错误消息应如下所示:

发生错误:用户输入格式错误

发生错误:a不能为零

#include <iostream>
#include <cmath>
#include <vector>
#include <stdexcept>

using namespace std;

vector<double> solutionFinal (double a, double b, double c){


    double s1, s2, discriminant;

    discriminant = b*b - 4*a*c;

    if (discriminant > 0){
        s1 = (-b + sqrt(discriminant)) / (2*a);
        s2 = (-b - sqrt(discriminant)) / (2*a);
        cout << "There are 2 solutions." << endl;
        cout << "The solutions are: " << s1 << " and  " << s2;
        return {s1, s2};
    }

    else if (discriminant == 0) {
        cout << "There is 1 solution." << endl;
        s1 = (-b + sqrt(discriminant)) / (2*a);
        cout << "The solution is: " << s1;
        return {s1};
    }

    else {
        cout << "There is no solution."  << endl;
        return {};
    }
}
int main (){


    double a, b, c;
    cout << "Please enter the values of a, b, and c respectively:" << endl;


    try{
        if (!(cin >> a >> b >> c)) {
        throw runtime_error("An error occured: Malformed user input");
    }

        if (a == 0) {
        throw runtime_error("An erorr occured: a must not be zero");
        }

    }

    auto result = solutionFinal(a, b, c);
    for (auto scalar : result){
    }
    catch (runtime_error& excpt) {
      cout << excpt.what();
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

您的代码格式不正确,大括号不累加。我更喜欢另一种花括号样式,因为我认为这样更容易:

#include <iostream>
#include <cmath>
#include <vector>
#include <stdexcept>

using namespace std;

vector<double> solutionFinal (double a, double b, double c)
{    
    double s1, s2, discriminant;

    discriminant = b*b - 4*a*c;

    if (discriminant > 0)
    {
        s1 = (-b + sqrt(discriminant)) / (2*a);
        s2 = (-b - sqrt(discriminant)) / (2*a);
        cout << "There are 2 solutions." << endl;
        cout << "The solutions are: " << s1 << " and  " << s2;
        return {s1, s2};
    }
    else if (discriminant == 0) 
    {
        cout << "There is 1 solution." << endl;
        s1 = (-b + sqrt(discriminant)) / (2*a);
        cout << "The solution is: " << s1;
        return {s1};
    }
    else 
    {
        cout << "There is no solution."  << endl;
        return {};
    }
}

int main ()
{
    double a, b, c;

    cout << "Please enter the values of a, b, and c respectively:" << endl;

    try
    {
        if (!(cin >> a >> b >> c)) 
        {
            throw runtime_error("An error occured: Malformed user input");
        }

        if (a == 0) 
        {
            throw runtime_error("An erorr occured: a must not be zero");
        }

        // this block was outside the try, between the try and the catch.
        // it MUST be inside the try block like this
        auto result = solutionFinal(a, b, c);
        for (auto scalar : result)
        {
        }
    }
    catch (runtime_error& excpt) 
    {
        cout << excpt.what();
    }

    return 0;
}