New Help Moving out of Main using Functions

Hi everyone,

I'm only one month into learning c++ and need some assistance. Main point: I need to move as much as I can out of main and utilize functions. Here is what my program should calculate: pay, pre-tax IRA contribution and taxes based on the number of dependents. It then calculates the net pay based on these calculations and finally, it should prompt the user to see if they are full time and, if so, their net pay should be deducted by 7%/
The user will be prompted for hours worked, number of dependents, full time status and wage.

The below program is not working out... I will post my prior working code that I was using before I added full time status, medical costs and additional functions below this one. Thanks so much for any hints or help.

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
173
174
175
176
177
178
179
180
181
182

#include<iostream>
using namespace std;

// function for gross pay calculation
double salary_function (int hoursWorked, int overTimeHours, int doubleTimeHours, double hourlyRate, double basePay, double grossPay, double overTimePay, double doubleTimePay);

// function for IRA deposits
double IRA_function (double grossPay, double iraDeductionAmount);

//function for tax withholdings based on # of dependents
double withold_function (int dependents, double modifiedGrossPay);

double benefit_Status_function (double benefitStatus, double medicalBenefitCost);


int main()

{


int hourlyRate, hoursWorked, overTime, doubleTime, overTimeHours, doubleTimeHours, dependents;
double withold, netPay, medicalBenefitsCost, benefitStatus, iraDeductionAmount, basePay, overTimePay, doubleTimePay, grossPay, witholdCalc, modifiedGrossPay, modifiedNetPay;
char fullTime;
bool medicalBenefitAmount;


// INPUT


cout << "Please enter your hourly rate of pay: " << endl;                             //user inputs hourly rate
cin >> hourlyRate;

cout << "Please enter how the number of hours worked: " << endl;                      //user inputs number of hours worked
cin >> hoursWorked;

cout << "Please enter how many dependents you have: " << endl;                      //user inputs number of dependents
cin >> dependents;

cout << "Are you a full-time worker?  Enter Y for Yes or N for No., \n"             //user inputs full-time status, if applicable
     << "and then press return: " << endl;
cin >> fullTime;
{

// OUTPUT


    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);


    cout << "******************************************" << endl;                                       // input is given to the user
    cout << "You worked " << hoursWorked << " hours total this pay period." << endl;
    cout << overTimeHours << " hours are paid at time and a half." << endl;
    cout << doubleTimeHours << " hours are paid at double time." << endl;
    cout << "******************************************" << endl;
    cout << "Your total base pay is: $" << basePay << endl;                                             // this should calculate total base pay
    cout << "Your overtime at time and a half is: $" << overTimePay << endl;                            // this shows overtime, if applicable
    cout << "Your overtime at double time is: $" <<doubleTimePay << endl;                               // this shows double time
    cout << "Your total gross pay with all overtime is: $" << grossPay << endl;                         // this shows total gross, including overtime if applicable
    cout << "Your IRA deduction is: " << iraDeductionAmount << endl;                                    // adjusted netpay with IRA contributions
    cout << "Your modified gross pay is: $" << modifiedGrossPay << endl;
    cout << "Your tax withholding amount with " << dependents << " dependents is: $" << witholdCalc << endl;        // amount of withholdings
    cout << "Your medical benefits cost is " << medicalBenefitsCost << endl;                          // benefit cossts
    cout << "Your modified net pay is now: $" << modifiedNetPay << endl;                                                     // amount of netpay

return 0;


}   // end of main

double salary_function (int hoursWorked, int overTimeHours, int doubleTimeHours, double hourlyRate, double basePay, double grossPay, double overTimePay, double doubleTimePay);


{

double grossPay;

overTimeHours = 0;
doubleTimeHours = 0;
grossPay = 0;

if (hoursWorked > 40)                                                                 //calculating if hours are over 40
{
    basePay = hourlyRate * 40;
    overTimeHours = hoursWorked - 40;
}
    if (overTimeHours > 10)                                                               //calculating overtime and double time hours
{
    doubleTimeHours = overTimeHours - 10;
    overTimeHours = 10;
    doubleTimePay = hourlyRate * 2 * doubleTimeHours;                                 // calculating rates for over and double time
    overTimePay = hourlyRate * 1.5 * overTimeHours;
    grossPay = basePay + overTimePay + doubleTimePay;
}
else                                                                                    //accounting for over time below 10 hours
{
    basePay = (hoursWorked - overTimeHours) * hourlyRate;
    overTimePay = overTimeHours * (hourlyRate * 1.5);
    grossPay = basePay + overTimePay + doubleTimePay;

return (grossPay);
}


double benefit_Status_function (double medicalBenefitCost. double, medicalBenefitAmount)
{
double benefitStatus;
  while (fullTime=='y')
    if (benefitStatus(fullTime)==true
            medicalBenefitAmount = modifiedGrossPay - witholdCalc - medicalBenefitCost
        break;
    else if (benefitStatus(fullTime)==false)
         medicalBenefitAmount = 0
         break;
    }
return (benefitStatus)
  }
bool medicalBenefitsCost(double fullTime)
{
if(fulltime == y || (medicalBenefitsCost = netPay * .07))
return true;
	else
return false;
}


double IRA_function (double grossPay, double iraDeductionAmount)                                                       // This function calculates the IRA deduction based on gross pay

{
    double iraDeductionAmount;

    if(grossPay >=500)
        iraDeductionAmount = grossPay * 0.10;

    else if (grossPay > 400)
        iraDeductionAmount = grossPay * 0.05;

    else
        iraDeductionAmount = 0;

        iraDeductionAmount = IRA_function(grossPay);                                        // IRA function
modifiedGrossPay = grossPay - iraDeductionAmount;                                   // modified gross pay calculation prior to tax



    return(iraDeductionAmount);

}

double withold_function (int dependents, double modifiedGrossPay)                           // This function computes withholding based on dependents

{
 double subtotal;

 double withold = 0;

 if (dependents > 2)
    withold = 0.15;

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

 else if (dependents == 1)
    withold = 0.2;

 else if (dependents == 0)
    withold = 0.28;

subtotal = modifiedGrossPay * withold;

return (subtotal);


}
witholdCalc = withold_function(dependents, modifiedGrossPay);                       // Withholding function
modifiedNetPay = modifiedGrossPay - witholdCalc - medicalBenefitsCost;                                            // net pay calculation of modified gross pay

medicalBenefitsCost = netPay * .07;                                               // medical benefits function

}
Here was the code I was working on for the last few weeks before doing additional modifications as needed above:
Working 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
#include<iostream>
using namespace std;

