未初始化的局部变量calculateTax使用

时间:2016-04-15 05:05:34

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

2 个答案:

答案 0 :(得分:1)

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

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

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

帮自己一个忙,就这样写吧

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

因此,让我们面对代码中的实际问题。不初始化变量实际上并不是那么糟糕,但使用未初始化的变量是。你写了一个漂亮的小功能来计算税收,那你为什么不用它呢?永远不会调用getCalculateTax

因此,您可能希望执行以下操作,而不是分配未初始化的变量:

taxOwed = getCalculateTax(totalPay);

这应该可以胜任;)

答案 1 :(得分:1)

calculateTax应该有2个参数:

double getCalculateTax(double totalPay, double taxRate)
{
    double calculateTax;

    calculateTax = (taxRate / 100) * totalPay;

    return calculateTax;
}

所以,电话是:

taxOwed = calculateTax(totalPay, taxRate);

从通话中删除if

//        if (hours > BASE_HOURS) // getOvertimePay() already checks if hours>base
            overtime = getOvertimePay(hours);

应该是它。

注意:尽量避免每行声明多个变量。在您需要的时间和地点声明它们:

double overtime = getOvertimePay(hours);
double totalPay = basePay + overtime;
double taxOwed = calculateTax(...);

并且,您可以稍微清理一下您的功能:

double getBasePay(double hoursWorked) {
    return (hoursWorked > BASE_HOURS ? BASE_HOURS : hoursWorked) * PAY_RATE;
    // return min(BASE_HOURS, hoursWorked)*PAY_RATE
} 

double getOvertimePay(double hoursWorked) {
    return (hoursWorked > BASE_HOURS ? hoursWorked - BASE_HOURS : 0) *
            PAY_RATE * OT_MULTIPLIER;
}

double getCalculateTax(double totalPay, double taxRate) {
   return (taxRate / 100) * totalPay; // are you sure about that "/100"? usually doubles [0..1] are used for percentages
}