Need Help!

I am just starting classes on c++ in college, and I'm having trouble with this "undefined reference to 'get_withholding_function'. I will put 3 stars on the line that is not working.

BTW - I'm using code::blocks

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

using namespace std;

double get_withholding_function (int dependents, double grossPay);

int main()
{
   int hoursWorked;
   int overtimeHoursReg;
   int overtimeHoursDouble;
   int dependents;
   double hourlyRate, basePay, overtimePay, grossPay, overtimeDouble, withHolding, netPay;


   cout<< "Income Calculator" << endl;
   cout<< "------------------" << endl;

   cout<< "Enter the hourly rate of pay: $";
   cin>> hourlyRate;
   cout<< "Enter the number of hours worked";
   cout<< " rounded to a whole number of hours:";
   cin>> hoursWorked;


     overtimePay = 0;
     overtimeHoursReg = 0;
     overtimeHoursDouble = 0;
     dependents = 0;


   if (hoursWorked > 50)
{
         overtimeHoursReg = 10;
         overtimeHoursDouble = (hoursWorked - 50);
         basePay = hourlyRate * 40;
         overtimePay = hourlyRate * overtimeHoursReg * 1.5;
         overtimeDouble = hourlyRate * (hoursWorked - 50) * 2;
         grossPay = basePay + overtimePay + overtimeDouble;
}
     else if (hoursWorked > 40 && hoursWorked < 51)
    {
         overtimeHoursReg = (hoursWorked - 40);
         basePay = hourlyRate * 40;
         overtimePay = hourlyRate * (hoursWorked - 40) * 1.5;
         grossPay = basePay + overtimePay;
    }
      else
    {
          basePay = hourlyRate * hoursWorked;
          grossPay = basePay + overtimePay;
    }

*** withHolding = get_withholding_function (dependents, grossPay);

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

   cout<< "Hours worked = " <<hoursWorked << endl;
   cout<< "Hourly rate = $" << hourlyRate << endl;
   cout<< "Base pay = $" << basePay << endl;
   cout<< "Overtime Hours at time and a half = " << overtimeHoursReg << endl;
   cout<< "Overtime Pay at time and a half = $" << overtimePay << endl;
   cout<< "Overtime Hours at double = " << overtimeHoursDouble << endl;
   cout<< "Overtime Pay at double time = $" << overtimeDouble << endl;
   cout<< "Gross pay = $" << grossPay << endl;
   cout<< "Tax Withholding Amount = $" << withHolding << endl;
   cout<< "Net Pay = $" << netPay << endl;

   return 0;
}

double get_withholding_function (int dependents, double grossPay, double netPay)
{
    double withHolding;


          if (dependents == 0)
       {
        withHolding = grossPay * 0.28;
        netPay = grossPay - withHolding;
       }
       else if (dependents == 1)
       {
       withHolding = grossPay * 0.20;
       netPay = grossPay - withHolding;
       }
       else if (dependents == 2)
      {
        withHolding = grossPay * 0.18;
        netPay = grossPay - withHolding;
      }
       else if (dependents > 2)
       {
        withHolding = grossPay * 0.15;
        netPay = grossPay - withHolding;
       }
return 0;
}
Last edited on
You have two parameters in your forward declaration of the get_withholding_function but three in the actual definition.
A couple of things:
On Line 5, your function prototype says double get_withholding_function (int dependents, double grossPay);. It has two arguments.

However, when you call the function on line 74, you have three arguments:
 
double get_withholding_function (int dependents, double grossPay, double netPay)


Then, on line 54, you are trying to call the function with two arguments:
withHolding = get_withholding_function (dependents, grossPay); .

I see that you are calculating the netPay; however, you are only returning the value 0 back to the variable witHolding. regardless of the value of netPay. The reason I know this is because you have a return 0 (lin99).

On a final note, I see on line 69, you display the netPay; however, the variable netPay is not initialized (only declared). I hope this helps.
okay, I fixed what you were talking about, but when I run the program I am getting "$nan" for tax withholding amount. sorry I am very new to this :P

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

using namespace std;

double get_withholding_function (int dependents, double grossPay, double netPay);