// function for IRA deposits
double IRA_function (double grossPay);

//function for tax withholdings based on # of dependents
double withold_function (int dependents, double grossPay);


int main()
{

int hoursWorked, overTimeHours, doubleTimeHours, dependents;
double withold, hourlyRate, basePay, grossPay, overTimePay, doubleTimePay, witholdCalc, netPay, iraDeductionAmount, modifiedGrossPay;

overTimeHours = 0;
doubleTimeHours = 0;


// INPUT


cout << "Please enter your hourly rate of pay: " << endl;                             //user inputs hourly rate
cin >> hourlyRate;

cout << "Please enter how the number of hours worked: " << endl;                      //user inputs number of hours worked
cin >> hoursWorked;

cout << "Please enter how many dependents you have: " << endl;                      //user inputs number of dependents
cin >> dependents;



if (hoursWorked > 40)                                                                 //calculating if hours are over 40
{
    basePay = hourlyRate * 40;
    overTimeHours = hoursWorked - 40;
}
if (overTimeHours > 10)                                                               //calculating overtime and double time hours
{
    doubleTimeHours = overTimeHours - 10;
    overTimeHours = 10;
    doubleTimePay = hourlyRate * 2 * doubleTimeHours;                                 // calculating rates for over and double time
    overTimePay = hourlyRate * 1.5 * overTimeHours;
    grossPay = basePay + overTimePay + doubleTimePay;
}
else                                                                                    //accounting for over time below 10 hours
{
    basePay = (hoursWorked - overTimeHours) * hourlyRate;
    overTimePay = overTimeHours * (hourlyRate * 1.5);
    grossPay = basePay + overTimePay + doubleTimePay;
}


iraDeductionAmount = IRA_function(grossPay);                                        // IRA function
modifiedGrossPay = grossPay - iraDeductionAmount;                                   // modified gross pay calculation prior to tax


witholdCalc = withold_function(dependents, modifiedGrossPay);                       // Withholding function
netPay = modifiedGrossPay - witholdCalc;                                            // net pay calculation of modified gross pay


// OUTPUT


    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    cout << "******************************************" << endl;                                       // input is given to the user
    cout << "You worked " << hoursWorked << " hours total this pay period." << endl;
    cout << overTimeHours << " hours are paid at time and a half." << endl;
    cout << doubleTimeHours << " hours are paid at double time." << endl;
    cout << "******************************************" << endl;
    cout << "Your total base pay is: $" << basePay << endl;                                             // this should calculate total base pay
    cout << "Your overtime at time and a half is: $" << overTimePay << endl;                            // this shows overtime, if applicable
    cout << "Your overtime at double time is: $" <<doubleTimePay << endl;                               // this shows double time
    cout << "Your total gross pay with all overtime is: $" << grossPay << endl;                         // this shows total gross, including overtime if applicable
    cout << "Your IRA deduction is: " << iraDeductionAmount << endl;                                    // adjusted netpay with IRA contributions
    cout << "Your modified gross pay is: $" << modifiedGrossPay << endl;
    cout << "Your tax withholding amount with " << dependents << " dependents is: $" << witholdCalc << endl;        // amount of withholdings
    cout << "Your modified net pay is now: $" << netPay << endl;                                                     // amount of netpay

return 0;


}   // end of main



double IRA_function (double grossPay)                                                       // This function calculates the IRA deduction based on gross pay

{
    double iraDeductionAmount;

    if(grossPay >= 500)
        iraDeductionAmount = grossPay * 0.10;

    else if (grossPay > 400)
        iraDeductionAmount = grossPay * 0.05;

    else
        iraDeductionAmount = 0;

    return(iraDeductionAmount);

}

double withold_function (int dependents, double modifiedGrossPay)                           // This function computes withholding based on dependents

{
 double subtotal;

 double withold = 0;

 if (dependents > 2)
    withold = 0.15;

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

 else if (dependents == 1)
    withold = 0.2;

 else if (dependents == 0)
    withold = 0.28;

subtotal = modifiedGrossPay * withold;

return (subtotal);
}
Hi,

Some things to help out:

MAKE SURE you initialise all of your variables to something at the same time as declaration - one per line.

You haven't called any of your functions from main()


Only send the arguments you need to a function (that is, the numbers you need to do the calculation and the variables you want values assigned to as references)

Don't re-declare the arguments as local variables inside the function - you already have access to them because they were arguments.

Don't reset arguments to zero inside the function.

The assignments to variables in functions don't automatically change the value of the variable in main unless you send arguments as references.

Functions don't have access to variables outside the function unless you send them as arguments.

Be careful with your variable names lines 107 - 126 are particularly confusing.

Put white space after the end of function definitions. line 120

This is not a complete list, I have to go to sleep now - hope it all works out.
Thanks for your help. I tried to work on what I could. As I stated, I'm new so I know this can be frustrated but I didn't 100% understand all the feedback. Thanks!

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
173
174
175
176
177
/*This program calculates pay, pre-tax IRA contribution,
taxes based on the number of dependents and medical benefit costs for full-time employees.
It then calculates the adjusted net pay based on these calculations.
The user will be prompted for wage, hours worked, number of dependents and employment status.
This program involves moving Main to individual functions. */

#include<iostream>
using namespace std;

// void - function for User Input
void getUserInput_function(int hoursWorked, double hourlyRate, double dependents, double fullTime);

// void - function to calculate Overtime Pay
void calculateOvertimePay_function(int hoursWorked, double hourlyRate, double &basePay, double &overTimeHours, double &doubleTimeHours, double &overTimePay, double &grossPay, double &doubleTimePay);

// function for IRA deposits
double IRA_function (double grossPay);

//function for tax withholdings based on # of dependents
double withold_function (int dependents, double grossPay);

// function for Payroll Output
double outputPayrollInfo_function (double modifiedGrossPay, double modifiedNetPay);


int main()
{
int hoursWorked, overTimeHours, doubleTimeHours, dependents;
double hourlyRate, basePay, grossPay, overTimePay, doubleTimePay, witholdCalc, netPay, iraDeductionAmount, modifiedGrossPay, modifiedNetPay;
}

