Help With Payroll Program Please

I am making a payroll program as a project for my class. We are being given a list of data in one file and are to output the payroll information in another file.

My problem is that my Healthcare function seems to refuse using the 0.07 percentage and use something around 0.0812.
I've spent hours trying to fix this bug and have can't find a reason why it won't work

My Healthcare function itself is here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//THIS FUNCTION CALCULATES MEDICAL BENEFITS COST
double HCCostFunc (int WorkStatus, double ModifiedGrossPay)
{
    float Percentage;

    if (WorkStatus == 1)
        Percentage = 0.07;

    else if (WorkStatus == 0)
        Percentage = 0;

    HCCost = ModifiedGrossPay * Percentage;

    return (HCCost, ModifiedGrossPay);
}



This is my entire program:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int hours, OvertimeHours, DoubletimeHours, dependents, WorkStatus, MoreData, EmployeeNumber, count;
double GrossPay, PayRate, BasePay, OvertimePay, DoubletimePay, IRADeduction, ModifiedGrossPay, Withholdings, NetPay, HCCost;

double calculatePay(int hours, double PayRate);                                         //COMPUTES PAY
double IRAFunc(double GrossPay);                                                        //COMPUTES IRA DEDUCTIONS
double WithholdingsFunc(int dependents, double ModifiedGrossPay, double IRADeduction);  //COMPUTES WITHHOLDINGS FROM DEPENDENTS AND WITHHOLDING PERCENTAGES
double HCCostFunc (int WorkStatus, double ModifiedGrossPay);                            //COMPUTES HEALTCHARE BENEFITS COST

int main()
{
    ifstream in_stream;
    ofstream out_stream;

    in_stream.open("employeeData.dat");
    if(in_stream.fail())
        {
        cout << "Employee data file opening failed.\n";
        exit(1);
        }

    out_stream.open("payrollData.dat");
    if(out_stream.fail())
        {
        cout << "Payroll Data file opening failed.\n";
        exit(1);
        }


    in_stream >> MoreData;
    count = 1;
    while (MoreData != 0)
    {
    in_stream >> PayRate >> hours >> dependents >> WorkStatus >> MoreData;

    calculatePay(hours, PayRate);                                           //EARNINGS FUNCTION CALL
    IRAFunc(GrossPay);                                                      //IRA FUNCTION CALL
    WithholdingsFunc(dependents, ModifiedGrossPay, IRADeduction);           //WITHOLDINGS FUNCTION CALL
    HCCostFunc(WorkStatus, ModifiedGrossPay);                      //MEDICAL BENEFITS FUNCTION CALL

    NetPay = ModifiedGrossPay - Withholdings - HCCost;                      //NET PAY AFTER MEDICAL BENEFITS COST
    EmployeeNumber = count;                                                 //EMPLOYEE NUMBER IN ORDER OF DATA RECEIVED
    count++;


    //DATA OUTPUTTED TO PAYROLL DATA FILE
    out_stream.setf(ios::fixed);
    out_stream.setf(ios::showpoint);
    out_stream.precision(2);
    out_stream << "EMPLOYEE RECORD NUMBER - " << EmployeeNumber << endl;
    out_stream << "Hours: " << hours << endl;
    out_stream << "Hourly Rate: " << PayRate << endl;
    out_stream << endl;
    out_stream << "Base Pay = $" << BasePay << endl;
    out_stream << "Overtime Hours: " << OvertimeHours << endl;
    out_stream << "Overtime Pay at Time and a Half = $" << OvertimePay << endl;
    out_stream << "Overtime Hours at Double Time: " << DoubletimeHours << endl;
    out_stream << "Overtime Pay at Double Time = $" << DoubletimePay << endl;
    out_stream << "Gross Pay = $" << GrossPay << endl;
    out_stream << endl;
    out_stream << "IRA Deductions = $" << IRADeduction << endl;
    out_stream << "Modified Gross Pay = $" << ModifiedGrossPay << endl;
    out_stream << "Taxes Withheld = $" << Withholdings << endl;
    out_stream << "Medical Benefits Deduction = $" << HCCost << endl;
    out_stream << endl;
    out_stream << "Net Pay = $" << NetPay << "\n\n\n\n\n";
    }


    in_stream.close();
    out_stream.close();

    return 0;
}

//THIS FUNCTION CALCULATES GROSSPAY
double calculatePay(int hours, double PayRate)
{
    BasePay = PayRate * 40;

    //IF DOUBLETIME AND OVERTIME IS WORKED
    if (hours > 50)
        {
        DoubletimeHours = hours - 50;
        DoubletimePay = 2 * PayRate * DoubletimeHours;
        OvertimeHours = 10;
        OvertimePay = 1.5 * PayRate * OvertimeHours;
        GrossPay = BasePay + OvertimePay + DoubletimePay;
        }

    //IF ONLY OVERTIME IS WORKED
    else if (hours > 40)
        {
        DoubletimeHours = 0;
        DoubletimePay = 0;
        OvertimeHours = hours - 40;
        OvertimePay = 1.5 * PayRate * OvertimeHours;
        GrossPay = BasePay + OvertimePay;
        }

    //NO OVERTIME
    else
        {
        BasePay = PayRate * hours;
        GrossPay = BasePay;
        DoubletimeHours = 0;
        DoubletimePay = 0;
        OvertimeHours = 0;
        OvertimePay = 0;
        }
    return (DoubletimeHours, DoubletimePay, OvertimeHours, OvertimePay, GrossPay);
}

