制作一个基本的 4 功能计算器,获得"error c4700: uninitialized local variable 'answ' used"

Making a basic 4 function calculator, getting "error c4700: uninitialized local variable 'answ' used"

本文关键字:c4700 uninitialized error variable used answ 获得 local 一个 功能 计算器      更新时间:2023-10-16

我在制作一个基本的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;
}

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

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

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

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

或者,如果您喜欢通过值传递,您可以将从函数返回的值分配给"answersw",但您没有这样做。

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

#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;
}