// function call for User Input
void getUserInput_function(int hoursWorked, double hourlyRate, double dependents, double fullTime);

// function call to calculate Overtime Pay
double calculateOvertimePay_function(int hoursWorked, double hourlyRate, double basePay, double overTimeHours, double doubleTimeHours, double overTimePay, double grossPay, double doubleTimePay);

// function call to output Payroll Info
double outputPayrollInfo_function (double modifiedGrossPay, double modifiedNetPay);


//function call for IRA function
iraDeductionAmount = IRA_function (grossPay);
modifiedGrossPay = grossPay - iraDeductionAmount;                                   // modified gross pay calculation prior to tax

// function call for withholdings function
withold = withold_function(dependents, modifiedGrossPay);
netPay = modifiedGrossPay - witholdCalc;                                            // net pay calculation of modified gross pay

cout << "Are you a full-time employee? Enter 'Y' for yes or 'N' for no: " <<endl;    // user inputs employment status
cin >> employmentStatus;

if (employmentStatus == 'Y' || employmentStatus == 'y')

medicalBenefitCost = netPay * .07;
modifiedNetPay = netPay - medicalBenefitCost;                                        //net Pay adjusted for medical benefits

}
// end of main

{
void getUserInput_function(int hoursWorked, double hourlyRate, double dependents, double fullTime);

cout << "Please enter your hourly rate of pay: " << endl;                             //user inputs hourly rate
cin >> hourlyRate;

cout << "Please enter how the number of hours worked: " << endl;                      //user inputs number of hours worked
cin >> hoursWorked;

cout << "Please enter how many dependents you have: " << endl;                      //user inputs number of dependents
cin >> dependents;

}

{

// void - function to calculate Overtime Pay
{

void calculateOvertimePay_function(int hoursWorked, double hourlyRate, double &basePay, double &overTimeHours, double &doubleTimeHours, double &overTimePay, double &grossPay, double &doubleTimePay);

if (hoursWorked > 40)                                                                 //calculating if hours are over 40
{
    basePay = hourlyRate * 40;
    overTimeHours = hoursWorked - 40;
}
if (overTimeHours > 10)                                                               //calculating overtime and double time hours
{
    doubleTimeHours = overTimeHours - 10;
    overTimeHours = 10;
    doubleTimePay = hourlyRate * 2 * doubleTimeHours;                                 // calculating rates for over and double time
    overTimePay = hourlyRate * 1.5 * overTimeHours;
    grossPay = basePay + overTimePay + doubleTimePay;
}
else                                                                                    //accounting for over time below 10 hours
{
    basePay = (hoursWorked - overTimeHours) * hourlyRate;
    overTimePay = overTimeHours * (hourlyRate * 1.5);
    grossPay = basePay + overTimePay + doubleTimePay;
}

{

double IRA_function(double grossPay);                // This function calculates the IRA deduction based on gross pay

    double iraDeductionAmount;

    if(grossPay >= 500)
        iraDeductionAmount = grossPay * 0.10;

    else if (grossPay > 400)
        iraDeductionAmount = grossPay * 0.05;

    else
        iraDeductionAmount = 0;

    return(iraDeductionAmount);

}

double withold_function (int dependents, double modifiedGrossPay);          // This function computes withholding based on dependents

 double subtotal;

 double withold = 0;

 if (dependents > 2)
    withold = 0.15;

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

 else if (dependents == 1)
    withold = 0.2;

 else if (dependents == 0)
    withold = 0.28;

subtotal = modifiedGrossPay * withold;

return (subtotal);

}

{

double outputPayrollInfo_function (double modifiedGrossPay, double modifiedNetPay);

cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    cout << "******************************************" << endl;
    cout << "                                          " << endl;                                      // input is given to the user
    cout << "You worked " << hoursWorked << " hours total this pay period." << endl;
    cout << overTimeHours << " hour(s) are paid at time and a half." << endl;
    cout << doubleTimeHours << " hour(s) are paid at double time." << endl;
     cout << "                                          " << endl;
    cout << "******************************************" << endl;
     cout << "                                          " << endl;
    cout << "Your total base pay is: $" << basePay << endl;                                             // this should calculate total base pay
    cout << "Your overtime at time and a half is: $" << overTimePay << endl;                            // this shows overtime, if applicable
    cout << "Your overtime at double time is: $" <<doubleTimePay << endl;                               // this shows double time
    cout << "Your total gross pay with all overtime is: $" << grossPay << endl;                         // this shows total gross, including overtime if applicable
    cout << "                                          " << endl;
    cout << "Your IRA deduction is: $" << iraDeductionAmount << endl;                                    // adjusted netpay with IRA contributions
    cout << "Your modified gross pay is: $" << modifiedGrossPay << endl;
     cout << "                                          " << endl;
    cout << "Your tax withholding amount with " << dependents << " dependent(s) is: $" << witholdCalc << endl;       // amount of withholdings
    cout << "Your modified net pay is now: $" << netPay << endl;
    cout << "                                          " << endl;
    cout << "If you work full-time employee, you pay $" << medicalBenefitCost << " towards medical costs," <<endl;
    cout << "so your adjusted net pay is now: $" << modifiedNetPay << endl;                                         // for FT employees, adjusted net pay for medical                                                  // amount of netpay

return 0;
} }
}
Hi,

A function definition must be enclosed in braces:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double MyFunction(double MyDouble); // function declaration with semicolon

int main() { // definition of main in braces


return 0;

}

double MyFunction(double MyDouble) // function definition in braces
{


return MyDouble;
}


For variables that are assigned values in a function getUserInput to be availble in main(), the variables must be passed as references:

void getUserInput (int &hoursWorked, double &hourlyRate, double &dependents, double &fullTime);

You should always initialse you r variables to something even if you immediately get input.

Maybe you should start a new project which is just the same as this one but it only has one function apart from main. Get the concepts down for that, get it working, then start adding new functions after that.

Cheers

Thanks a lot. I redid my initial project and it ran with no errors (I also redid my medical benefits function). I went back through and did some tweeking but am still getting a few build errors.

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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include<iostream>
using namespace std;


// void function declaration for User Input
void getUserInput_function(int hoursWorked, int employmentStatus, double hourlyRate, double dependents);

// void function declaration to calculate Overtime Pay
void calculateOvertimePay_function(int hoursWorked, double hourlyRate, double &basePay, int &overTimeHours, int &doubleTimeHours, double &overTimePay, double &grossPay, double &doubleTimePay);