//THIS FUNCTION CALCULATES IRA DEDUCTIONS
double IRAFunc(double GrossPay)
{
    if (GrossPay < 400)
        IRADeduction = 0;

    else if (GrossPay >= 400 && GrossPay < 500)
        IRADeduction = GrossPay * 0.05;

    else if (GrossPay >= 500)
        IRADeduction = GrossPay * 0.10;

    ModifiedGrossPay = GrossPay - IRADeduction;

    return (IRADeduction, ModifiedGrossPay);
}

//THIS FUNCTION CALCULATES WITHHOLDINGS
double WithholdingsFunc(int dependents, double ModifiedGrossPay, double IRADeduction)
{
    double Percentage;

    if (dependents == 0)
        Percentage = 0.28;

    else if (dependents == 1)
        Percentage = 0.20;

    else if (dependents == 2)
        Percentage = 0.18;

    else
        Percentage = 0.15;

    Withholdings = Percentage * ModifiedGrossPay;

    return (Withholdings, ModifiedGrossPay);
}

//THIS FUNCTION CALCULATES MEDICAL BENEFITS COST
double HCCostFunc (int WorkStatus, double ModifiedGrossPay)
{
    float Percentage;

    if (WorkStatus == 1)
        Percentage = 0.07;

    else if (WorkStatus == 0)
        Percentage = 0;

    HCCost = ModifiedGrossPay * Percentage;

    return (HCCost, ModifiedGrossPay);
}
Last edited on
First off, please format your code. It's so much better for everyone :)

Secondly in C++ you can't return 2 values like you're trying to with:

return (HCCost, ModifiedGrossPay);

You can only return one value. What happens here is that the result of the comma operator is being returned - I think.

That doesn't really matter in this case, because you ignore all return values. I think you're under the (mistaken) impression that the global (yuck) variables are being modified by the functions. They are not.

Please look up the implications of pass-by-value, which is what C++ does.
I apologize for not formatting, this is my first time using a forum.
The variables are global since that is a guideline from my instructor, even though he says it's bad form.

However, my program still will not output correct data even when returning only one value. The only incorrect value is the HCCost value.

Might you have any ideas as to what is going wrong?
Considering the rest of my program works even with the return errors.

I appreciate the help by the way, since I am new to both C++ and forums I was expected some pretty condescending replies.
You CAN NOT return more than one value from a funtcion in C++.

For example, HCCostFunc function returns a double.

When you write "return (HCCost, ModifiedGrossPay)" You are applying the comma operator to HCCost and ModifiedGrossPay, the result of which is ModifiedGrossPay, which is then returned.

Please stop trying things like:
1
2
3
4
return (DoubletimeHours, DoubletimePay, OvertimeHours, OvertimePay, GrossPay);
return (IRADeduction, ModifiedGrossPay);
return (Withholdings, ModifiedGrossPay);
return (HCCost, ModifiedGrossPay);

It's just not doing what you think it is.

Now, there's an easy solution to what you're trying to do. To illustrate the problem:

You've defined "double ModifiedGrossPay" globally.
When you call " HCCostFunc (int WorkStatus, double ModifiedGrossPay)", the second argument you're passing is a copy (just the value) of "ModifiedGrossPay". Please, please look up the concept of "pass by value"

The point of the global variables I imagine is to avoid passing and returning variables, so just strip all that out and your program will make more sense. I can only assume your instructor wants to illustrate the evils of globals, in which case, this program will do nicely :-)

i.e. change your function prototypes to :
1
2
3
4
void calculatePay();                                         //COMPUTES PAY
void IRAFunc();                                                        //COMPUTES IRA DEDUCTIONS
void WithholdingsFunc();  //COMPUTES WITHHOLDINGS FROM DEPENDENTS AND WITHHOLDING PERCENTAGES
void HCCostFunc ();                            //COMPUTES HEALTCHARE BENEFITS COST 

Change the function definitions to match, and in the function bodies eliminate the return statements. Example, change this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//THIS FUNCTION CALCULATES MEDICAL BENEFITS COST
double HCCostFunc (int WorkStatus, double ModifiedGrossPay)
{
    float Percentage;

    if (WorkStatus == 1)
        Percentage = 0.07;

    else if (WorkStatus == 0)
        Percentage = 0;

    HCCost = ModifiedGrossPay * Percentage;

    return (HCCost, ModifiedGrossPay);
}

to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
//THIS FUNCTION CALCULATES MEDICAL BENEFITS COST
void HCCostFunc ()
{
    float Percentage;

    if (WorkStatus == 1)
        Percentage = 0.07;

    else if (WorkStatus == 0)
        Percentage = 0;

    HCCost = ModifiedGrossPay * Percentage;
}

Do that for all the functions, and finally, in main, use the functions according to their new form:
1
2
3
4
    calculatePay();                                           //EARNINGS FUNCTION CALL
    IRAFunc();                                                      //IRA FUNCTION CALL
    WithholdingsFunc();           //WITHOLDINGS FUNCTION CALL
    HCCostFunc();                      //MEDICAL BENEFITS FUNCTION CALL 

Thank you so much for the help!!
Topic archived. No new replies allowed.