trouble with if, else if statements in function

I can't find out what is wrong with my if and else-if statements in my function at the bottom of my code. It always reads that the withholding rate is 20% no matter what number i put in for "Number of dependents".

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

double withholding_amount (double grossPay, int numberOfDependents);

int main()
{
    int hours, overtimeHours, doubleTimehours, numberOfDependents;
    double rate, basePay, overtimePay, doubleTimepay, grossPay, withholdingAmount, netPay;
    cout << "Enter the hourly rate of pay: $";
    cin >> rate;
    cout << "Enter the number of hours worked,\n";
    cout << "rounded to the nearest whole number:";
    cin >> hours;
    cout << "Enter the number of dependents you have:";
    cin >> numberOfDependents;

overtimeHours = 0,
overtimePay = 0,
doubleTimehours = 0,
doubleTimepay = 0;
    if (hours <= 40)
    {
        basePay = rate * hours;
    }
    else
    {
        if (hours > 50)
        {
            basePay = rate * 40,
            overtimeHours = 10,
            overtimePay = (overtimeHours * 1.5 *rate),
            doubleTimehours = (hours - 50),
            doubleTimepay = 2 * rate * (hours - 50);
        }
        else
        {
            basePay = rate * 40,
            overtimeHours = (hours - 40),
            overtimePay = 1.5 * rate * (hours - 40);
        }
    }

grossPay = basePay + overtimePay + doubleTimepay;
withholdingAmount = withholding_amount(grossPay, numberOfDependents);
netPay = grossPay - withholdingAmount;

    cout << endl;
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout << "Total hours worked = " << hours << endl;
    cout << "Hourly pay rate = $" << rate << endl;
    cout << "Base pay = $" << basePay << endl;
    cout << "Overtime hours at time and a half = " << overtimeHours << endl;
    cout << "Overtime pay at time and a half = $" << overtimePay << endl;
    cout << "Overtime hours at double time = " << doubleTimehours << endl;
    cout << "Overtime pay at double time = $" << doubleTimepay << endl;
    cout << "Gross pay = $" << grossPay << endl;
    cout << "Taxes withheld = $" << withholdingAmount << endl;
    cout << "Net pay = $" << netPay << endl;
    return 0;
}

double withholding_amount(double grossPay, int numberOfDependents)
{
    double withholdRate, withholdingAmount;

    if (numberOfDependents = 0)
    {
        withholdRate = .28;
    }
    else if (numberOfDependents = 1)
    {
        withholdRate = .20;
    }
    else if (numberOfDependents = 2)
    {
        withholdRate =  .18;
    }
    else
    {
        withholdRate = .15;
    }

withholdingAmount = grossPay * withholdRate;

return (withholdingAmount);
}
= is assignment.
== is comparision.
Hi @KennySnook,
you are using the
assignment operator =
instead of the
relational operator ==
(Equal to)
Topic archived. No new replies allowed.