// function declaration for IRA deposits
double IRA_function(double grossPay);

//function declaration for tax withholdings based on # of dependents
double withold_function(int dependents, double grossPay);

// function declaration to calculate medical benefit cost
double medical_cost_function(int employmentStatus, double netPay);

// function declaration for Payroll Output
double outputPayrollInfo_function (double modifiedGrossPay, double modifiedNetPay);


int main()
{
int hoursWorked, overTimeHours, doubleTimeHours, dependents, employmentStatus;
double hourlyRate, basePay, grossPay, netPay, overTimePay, doubleTimePay, modifiedGrossPay, modifiedNetPay, witholdCalc, iraDeductionAmount, medicalBenefitCost;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

overTimeHours = 0;
doubleTimeHours = 0;


// function call for User Input
getUserInput_function(hoursWorked, employmentStatus, hourlyRate, dependents);

// function call to calculate Overtime Pay
calculateOvertimePay_function(hoursWorked, overTimeHours, doubleTimeHours, hourlyRate, basePay,  overTimePay, grossPay, doubleTimePay);

// function call to output Payroll Info
outputPayrollInfo_function(modifiedGrossPay, modifiedNetPay);

// function call for IRA
iraDeductionAmount = IRA_function(grossPay);
modifiedGrossPay = grossPay - iraDeductionAmount;                                   // modified gross pay calculation prior to tax

 // function call for withholdings
witholdCalc = withold_function(dependents, modifiedGrossPay);
netPay = modifiedGrossPay - witholdCalc;                                            // net pay calculation of modified gross pay

// function call for medical benefits
medicalBenefitCost = medical_cost_function(employmentStatus, netPay);
modifiedNetPay = netPay - medicalBenefitCost;                                       //net Pay adjusted for medical benefits

}

// end of main

void getUserInput_function(int &hoursWorked, double &hourlyRate, double &dependents, double &employmentStatus);

{

cout << "Please enter your hourly rate of pay: $" << endl;                            //user inputs hourly rate
cin >> hourlyRate;

cout << "Please enter how the number of hours worked: " << endl;                      //user inputs number of hours worked
cin >> hoursWorked;

cout << "Please enter how many dependents you have: " << endl;                        //user inputs number of dependents
cin >> dependents;

cout << "Enter 1 if you are a full-time employee and 0 for part time: " <<endl;       // user inputs employment status
cin >> employmentStatus;

}

// void - function to calculate Overtime Pay

{

void calculateOvertimePay_function(int hoursWorked, double hourlyRate, double &basePay, double &overTimeHours, double &doubleTimeHours, double &overTimePay, double &grossPay, double &doubleTimePay);

if (hoursWorked > 40)                                                                 //calculating if hours are over 40
{
    basePay = hourlyRate * 40;
    overTimeHours = hoursWorked - 40;
}
if (overTimeHours > 10)                                                               //calculating overtime and double time hours
{
    doubleTimeHours = overTimeHours - 10;
    overTimeHours = 10;
    doubleTimePay = hourlyRate * 2 * doubleTimeHours;                                 // calculating rates for over and double time
    overTimePay = hourlyRate * 1.5 * overTimeHours;
    grossPay = basePay + overTimePay + doubleTimePay;
}
else                                                                                    //accounting for over time below 10 hours
{
    basePay = (hoursWorked - overTimeHours) * hourlyRate;
    overTimePay = overTimeHours * (hourlyRate * 1.5);
    grossPay = basePay + overTimePay + doubleTimePay;
}


// IRA function definition.  Calculates deduction based on gross pay
double IRA_function (double grossPay)

{
    double iraDeductionAmount;

    if(grossPay >= 500)
        iraDeductionAmount = grossPay * 0.10;

    else if (grossPay > 400)
        iraDeductionAmount = grossPay * 0.05;

    else
        iraDeductionAmount = 0;

    return(iraDeductionAmount);

}

// Withholding function definition.  Calculates withholding based on # of dependents.
double withold_function (int dependents, double modifiedGrossPay)

{

 double subtotal;

 double withold = 0;

 if (dependents > 2)
    withold = 0.15;

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

 else if (dependents == 1)
    withold = 0.2;

 else if (dependents == 0)
    withold = 0.28;

subtotal = modifiedGrossPay * withold;

return (subtotal);

}
    // Medical benefit function definition.  Calculates medical benefits costs based on employment status (1 for FT, 0 for PT)
    double medical_cost_function(int employmentStatus, double netPay)

{
    double medBenCost = 0;                       // initializing costs to zero to account for PT

    if (employmentStatus == 1)
        medBenCost = netPay * .07;

    return (medBenCost);
}

double outputPayrollInfo_function (double modifiedGrossPay, double modifiedNetPay);

{

    cout << "******************************************" << endl;
    cout << "                                          " << endl;                                      // input is given to the user
    cout << "You worked " << hoursWorked << " hours total this pay period." << endl;
    cout << overTimeHours << " hour(s) are paid at time and a half." << endl;
    cout << doubleTimeHours << " hour(s) are paid at double time." << endl;
     cout << "                                          " << endl;
    cout << "******************************************" << endl;
     cout << "                                          " << endl;
    cout << "Your total base pay is: $" << basePay << endl;                                             // this should calculate total base pay
    cout << "Your overtime at time and a half is: $" << overTimePay << endl;                            // this shows overtime, if applicable
    cout << "Your overtime at double time is: $" <<doubleTimePay << endl;                               // this shows double time
    cout << "Your total gross pay with all overtime is: $" << grossPay << endl;                         // this shows total gross, including overtime if applicable
    cout << "                                          " << endl;
    cout << "Your IRA deduction is: $" << iraDeductionAmount << endl;                                    // adjusted netpay with IRA contributions
    cout << "Your modified gross pay is: $" << modifiedGrossPay << endl;
     cout << "                                          " << endl;
    cout << "Your tax withholding amount with " << dependents << " dependent(s) is: $" << witholdCalc << endl;       // amount of withholdings
    cout << "Your modified net pay is now: $" << netPay << endl;
    cout << "                                          " << endl;
    cout << "If you work full-time employee, you pay $" << medicalBenefitCost << " towards medical costs," <<endl;
    cout << "so your adjusted net pay is now: $" << modifiedNetPay << endl;                                         // for FT employees, adjusted net pay for medical                                                  // amount of netpay

return 0;
} }
Also, here is the errors:


