创建二次公式求解器 - 未在范围中声明的变量

时间:2017-02-09 19:01:27

标签: c++

所以我一直致力于这项任务,即在C ++中创建一个二次公式求解器,并且在大多数情况下我相信我已经得到了所有东西,但我一直收到错误:

main.cpp:54:11:错误:在此范围内未声明“a”

但是我在equSolver()和outResults()

中的每个变量都收到此错误
#include <iostream>
#include <cmath>

using namespace std;

void findCoeff();
void equSolver(double a, double b, double c, double d);
double discr(double a, double b, double c);
void outResults(double root1, double root2, double a, double b, double c, double d);

void findCoeff(){
        double a;
        double b;
        double c;

        cout << "\nEnter coefficient a:\n";
        cin >> a;
        cout << "\nEnter coefficient b:\n";
        cin >> b;
        cout << "\nEnter coefficient c:\n";
        cin >> c;
}

double discr(double a, double b, double c){

    double d;
    d = pow(b, 2) - (4 * a * c);
    return d;
}

void equSolver(double a, double b, double c, double d){
    double root1 = ((-1 * b) + sqrt(d)) / (2 * a);
    double root2 = ((-1 * b) - sqrt(d)) / (2 * a);

}

void outResults(double root1, double root2, double a, double b, double c, double d){
    if( std::isnan(root1) || std::isnan(root2)){
        cout << "Quadratic equation with the following coefficients: \n";
        cout << "a: " << a << "; b: " << b << "; c: " << c << "\n";
        cout << "has no roots in the real domain\n";
    }
    else {
        cout << "Quadratic equation with the following coefficients: \n";
        cout << "a: " << a << "; b:" << b << "; c:" << "\n";
        cout << "has the following roots\n";
        cout << "Root1: " << root1 << "; Root2: " << root2 << "\n";

    }
}

int main(){
findCoeff();
equSolver(a, b, c, d);
outResults(root1, root2, a, b, c, d);
}

我试图在主要内部或作为参数对del变量进行delcare,但之后它们都保持为0.程序运行但实际上没有进行任何计算。

由于

2 个答案:

答案 0 :(得分:2)

您有理解如何在C ++中使用变量的问题。例如你的功能

void findCoeff();

创建三个本地double变量,从std::cin输入值,然后丢弃它们。出于某种原因,你认为它们会神奇地出现在调用该函数的main()中,但事实并非如此。与equSolver()相同 - 您计算2个值,将它们存储在2个变量中然后丢弃。之后你会期望它们神奇地出现在main()内并传递给下一个函数。这在C ++中不起作用。您必须在main()中定义这些变量,然后以某种方式创建函数以将值存储到它们中。例如,您可以使用引用:

void findCoeff( double &ra, double &rb, double &rc ){
    cout << "\nEnter coefficient a:\n";
    cin >> ra;
    cout << "\nEnter coefficient b:\n";
    cin >> rb;
    cout << "\nEnter coefficient c:\n";
    cin >> rc;
}

int main()
{
    double a = 0, b = 0, c = 0;
    findCoeff( a, b, c ); // now findCoeff() would modify a,b, and c through reference and you can use that values
    double d = discr(a, b, c);
    ...
}

更好的方法是让该函数返回该值(如discr()所做的那样),但由于返回多个值的复杂性,此时主题对您来说太高级了。

答案 1 :(得分:0)

查看您的main()功能:

int main(){
    findCoeff();
    equSolver(a, b, c, d);
    outResults(root1, root2, a, b, c, d);
}

您的计划首先输入main()并致电findCoeff()

void findCoeff(){
        double a;
        double b;
        double c;

        cout << "\nEnter coefficient a:\n";
        cin >> a;
        cout << "\nEnter coefficient b:\n";
        cin >> b;
        cout << "\nEnter coefficient c:\n";
        cin >> c;
}

此处,abc位于本地范围中,因为它们位于函数内。只有findCoeff()才能看到它们。在用户输入ab以及cfindCoeff()退出的值后,这些本地范围变量将被丢弃。

所以让我们回到main()

int main(){
    findCoeff();
    equSolver(a, b, c, d);
    outResults(root1, root2, a, b, c, d);
}

现在您要求abcd,其中没有一项已在main()内宣布。这就是你得到的原因:main.cpp:54:11: error: 'a' was not declared in this scope

如果您希望在a中看到bcd(和main()),则可以将其置于<强>全局范围即在函数之外。

double a;
double b;
double c;
double d;
void findCoeff(){
        cout << "\nEnter coefficient a:\n";
        cin >> a;
        cout << "\nEnter coefficient b:\n";
        cin >> b;
        cout << "\nEnter coefficient c:\n";
        cin >> c;
}
...

请记住,拥有全局变量是不好的做法。 Slava的答案给出了更好的解决方案,避免了全局变量。但如果目前这些概念超出了你的范围,那么这对你的任务就足够了。