制作一个基本的4函数计算器,得到"错误c4700:未初始化的局部变量'回答'使用"

时间:2015-11-05 02:07:40

标签: c++ compiler-errors initialization

我试图制作一个基本的4函数计算器时遇到此错误。我对c ++相当新,所以可能有更多的错误,但我主要想关注错误,因为我无法编译它。谢谢你的帮助!

#include <iostream>
using namespace std;

float fadd(float num1, float num2, float answ);
float fsub(float num1, float num2, float answ);
float fmul(float num1, float num2, float answ);
float fdiv(float num1, float num2, float answ);
char contOption();

int main()
{
    float answ, num1, num2;
    char oper, cont;
    cout << "Student name:           Jose Gomez" << endl;
    cout << "Student number:         900724015" << endl << endl << endl;
    do 
    {
    cout << "Please enter first number, operator & second number: ";
    cin >> num1 >> oper >> num2;
    switch (oper)
    {
    case '+':
        fadd(num1, num2, answ); //the 'answ' here is what is giving me the 
                                //error and i do not know how to fix it
        cout << endl << "Answer = " << answ;
    case '-':
        fsub(num1, num2, answ);
        cout << endl << "Answer = " << answ;
    case '*':
        fmul(num1, num2, answ);
        cout << endl << "Answer = " << answ;
    case '/':
        fdiv(num1, num2, answ);
        cout << endl << "Answer = " << answ;
    default:
        cout << "Sorry, illegal operation.  Only '+', '-', '*', '/' are        allowed" << endl << endl;
    cout << "Please enter first number, operator & second number: ";
    cin >> num1 >> oper >> num2;
}
    cont = contOption();

} while (cont == 'y' || cont == 'Y');


cin.ignore();
cin.get();
return 0;
}

float fadd(float num1, float num2, float answ)
{
    answ = num1 + num2;
    return answ;
}

float fsub(float num1, float num2, float answ)
{
answ = num1 - num2;
return answ;
}

float fmul(float num1, float num2, float answ)
{
answ = num1*num2;
return answ;
}

float fdiv(float num1, float num2, float answ)
{
answ = num1 / num2;
if (num2 == 0)
    cout << "Sorry, divide by 0 is an illegal operation";
else if (num2 != 0)
    return answ;

}

char contOption()
{
char cont;
cout << endl << "Would you like to perform another calculation? (y / n): ";
cin >> cont;
return cont;

}

1 个答案:

答案 0 :(得分:-1)

首先,您需要初始化变量:

float answ, num1, num2;
answ = num1 = num2 = 0.0;

否则他们是未定义的。其次,你应该在你的switch语句中添加中断,否则你将每次检查每个条件并达到默认条件。

接下来,在您的函数中,您按值而不是通过引用传递参数。这会导致您的函数复制您传入的变量。因此,当您在运行计算后尝试打印答案时,您将尝试打印未初始化的变量。通过引用传递参数实际上将使用您传入的变量。

或者,您可以指定“回复”。如果您希望通过值传递,则从函数返回的值,但是您没有这样做。

这是一个通过引用传递的示例。

#include <iostream>
using namespace std;

void fadd(float& num1, float& num2, float& answ);
void fsub(float& num1, float& num2, float& answ);
void fmul(float& num1, float& num2, float& answ);
void fdiv(float& num1, float& num2, float& answ);
char contOption();

int main()
{
    float answ, num1, num2;
    answ = num1 = num2 = 0.0;
    char oper, cont;
    cout << "Student name:           Jose Gomez" << endl;
    cout << "Student number:         900724015" << endl << endl << endl;
    do
    {
        cout << "Please enter first number, operator & second number: ";
        cin >> num1 >> oper >> num2;
        switch (oper)
        {
        case '+':
            fadd(num1, num2, answ); //the 'answ' here is what is giving me the 
                                    //error and i do not know how to fix it
            cout << endl << "Answer = " << answ;
            break;
        case '-':
            fsub(num1, num2, answ);
            cout << endl << "Answer = " << answ;
            break;
        case '*':
            fmul(num1, num2, answ);
            cout << endl << "Answer = " << answ;
            break;
        case '/':
            fdiv(num1, num2, answ);
            cout << endl << "Answer = " << answ;
            break;
        default:
            cout << "Sorry, illegal operation.  Only '+', '-', '*', '/' are        allowed" << endl << endl;

        cout << "Please enter first number, operator & second number: ";
        cin >> num1 >> oper >> num2;
    }

    cont = contOption();

    } while (cont == 'y' || cont == 'Y');


    cin.ignore();
    cin.get();
    return 0;
}

void fadd(float& num1, float& num2, float& answ)
{
    answ = num1 + num2;
}

void fsub(float& num1, float& num2, float& answ)
{
    answ = num1 - num2;
}

void fmul(float& num1, float& num2, float& answ)
{
    answ = num1*num2;
}

void fdiv(float& num1, float& num2, float& answ)
{
    answ = num1 / num2;
    if (num2 == 0)
        cout << "Sorry, divide by 0 is an illegal operation";
}

char contOption()
{
    char cont;
    cout << endl << "Would you like to perform another calculation? (y / n): ";
    cin >> cont;
    return cont;
}