-------------- Build: Debug in Lab 6 (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -fexceptions -g -c "E:\CSE 110\Lab 6\main.cpp" -o obj\Debug\main.o
E:\CSE 110\Lab 6\main.cpp: In function 'int main()':

LINE 40:
E:\CSE 110\Lab 6\main.cpp:47:134: error: invalid initialization of reference of type 'double&' from expression of type 'int'
calculateOvertimePay_function(hoursWorked, overTimeHours, doubleTimeHours, hourlyRate, basePay, overTimePay, grossPay, doubleTimePay);

LINE8:
E:\CSE 110\Lab 6\main.cpp:15:6: error: in passing argument 3 of 'void calculateOvertimePay_function(int, double, double&, int&, int&, double&, double&, double&)'
void calculateOvertimePay_function(int hoursWorked, double hourlyRate, double &basePay, int &overTimeHours, int &doubleTimeHours, double &overTimePay, double &grossPay, double &doubleTimePay);
^
E:\CSE 110\Lab 6\main.cpp: At global scope:
LINE 64:
E:\CSE 110\Lab 6\main.cpp:70:1: error: expected unqualified-id before '{' token
{
^
LINE 82:
E:\CSE 110\Lab 6\main.cpp:88:1: error: expected unqualified-id before '{' token
{
^
Process terminated with status 1 (0 minute(s), 0 second(s))
4 error(s), 0 warning(s) (0 minute(s), 0 second(s))
Last edited on
Made some changes... I can now get this to run but get a bunch of garbage. Help please.

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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include<iostream>
using namespace std;


// void function declaration for User Input
void getUserInput_function(int hoursWorked, int dependents, int employmentStatus, double hourlyRate);

// void function declaration to calculate Overtime Pay
void calculateOvertimePay_function(int &hoursWorked, double &hourlyRate, double &basePay, int &overTimeHours, int &doubleTimeHours, double &overTimePay, double &grossPay, double &doubleTimePay);

// function declaration for IRA deposits
double IRA_function(double grossPay);

//function declaration for tax withholdings based on # of dependents
double withold_function(int &dependents, double modifiedGrossPay);

// function declaration to calculate medical benefit cost
double medical_cost_function(int &employmentStatus, double netPay, double &modifiedNetPay);

// function declaration for Payroll Output
double outputPayrollInfo_function (int hoursWorked, int overTimeHours, int doubleTimeHours, int dependents, double basePay, double grossPay, double &modifiedGrossPay, double &netPay, double modifiedNetPay, double overTimePay, double doubleTimePay, double &witholdCalc, double &iraDeductionAmount, double &medicalBenefitCost);


int main()
{
int hoursWorked, overTimeHours, doubleTimeHours, dependents, employmentStatus;
double hourlyRate, basePay, grossPay, netPay, overTimePay, doubleTimePay, modifiedGrossPay, modifiedNetPay, witholdCalc, iraDeductionAmount, medicalBenefitCost;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

overTimeHours = 0;
doubleTimeHours = 0;


// function call for User Input
getUserInput_function(hoursWorked, dependents, employmentStatus, hourlyRate);

// function call to calculate Overtime Pay
calculateOvertimePay_function(hoursWorked, hourlyRate, basePay, overTimeHours, doubleTimeHours, overTimePay, grossPay, doubleTimePay);

// function call to output Payroll Info
outputPayrollInfo_function(hoursWorked, overTimeHours, doubleTimeHours, dependents, basePay, grossPay, modifiedGrossPay, netPay, modifiedNetPay, overTimePay, doubleTimePay, witholdCalc, iraDeductionAmount, medicalBenefitCost);

// function call for IRA
iraDeductionAmount = IRA_function(grossPay);
modifiedGrossPay = grossPay - iraDeductionAmount;                                   // modified gross pay calculation prior to tax

 // function call for withholdings
witholdCalc = withold_function(dependents, modifiedGrossPay);
netPay = modifiedGrossPay - witholdCalc;                                            // net pay calculation of modified gross pay

// function call for medical benefits
medicalBenefitCost = medical_cost_function(employmentStatus, netPay, modifiedNetPay);
modifiedNetPay = netPay - medicalBenefitCost;                                       //net Pay adjusted for medical benefits

}  // end of main

void getUserInput_function(int hoursWorked, int employmentStatus, int hourlyRate, double dependents)

{

cout << "Please enter your hourly rate of pay: $" << endl;                            //user inputs hourly rate
cin >> hourlyRate;

cout << "Please enter how the number of hours worked: " << endl;                      //user inputs number of hours worked
cin >> hoursWorked;

cout << "Please enter how many dependents you have: " << endl;                        //user inputs number of dependents
cin >> dependents;

cout << "Enter 1 if you are a full-time employee and 0 for part time: " <<endl;       // user inputs employment status
cin >> employmentStatus;
}

// void - function to calculate Overtime Pay

void calculateOvertimePay_function(int &hoursWorked, double &hourlyRate, double &basePay, int &overTimeHours, int &doubleTimeHours, double &overTimePay, double &grossPay, double &doubleTimePay)

{

if (hoursWorked > 40)                                                                 //calculating if hours are over 40
{
    basePay = hourlyRate * 40;
    overTimeHours = hoursWorked - 40;
}
if (overTimeHours > 10)                                                               //calculating overtime and double time hours
{
    doubleTimeHours = overTimeHours - 10;
    overTimeHours = 10;
    doubleTimePay = hourlyRate * 2 * doubleTimeHours;                                 // calculating rates for over and double time
    overTimePay = hourlyRate * 1.5 * overTimeHours;
    grossPay = basePay + overTimePay + doubleTimePay;
}
else                                                                                    //accounting for over time below 10 hours
{
    basePay = (hoursWorked - overTimeHours) * hourlyRate;
    overTimePay = overTimeHours * (hourlyRate * 1.5);
    grossPay = basePay + overTimePay + doubleTimePay;
}
}

// IRA function definition.  Calculates deduction based on gross pay
double IRA_function (double grossPay)

{
    double iraDeductionAmount;

    if(grossPay >= 500)
        iraDeductionAmount = grossPay * 0.10;

    else if (grossPay > 400)
        iraDeductionAmount = grossPay * 0.05;

    else
        iraDeductionAmount = 0;

    return(iraDeductionAmount);

}

// Withholding function definition.  Calculates withholding based on # of dependents.
double withold_function (int &dependents, double modifiedGrossPay)

{

 double subtotal;

 double withold = 0;

 if (dependents > 2)
    withold = 0.15;

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

 else if (dependents == 1)
    withold = 0.2;

 else if (dependents == 0)
    withold = 0.28;

subtotal = modifiedGrossPay * withold;

return (subtotal);

}

// Medical benefit function definition.  Calculates medical benefits costs based on employment status (1 for FT, 0 for PT)
    double medical_cost_function(int &employmentStatus, double netPay, double &modifiedNetPay)

{
    double medBenCost = 0; 

    if (employmentStatus == 1)
        medBenCost = netPay * .07;

    return (medBenCost);
}

double outputPayrollInfo_function(int hoursWorked, int overTimeHours, int doubleTimeHours, int dependents, double basePay, double grossPay, double &modifiedGrossPay, double &netPay, double modifiedNetPay, double overTimePay, double doubleTimePay, double &witholdCalc, double &iraDeductionAmount, double &medicalBenefitCost)

{

    cout << "******************************************" << endl;
    cout << "                                          " << endl;                                     
    cout << "You worked " << hoursWorked << " hours total this pay period." << endl;
    cout << overTimeHours << " hour(s) are paid at time and a half." << endl;
    cout << doubleTimeHours << " hour(s) are paid at double time." << endl;
    cout << "                                          " << endl;
    cout << "******************************************" << endl;
    cout << "                                          " << endl;
    cout << "Your total base pay is: $" << basePay << endl;              
    cout << "Your overtime at time and a half is: $" << overTimePay << endl;               
    cout << "Your overtime at double time is: $" <<doubleTimePay << endl;                       
    cout << "Your total gross pay with all overtime is: $" << grossPay << endl;   
    cout << "                                          " << endl;
    cout << "Your IRA deduction is: $" << iraDeductionAmount << endl;
    cout << "Your modified gross pay is: $" << modifiedGrossPay << endl;
    cout << "                                          " << endl;
    cout << "Your tax withholding amount with " << dependents << " dependent(s) is: $" << witholdCalc << endl;       // amount of withholdings
    cout << "Your modified net pay is now: $" << netPay << endl;
    cout << "                                          " << endl;
    cout << "If you work full-time employee, you pay $" << medicalBenefitCost << " towards medical costs," <<endl;
    cout << "so your adjusted net pay is now: $" << modifiedNetPay << endl;                       
return 0;
}

Last edited on
Hi

Maybe you should start a new project which is just the same as this one but it only has one function apart from main. Get the concepts down for that, get it working, then start adding new functions after that.


I strongly suggest you do this, we will get one function working, then you can apply lessons learnt from that to all the other functions.
Some other stuff ...

When compiling turn on some other switches such as -Wextra -pedantic this will produce more warnings about things that will be a problem. You will have to sort out how to do this in your IDE. Look at the documentation to find out what they mean.

Also, having using namespace std; is a bad idea - Google to see why. I wish authors & lecturers would quit doing this.

Function parameter lists (the arguments) must match between the function declaration and it's definition. Those errors came from specifying references in one but not in the other.

Function definitions don't have a semicolon on the first line. for example remove the one on line 62.

Make sure you understand what the different types are and how they work. C++ is a strongly typed language - the types must match up.

Cheers
Thanks for the information. I don't know that my lecturer is accepting other formats and I'm not familiar with switches yet. I did some Googling and will continue to look into this. I pulled my code apart so it's just simply input/some overtime/and output. I'm not returning zeros. I tried differing troubleshooting with using the change (&) but still no where...

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
#include<iostream>
using namespace std;


// void function declaration for User Input
void getUserInput_function(int &hoursWorked, int &dependents, int &employmentStatus, double &hourlyRate);

// void function declaration to calculate Overtime Pay
void calculateOvertimePay_function(int &hoursWorked, double hourlyRate, double &basePay, int &overTimeHours, int &doubleTimeHours, double &overTimePay, double &grossPay, double &doubleTimePay);

// function declaration for Payroll Output
double outputPayrollInfo_function (int hoursWorked, int &overTimeHours, int &doubleTimeHours, int dependents, double &basePay, double &grossPay, double &modifiedGrossPay, double &netPay, double &modifiedNetPay, double &overTimePay, double &doubleTimePay, double &witholdCalc, double &iraDeductionAmount, double &medicalBenefitCost);

int main()
{
int hoursWorked, overTimeHours, doubleTimeHours, dependents, employmentStatus;
double hourlyRate, basePay, grossPay, netPay, overTimePay, doubleTimePay, modifiedGrossPay, modifiedNetPay, witholdCalc, iraDeductionAmount, medicalBenefitCost;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

overTimeHours = 0;
doubleTimeHours = 0;


// function call for User Input
getUserInput_function(hoursWorked, dependents, employmentStatus, hourlyRate);

// function call to calculate Overtime Pay
calculateOvertimePay_function(hoursWorked, hourlyRate, basePay, overTimeHours, doubleTimeHours, overTimePay, grossPay, doubleTimePay);

// function call to output Payroll Info
outputPayrollInfo_function(hoursWorked, overTimeHours, doubleTimeHours, dependents, basePay, grossPay, modifiedGrossPay, netPay, modifiedNetPay, overTimePay, doubleTimePay, witholdCalc, iraDeductionAmount, medicalBenefitCost);
}

void getUserInput_function(int &hoursWorked, int &employmentStatus, int &hourlyRate, double &dependents)
{

cout << "Please enter your hourly rate of pay: $" << endl;                            //user inputs hourly rate
cin >> hourlyRate;

cout << "Please enter how the number of hours worked: " << endl;                      //user inputs number of hours worked
cin >> hoursWorked;
}

// void - function to calculate Overtime Pay

void calculateOvertimePay_function(int &hoursWorked, double hourlyRate, double &basePay, int &overTimeHours, int &doubleTimeHours, double &overTimePay, double &grossPay, double &doubleTimePay)

{


if (hoursWorked > 40)                                                                 //calculating if hours are over 40
{
    basePay = hourlyRate * 40;
    overTimeHours = hoursWorked - 40;
}
if (overTimeHours > 10)                                                               //calculating overtime and double time hours
{
    doubleTimeHours = overTimeHours - 10;
    overTimeHours = 10;
    doubleTimePay = hourlyRate * 2 * doubleTimeHours;                                 // calculating rates for over and double time
    overTimePay = hourlyRate * 1.5 * overTimeHours;
    grossPay = basePay + overTimePay + doubleTimePay;
}
else                                                                                    //accounting for over time below 10 hours
{
    basePay = (hoursWorked - overTimeHours) * hourlyRate;
    overTimePay = overTimeHours * (hourlyRate * 1.5);
    grossPay = basePay + overTimePay + doubleTimePay;
}

}

double outputPayrollInfo_function (int hoursWorked, int &overTimeHours, int &doubleTimeHours, int dependents, double &basePay, double &grossPay, double &modifiedGrossPay, double &netPay, double &modifiedNetPay, double &overTimePay, double &doubleTimePay, double &witholdCalc, double &iraDeductionAmount, double &medicalBenefitCost)

{

    cout << "******************************************" << endl;
    cout << "                                          " << endl;                                      // input is given to the user
    cout << "You worked " << hoursWorked << " hours total this pay period." << endl;
    cout << overTimeHours << " hour(s) are paid at time and a half." << endl;
    cout << doubleTimeHours << " hour(s) are paid at double time." << endl;
    cout << "                                          " << endl;
    cout << "******************************************" << endl;
    cout << "                                          " << endl;
    cout << "Your total base pay is: $" << basePay << endl;                                             // this should calculate total base pay
    cout << "Your overtime at time and a half is: $" << overTimePay << endl;                            // this shows overtime, if applicable
    cout << "Your overtime at double time is: $" <<doubleTimePay << endl;                               // this shows double time
    cout << "Your total gross pay with all overtime is: $" << grossPay << endl;                         // this shows total gross, including overtime if applicable

return 0;
}
Starting from your "working" version in the second post. Cleaned up the indentation and move the output to a new function named display_payroll_results() where all its parameters are passed by value:
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
#include<iostream>
using namespace std;

// function for IRA deposits
double IRA_function (double grossPay);

//function for tax withholdings based on # of dependents
double withold_function (int dependents, double grossPay);

// Display Payroll Results
// All parameters are inputs only so are passed "by value"
void display_payroll_results(int hoursWorked, int overTimeHours, int doubleTimeHours, int dependents, 
                             double basePay, double grossPay, double overTimePay, 
                             double doubleTimePay, double witholdCalc, double netPay, double iraDeductionAmount, double modifiedGrossPay
)
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    cout << "******************************************" << endl;                                       // input is given to the user
    cout << "You worked " << hoursWorked << " hours total this pay period." << endl;
    cout << overTimeHours << " hours are paid at time and a half." << endl;
    cout << doubleTimeHours << " hours are paid at double time." << endl;
    cout << "******************************************" << endl;
    cout << "Your total base pay is: $" << basePay << endl;                                             // this should calculate total base pay
    cout << "Your overtime at time and a half is: $" << overTimePay << endl;                            // this shows overtime, if applicable
    cout << "Your overtime at double time is: $" <<doubleTimePay << endl;                               // this shows double time
    cout << "Your total gross pay with all overtime is: $" << grossPay << endl;                         // this shows total gross, including overtime if applicable
    cout << "Your IRA deduction is: " << iraDeductionAmount << endl;                                    // adjusted netpay with IRA contributions
    cout << "Your modified gross pay is: $" << modifiedGrossPay << endl;
    cout << "Your tax withholding amount with " << dependents << " dependents is: $" << witholdCalc << endl;        // amount of withholdings
    cout << "Your modified net pay is now: $" << netPay << endl;                                                     // amount of netpay
}

int main()
{

    int hoursWorked, overTimeHours, doubleTimeHours, dependents;
    double withold, hourlyRate, basePay, grossPay, overTimePay, doubleTimePay, witholdCalc, netPay, iraDeductionAmount, modifiedGrossPay;

    overTimeHours = 0;
    doubleTimeHours = 0;


    // INPUT


    cout << "Please enter your hourly rate of pay: " << endl;                             //user inputs hourly rate
    cin >> hourlyRate;

    cout << "Please enter how the number of hours worked: " << endl;                      //user inputs number of hours worked
    cin >> hoursWorked;

    cout << "Please enter how many dependents you have: " << endl;                      //user inputs number of dependents
    cin >> dependents;



    if (hoursWorked > 40)                                                                 //calculating if hours are over 40
    {
        basePay = hourlyRate * 40;
        overTimeHours = hoursWorked - 40;
    }
    if (overTimeHours > 10)                                                               //calculating overtime and double time hours
    {
        doubleTimeHours = overTimeHours - 10;
        overTimeHours = 10;
        doubleTimePay = hourlyRate * 2 * doubleTimeHours;                                 // calculating rates for over and double time
        overTimePay = hourlyRate * 1.5 * overTimeHours;
        grossPay = basePay + overTimePay + doubleTimePay;
    }
    else                                                                                    //accounting for over time below 10 hours
    {
        basePay = (hoursWorked - overTimeHours) * hourlyRate;
        overTimePay = overTimeHours * (hourlyRate * 1.5);
        grossPay = basePay + overTimePay + doubleTimePay;
    }


    iraDeductionAmount = IRA_function(grossPay);                                        // IRA function
    modifiedGrossPay = grossPay - iraDeductionAmount;                                   // modified gross pay calculation prior to tax


    witholdCalc = withold_function(dependents, modifiedGrossPay);                       // Withholding function
    netPay = modifiedGrossPay - witholdCalc;                                            // net pay calculation of modified gross pay


    // Output
    display_payroll_results(hoursWorked, overTimeHours, doubleTimeHours, dependents, 
                            basePay, grossPay, overTimePay, doubleTimePay, witholdCalc, netPay, iraDeductionAmount, modifiedGrossPay);
    return 0;


}   // end of main



double IRA_function (double grossPay)                                                       // This function calculates the IRA deduction based on gross pay

{
    double iraDeductionAmount;

    if(grossPay >= 500)
        iraDeductionAmount = grossPay * 0.10;

    else if (grossPay > 400)
        iraDeductionAmount = grossPay * 0.05;

    else
        iraDeductionAmount = 0;

    return(iraDeductionAmount);

}

double withold_function (int dependents, double modifiedGrossPay)                           // This function computes withholding based on dependents

{
     double subtotal;

     double withold = 0;

     if (dependents > 2)
        withold = 0.15;

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

     else if (dependents == 1)
        withold = 0.2;

     else if (dependents == 0)
        withold = 0.28;

    subtotal = modifiedGrossPay * withold;

    return (subtotal);
}

The compiler warns about that version having unused local variables or uninitialized local variables. Do I fix them now or after I make a clean calculate_gross_pay function? After.
I should have said in my last posting that TheIdeasMan has been giving you excellent advice.

I added calculate_gross_pay() but not showing IRA_function() and withold_function() which are not changing:
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
#include<iostream>
using namespace std;

// function for IRA deposits
double IRA_function (double grossPay);

//function for tax withholdings based on # of dependents
double withold_function (int dependents, double grossPay);

// Calculate Gross Pay
// input parameters hourlyRate, hoursWorked are pass by value.  All the output parameters are pass by reference.  All will be set.
// Function return value is the grossPay.  
double calc_gross_pay(double hourlyRate, int hoursWorked, 
                      int & overTimeHours, int & doubleTimeHours, double & basePay, double & overTimePay, double & doubleTimePay)
{
    double grossPay = 0.0;

    if (hoursWorked > 40)                                                                 //calculating if hours are over 40
    {
        basePay = hourlyRate * 40;
        overTimeHours = hoursWorked - 40;
    }
    if (overTimeHours > 10)                                                               //calculating overtime and double time hours
    {
        doubleTimeHours = overTimeHours - 10;
        overTimeHours = 10;
        doubleTimePay = hourlyRate * 2 * doubleTimeHours;                                 // calculating rates for over and double time
        overTimePay = hourlyRate * 1.5 * overTimeHours;
        grossPay = basePay + overTimePay + doubleTimePay;
    }
    else                                                                                    //accounting for over time below 10 hours
    {
        basePay = (hoursWorked - overTimeHours) * hourlyRate;
        overTimePay = overTimeHours * (hourlyRate * 1.5);
        grossPay = basePay + overTimePay + doubleTimePay;
    }

    return grossPay;
}

// Display Payroll Results
// All parameters are inputs only so are passed "by value"
void display_payroll_results(int hoursWorked, int overTimeHours, int doubleTimeHours, int dependents, 
                             double basePay, double grossPay, double overTimePay, 
                             double doubleTimePay, double witholdCalc, double netPay, double iraDeductionAmount, double modifiedGrossPay
)
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    cout << "******************************************" << endl;                                       // input is given to the user
    cout << "You worked " << hoursWorked << " hours total this pay period." << endl;
    cout << overTimeHours << " hours are paid at time and a half." << endl;
    cout << doubleTimeHours << " hours are paid at double time." << endl;
    cout << "******************************************" << endl;
    cout << "Your total base pay is: $" << basePay << endl;                                             // this should calculate total base pay
    cout << "Your overtime at time and a half is: $" << overTimePay << endl;                            // this shows overtime, if applicable
    cout << "Your overtime at double time is: $" <<doubleTimePay << endl;                               // this shows double time
    cout << "Your total gross pay with all overtime is: $" << grossPay << endl;                         // this shows total gross, including overtime if applicable
    cout << "Your IRA deduction is: " << iraDeductionAmount << endl;                                    // adjusted netpay with IRA contributions
    cout << "Your modified gross pay is: $" << modifiedGrossPay << endl;
    cout << "Your tax withholding amount with " << dependents << " dependents is: $" << witholdCalc << endl;        // amount of withholdings
    cout << "Your modified net pay is now: $" << netPay << endl;                                                     // amount of netpay
}

int main()
{

    int hoursWorked, overTimeHours, doubleTimeHours, dependents;
    double withold, hourlyRate, basePay, grossPay, overTimePay, doubleTimePay, witholdCalc, netPay, iraDeductionAmount, modifiedGrossPay;

    overTimeHours = 0;
    doubleTimeHours = 0;


    // INPUT


    cout << "Please enter your hourly rate of pay: " << endl;                             //user inputs hourly rate
    cin >> hourlyRate;

    cout << "Please enter how the number of hours worked: " << endl;                      //user inputs number of hours worked
    cin >> hoursWorked;

    cout << "Please enter how many dependents you have: " << endl;                      //user inputs number of dependents
    cin >> dependents;

    grossPay = calc_gross_pay(hourlyRate, hoursWorked, 
        overTimeHours, doubleTimeHours, basePay, overTimePay, doubleTimePay);


    iraDeductionAmount = IRA_function(grossPay);                                        // IRA function
    modifiedGrossPay = grossPay - iraDeductionAmount;                                   // modified gross pay calculation prior to tax


    witholdCalc = withold_function(dependents, modifiedGrossPay);                       // Withholding function
    netPay = modifiedGrossPay - witholdCalc;                                            // net pay calculation of modified gross pay


    // Output
    display_payroll_results(hoursWorked, overTimeHours, doubleTimeHours, dependents, 
                            basePay, grossPay, overTimePay, doubleTimePay, witholdCalc, netPay, iraDeductionAmount, modifiedGrossPay);
    return 0;


}   // end of main 

Now main() is small enough for me to understand.

I looked back and saw that you created a similar function calculateOvertimePay_function() in your later postings. Your version does not return a value, but has a "call by reference" parameter grossPay. Your version uses "call by reference" for all the parameters, even the ones that are only inputs. However, with them all "call by reference" it is harder to see which you plan as inputs and which are outputs.

It is now easier see the execution paths through the calc_gross_pay() or the calculateOvertimePay_function(). There are several execution paths. However, are all the output parameters set for all possible inputs? No. In calc_gross_pay(), we needed a local variable named grossPay and I initialized it to 0.0 so the function return is always set. In your version, grossPay is not always set. What is the value of grossPay, basePay, and overTimeHours if the hoursWorked are less than or equal to 40?

I will leave it to you to fix calculateOvertimePay_function() or its cousin calc_gross_pay() so all the output parameters are set. If I were you, I would consider making three distinct paths through the function which set all the output parameters: hoursWorked <= 40, hoursWorked <=50 (and more than 40), and hours worked > 50. Then you can explicitly set each of the output variables. [Because I am not smart enough to handle complicated partial paths and still make sure they are all set.]
Last edited on
Thank you everyone for your help. I got this up and running finally... thanks again.
Topic archived. No new replies allowed.