0

I am getting the error uninitialized local variable calculateTax used, I have attempted to define it with int and it didn't work, I am worried if I add something like int calculateTax = 0 it will override the function I have created for the variable.

I am a total noob when it comes to programming so I hope there is something obvious I am missing, any help would be most appreciated

Here is the code:

    //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;
}
Ben
  • 1
  • 1
  • 2
    `main()` defines a `double calculateTax` (i.e. uninitialised) and doesn't assign to it before `taxOwed = calculateTax;`. That's *undefined behaviour*. There is no relationship between `main()`'s variable and the same-named variable inside `getCalculateTax`. Remove `calculateTax`from the long list of variables atop `main()`, and once you know the `totalPay` you can say `double calculateTax = getCalculateTax(totalPay);` so it's immediately initialised. – Tony Delroy Apr 15 '16 at 05:08

2 Answers2

1

You have a clear case of undefinded Behavior. You're using an unitialized variable calculateTax in your main(). If a variable is not initialized it stores the raw Bits which were at the space of memory where your variable is stored. Those Bits will then be interpreted as the type you declared your variable to be, e.g. double in your case. You could be lucky and this memory chunk only contains 0s so your variable will hold the value 0, however if there is a single Bit set to 1, your variable will hold a totally different value. Got that?

Therefore the best thing you can do is to allways initialize all your variables directly after declaring them. So instead of

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

Do yourself a favor and write it like that

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

So let's face the actual problem in your code. Not initializing a variable isn't actually that bad, however using an uninitialized variable is. You wrote a beautiful little function to calculate the taxes, so why aren't you using it? getCalculateTax is never called.

So instead of assigning an uninitialized variable you probably wanted to do something like this:

taxOwed = getCalculateTax(totalPay);

This should do the job ;)

Community
  • 1
  • 1
muXXmit2X
  • 2,745
  • 3
  • 17
  • 34
1

calculateTax should take 2 arguments:

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

    calculateTax = (taxRate / 100) * totalPay;

    return calculateTax;
}

so, the call is:

taxOwed = calculateTax(totalPay, taxRate);

remove the if from the call

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

and that should be it.

NOTES: try to avoid declaring multiple vars per line. Declare them when and where you need them:

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

and, you can clean up your functions a little:

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
}
Exceptyon
  • 1,584
  • 16
  • 23