In my First class using C++ need help with a lab

So I wrote this code for a lab and I'm lost with adding this function to it. Here are the directions that I'm following for this.

Modify Lab 2 (which is the code I posted below) by adding a function to the program that
calculates federal income tax withholding based on the
number of dependents and gross pay.
• Deduct the withholding amount from gross pay, and modify
the output to include the resulting net pay.
– Prompt the user for the number of dependents
– Determine the federal tax "withholding" amount based
on gross pay and the number of dependents


The withholding function will determine the income tax
withholding amount as a function of gross pay and the
number of dependents input by the user as follows:
0 dependents - withhold 28%
1 dependent - withhold 20%
2 dependents - withhold 18%
greater than 2 dependents - withhold 15%
• Modify the output to now include the amount withheld and
the net pay.

I need the output to look like this:
Total Hours Worked = x
Hourly pay rate = $xx.xx
Base pay = $xx.xx
Overtime hours at time and a half = x
Overtime pay at time and a half = $xx.xx
Overtime hours at double time = x
Overtime pay at double time = $xx.xx
Gross pay = $xx.xx
Taxes withheld = $xx.xx
Net pay = $xx.xx

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
#include <iostream>

using namespace std;

int main()

{
int hours_worked, timeHalf_hours = 0, doubleTime_hours = 0;
double gross_pay, pay_rate, base_pay, timeHalf_pay = 0, doubleTime_pay = 0;

cout << "Enter the hourly rate of pay: $";
cin >> pay_rate;
cout << "Enter the number of hours worked,\n"
<<"rounded to a whole number of hours: ";
cin >> hours_worked;

if (hours_worked <= 40)
{
base_pay = pay_rate * hours_worked;

}
if (hours_worked > 41 && hours_worked <= 50)
{
base_pay = pay_rate * 40;
timeHalf_hours = hours_worked - 40;
timeHalf_pay = 1.5 * pay_rate * (hours_worked - 40);
gross_pay = timeHalf_pay + base_pay;
}
else
{
base_pay = pay_rate * 40;
timeHalf_hours = hours_worked - 40 - (hours_worked - 50);
timeHalf_pay = 1.5 * pay_rate * (hours_worked - 40);
doubleTime_hours = hours_worked - 50;
doubleTime_pay = 2 * pay_rate * (hours_worked - 50);
gross_pay = doubleTime_pay + base_pay;
}


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

cout << " Hours worked = " << hours_worked << endl;
cout << "Hourly pay rate = $" << pay_rate << endl;
cout << "Base pay = $" << base_pay << endl;
cout << "timeHalf hours = " << timeHalf_hours << " hours"<< endl;
cout << "timeHalf pay = $" << timeHalf_pay << endl;
cout << "doubletime hours = " << doubleTime_hours << " hours"<< endl;
cout << " doubleTime pay = $" << doubleTime_pay<< endl;
cout << " Gross pay = $" << gross_pay << endl;



return 0;
}
Last edited on
Please edit your post and make sure your code is actually [code]between code tags[/code] so that it has line numbers and syntax highlighting.

What exactly is confusing you? You seem to have been OK writing the rest of the program.
I just don't understand how to create the function and where to place it.
Try reading the tutorial on functions:
http://www.cplusplus.com/doc/tutorial/functions/

At the very least, write what you can, and when you can proceed no further ask for help.
I'm not sure why I'm struggling with it, it seems fairly simple but even after I read that I'm still confused. I understand declare, define, then put it in my output. So I guess declaring it would be something along the lines of: int withholding (dependants) ?
Last edited on
I don't know much about tax calculation; I can only help you with syntax here.

Remember: your function parameters need types too, as they are effectively variables local to the function.
Topic archived. No new replies allowed.