Void function not counting my values

I have a simple salary program here that calculates the Federal Withholding Tax and FICA tax. I've already tested this program in a regular while loop and it works without a problem, but in a value-passing function it seems that the program only wants to print out the net pay and not the deductions. I can't seem to understand what I'm missing here and any pointers would be appreciated.

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

using namespace std;

void calcFedTaxes (int &salary, double FWTrate, double FICArate, double FWT,   double FICA);
void calcNetPay (int &salary, double FWT, double FICA, int &netPay);
void displayInfo (double FWT, double FICA, int &netPay);

int main()
{
	int salary = 0;
	int netPay = 0;
	double FWTrate = 0.2; //20% weekly salary
	double FICArate = 0.08; //8% weekly salary
	double FWT = 0; 
        double FICA = 0;
		
	cout << "Enter salary: ";
	cin >> salary; 
	
	while (salary > 0)
	{
		calcFedTaxes(salary, FWTrate, FICArate, FWT, FICA);
		calcNetPay (salary, FWT, FICA, netPay);
		displayInfo (FWT, FICA, netPay);
		
		break;
	}
	
	
        return 0;
}

void calcFedTaxes (int salary, double FWTrate, double FICArate, double FWT,     double FICA)
{
	FWT = salary * FWTrate;
	FICA = salary * FICArate;
}

void calcNetPay (int salary, double FWT, double FICA, int &netPay)
{
       netPay = salary - FWT + FICA; 
}

void displayInfo (double FWT, double FICA, int &netPay)
{
	cout << "Federal Withholidng Tax: " << " $" << FWT << endl;
	cout << "Federal Insurance Contributions Act: " << " $" << FICA << endl;
	cout << "Net Pay: " << " $" << netPay << endl;
}
Last edited on
This shouldnt compile. Your function prototypes do not match your function definitions.

In the function prototype salary is passed by reference. While it is being passed by value in the function definition.

1
2
void calcFedTaxes (int &salary, double FWTrate, double FICArate, double FWT,   double FICA); // remove the &, not needed for salary
void calcNetPay (int &salary, double FWT, double FICA, int &netPay); // you can remove the &, not needed for salary 


However, your problem is, in the calcFedTaxes function, since you pass FWT and FICA as value, they are being copied, meaning whatever value they get (salary * FWTrate etc) doesn't carry over to the displayInfo, because those actions were assigned to the copies. So you want to pass FWT and FICA by reference to calcFedTaxes

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

  using namespace std;

  void calcFedTaxes (int salary, double FWTrate, double FICArate, double &FWT,   double& FICA); // pass FWT and FICA by reference
  void calcNetPay (int salary, double FWT, double FICA, int &netPay);
  void displayInfo (double FWT, double FICA, int &netPay);

  int main()
  {
	  int salary = 0;
	  int netPay = 0;
	  double FWTrate = 0.2; //20% weekly salary
	  double FICArate = 0.08; //8% weekly salary
	  double FWT = 0; 
	  double FICA = 0;
		
	  cout << "Enter salary: ";
	  cin >> salary; 
	
	  while (salary > 0)
	  {
		  calcFedTaxes(salary, FWTrate, FICArate, FWT, FICA);
		  calcNetPay (salary, FWT, FICA, netPay);
		  displayInfo (FWT, FICA, netPay);
		
		  break;
	  }
	
	
	  return 0;
  }

  void calcFedTaxes (int salary, double FWTrate, double FICArate, double& FWT,     double& FICA) // has to match the prototypes above main
  {
	  FWT = salary * FWTrate;
	  FICA = salary * FICArate;
  }

  void calcNetPay (int salary, double FWT, double FICA, int &netPay)
  {
	  netPay = salary - FWT + FICA; 
  }

  void displayInfo (double FWT, double FICA, int &netPay)
  {
	  cout << "Federal Withholidng Tax: " << " $" << FWT << endl;
	  cout << "Federal Insurance Contributions Act: " << " $" << FICA << endl;
	  cout << "Net Pay: " << " $" << netPay << endl;
  }
Last edited on
Thanks a bunch. Understanding the difference between void values and references has been a colossal headache for me and I'm sure a lot of other people have asked this same question. Looking at the program I understand why you'd need to add a reference because the values don't carry over to the total.

My only question then is why don't you need to do the same for calcNetPay? Does it automatically carry over?
You don't need it in calcNetPay since you're only using them to calculate. Whether it's the original or not doesnt matter, they will still carry the same value =)
Topic archived. No new replies allowed.