c++ - 制作基本的四功能计算器,使用 “error c4700: uninitialized local variable ' answ”

标签 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;

}

最佳答案

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

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

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

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

另外,如果您希望按值传递,则可以为“answ”分配从函数返回的值,但您不这样做。

这是一个通过引用传递的示例。
#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;
}

关于c++ - 制作基本的四功能计算器,使用 “error c4700: uninitialized local variable ' answ”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33535465/

相关文章:

Android studio 编译错误 "Content is not allowed in prolog"

c++ - C++中的继承。为什么错了?

c++ - 将宏转换为函数

c++ - 当一个字符串是 ""或一个 vector 没有元素时, `begin()` 等于 `end()` 吗?

compiler-errors - 使用 Eclipse Android 编译 FFMPEG

java - 从不同的类调用方法到另一个类和方法

属性函数的javascript初始值

c++ - C++、结构和类构造函数中的 C 风格初始化列表

c++ - boost-asio 错误消息使用的编码是什么?

c++ - 从其行和列的给定总和生成一个二维整数数组