c++ - 使用了未初始化的局部变量 calculateTax

标签 c++

我收到使用未初始化局部变量 calculateTax 的错误,我试图用 int 定义它但没有用,我担心如果我添加类似 int calculateTax = 0 的东西,它会覆盖我为创建的函数变量。

在编程方面我完全是菜鸟,所以我希望有一些明显的东西是我遗漏的,任何帮助将不胜感激

代码如下:

    //Gross pay calculation
    //additional tax rate calculation
    #include <iostream>
    #include <iomanip>
    using namespace std;

    //Setting global contants
    const double PAY_RATE = 22.55;
    const double BASE_HOURS = 40.0;
    const double OT_MULTIPLIER = 1.5;

   //Prototype functions
    double getBasePay (double);
    double getOvertimePay(double);
    double getCalculateTax(double);

    int taxRate;


    int main()
    {
        double hours,
        basePay,
        overtime = 0.0,
        taxRate,
        taxOwed,
        calculateTax,
        totalPay;


        //Input hours worked
        cout << "How many hours did you work? ";
        cin >> hours;

        //Input Tax rate
        cout << "What is the percent of your tax rate? ";
        cin >> taxRate;

        //get base pay
        basePay = getBasePay(hours);

        //Get OT if applicable

        if (hours > BASE_HOURS)
            overtime = getOvertimePay(hours);

        //calculate total pay
        totalPay = basePay + overtime;

        //calculate Tax rate
    taxOwed = calculateTax;

    // Setting output format
    cout << setprecision(2) << fixed << showpoint;

    //Display calculated pay
    cout << "Base pay: $" << basePay << endl
        << "Overtime pay: $" << overtime << endl
        << "Total pay: $" << totalPay << endl
        << "Taxes owed: $" << taxOwed << endl;

    //Adding Pause before creating functions
    char c;
    cout << "Press any key to end program: ";
    cin >> c;
    return 0;
}

//#############################################
// Get base pay function accepts hours worked #
// and returns pay for non OT hours           #
//#############################################

double getBasePay(double hoursWorked)
{
    double basePay;

    // determine base pay
    if (hoursWorked > BASE_HOURS)
        basePay = BASE_HOURS * PAY_RATE;
    else
        basePay = hoursWorked * PAY_RATE;

    return basePay;
} 

//##############################################
// The get overtime function accepts hours     #
//then returns the OT pay if applicable        #
//##############################################

double getOvertimePay(double hoursWorked)

{
    double overtimePay;

    //Determine OT pay
    if (hoursWorked > BASE_HOURS)
    {
        overtimePay = (hoursWorked - BASE_HOURS) *
            PAY_RATE * OT_MULTIPLIER;
    }
    else
        overtimePay = 0.0;

    return overtimePay;
}
//##########################################
//this taxes function calculates tax owed  #
// based on the total pay regardless of OT #
//##########################################

double getCalculateTax(double totalPay)
{
    double calculateTax;

    calculateTax = (taxRate / 100) * totalPay;

    return calculateTax;
}

最佳答案

你有一个明确的例子 undefinded Behavior .您在 main() 中使用了一个单元化变量 calculateTax。如果变量未初始化,它会存储原始位,这些位位于存储变量的内存空间中。这些位将被解释为您声明变量的类型,例如double 在你的情况下。你可能很幸运,这个内存块只包含 0,所以你的变量将保持值 0,但是如果有一个位设置为 1,你的变量将保持一个完全不同的值。明白了吗?

因此,您能做的最好的事情就是始终在声明变量之后直接初始化所有变量。 所以不是

double hours,
    basePay,
    overtime = 0.0,
    ... ;

帮自己一个忙,这样写吧

double hours = 0;
double basePay = 0;
double overtime = 0.0;
    ...

那么让我们面对代码中的实际问题。不初始化变量实际上并没有那么糟糕,但是使用未初始化的变量才是。你写了一个漂亮的小函数来计算税收,那你为什么不使用它呢? getCalculateTax 从未被调用。

因此,您可能不想分配未初始化的变量,而是想做这样的事情:

taxOwed = getCalculateTax(totalPay);

这应该可以完成工作 ;)

关于c++ - 使用了未初始化的局部变量 calculateTax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36638635/

相关文章:

c++ - 柏林噪声 2D : turning static into clouds

c++ - 删除字符串数组中的所有连续重复项

c++ - 运行快速修复引擎

c++ - 检查已经运行的进程和通信拷贝

c++ - 包含 <string> 时 xutility 出错

c++ - 为什么迭代一个 vector 这么慢?

c++ - RapidJSON::Value&的GoogleTest/Mock SaveArg

c++ - 在 C++ 中执行另一个程序

c# - 平台独立性: How is it different from moving source code from one OS to another?

c++ - 如何在Mfc c++中创建透明矩形?