int main()
{
   int hoursWorked;
   int overtimeHoursReg;
   int overtimeHoursDouble;
   int dependents;
   double hourlyRate, basePay, overtimePay, grossPay, overtimeDouble, withHolding, netPay;


   cout<< "Income Calculator" << endl;
   cout<< "------------------" << endl;

   cout<< "Enter the hourly rate of pay: $";
   cin>> hourlyRate;
   cout<< "Enter the number of hours worked";
   cout<< " rounded to a whole number of hours:";
   cin>> hoursWorked;
   cout<< "Enter household dependents:";
   cin>> dependents;


     overtimePay = 0;
     overtimeHoursReg = 0;
     overtimeHoursDouble = 0;
     netPay = 0;
     dependents = 0;


   if (hoursWorked > 50)
{
         overtimeHoursReg = 10;
         overtimeHoursDouble = (hoursWorked - 50);
         basePay = hourlyRate * 40;
         overtimePay = hourlyRate * overtimeHoursReg * 1.5;
         overtimeDouble = hourlyRate * (hoursWorked - 50) * 2;
         grossPay = basePay + overtimePay + overtimeDouble;
}
     else if (hoursWorked > 40 && hoursWorked < 51)
    {
         overtimeHoursReg = (hoursWorked - 40);
         basePay = hourlyRate * 40;
         overtimePay = hourlyRate * (hoursWorked - 40) * 1.5;
         grossPay = basePay + overtimePay;
    }
      else
    {
          basePay = hourlyRate * hoursWorked;
          grossPay = basePay + overtimePay;
    }

withHolding = get_withholding_function (dependents, grossPay, netPay);

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

   cout<< "Hours worked = " <<hoursWorked << endl;
   cout<< "Hourly rate = $" << hourlyRate << endl;
   cout<< "Base pay = $" << basePay << endl;
   cout<< "Overtime Hours at time and a half = " << overtimeHoursReg << endl;
   cout<< "Overtime Pay at time and a half = $" << overtimePay << endl;
   cout<< "Overtime Hours at double = " << overtimeHoursDouble << endl;
   cout<< "Overtime Pay at double time = $" << overtimeDouble << endl;
   cout<< "Gross pay = $" << grossPay << endl;
   cout<< "Tax Withholding Amount = $" << withHolding << endl;
   cout<< "Net Pay = $" << netPay << endl;

   return 0;
}

double get_withholding_function (int dependents, double grossPay, double netPay)
{
    double withHolding;


          if (dependents == 0)
       {
        withHolding = grossPay * 0.28;
        netPay = grossPay - withHolding;
       }
       else if (dependents == 1)
       {
       withHolding = grossPay * 0.20;
       netPay = grossPay - withHolding;
       }
       else if (dependents == 2)
      {
        withHolding = grossPay * 0.18;
        netPay = grossPay - withHolding;
      }
       else if (dependents > 2)
       {
        withHolding = grossPay * 0.15;
        netPay = grossPay - withHolding;
       }
}
The variable overtimeDouble is not initiated. If you don't initialize it, then it would cause a run-time error.

The function double get_withholding_function (int dependents, double grossPay, double netPay) must have a return.

Two possible outcomes,

1. if you return withHolding , then it will send the withHolding value to line 57 and line 71.

2. if you return netPay, then it will send the netPay value to line 57 and line 71. Since the variable name is withHolding, you may not want to return netPay.

That being said, you must find a way to obtain netPay on line 72. If you don't, then the netPay will be zero. The zero is from the netPay initialization on line 31.

You are doing the calculations on netPay on the function double get_withholding_function (int dependents, double grossPay, double netPay), but it will not go anywhere, unless you return the netPay or pass the variable netPay by reference. There is another way to do it, but I will let you work it out.

I can't find out how to fix it. my head is about to explode!
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
double get_withholding_function(int dependents, double grossPay, double netPay)
{
	double withHolding;


	if (dependents == 0)
	{
		withHolding = grossPay * 0.28;
		netPay = grossPay - withHolding;
	}
	else if (dependents == 1)
	{
		withHolding = grossPay * 0.20;
		netPay = grossPay - withHolding;
	}
	else if (dependents == 2)
	{
		withHolding = grossPay * 0.18;
		netPay = grossPay - withHolding;
	}
	else if (dependents > 2)
	{
		withHolding = grossPay * 0.15;
		netPay = grossPay - withHolding;
	}

	return withHolding;
}


You move the netPay calculation to main() function:

1
2
3
   cout<< "Tax Withholding Amount = $" << withHolding << endl;
    netPay = grossPay - withHolding;
   cout<< "Net Pay = $" << netPay << endl;


This should give you an idea. I hope this helps.
Last edited on
ah! got it! thanks a lot! my head is hurting pretty bad, I would have never thought of that....
No worries. I know what you mean.
Topic archived. No new replies allowed.