What am I doing wrong??? Function help

Pages: 12
I am writing a function calculateTaxes() that calculates and returns the amount of city income tax and the amount of federal income tax to be withheld from an employees's pay for one pay period. Assuming the city income tax is calculated by taking 1.15% of gross pay on the first $40,000 earned per year, and that federal income tax is calculated by taking the gross pay less $50 for each dependent claimed and multiplying by 20%.
Then I'm trying to use the function in a program that will read for either of several employees the employee number, number of dependents, hourly pay rate, city income tax withheld to date, federal income tax withheld to date, hours worked, and then calls calculateTaxes() to find the amount of taxes to be withheld. I want the program to display employee number, gross and net pay for this period, and total amounts withheld through this period.
It's running, but not doing the calculation...or maybe I'm not doing something right with calling for or sending info to/from the function??? It takes input up until it asks for grossPay- it says grossPay is being used without being initialized, but if I initialize it with = 0 then the output is always 0... Help please! 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
#include "stdafx.h"
#include <iostream>


using namespace std;

double calculateTaxes(double city,double federal);


int main()
{   
	double empNum,numDepends,hourlyPay,hoursWorkd,grossPay =0.0, netPay = 0.0, cityTaxes=0.0, fedTaxes=0.0;

	
	
	cout << "Enter your employee number. "<< endl;
	cin >> empNum;
	cout << "Enter the number of dependents. "<< endl;
	cin >> numDepends;
	cout << "Enter your hourly pay rate. "<< endl;
	cin >> hourlyPay;
	//cout << "Enter the city income tax witheald to date. "<< endl;
	//cin >> cityTaxes;
	//cout << "Enter the federal income tax withheld to date. "<< endl;
	//cin >> fedTaxes;
	cout << "Enter the hours worked this period. "<< endl;
	cin >> hoursWorkd;

	grossPay=hoursWorkd * hourlyPay;

	cout << "Employee Number:" <<empNum<< " Gross Pay: "<< grossPay<< " Net Pay: " << netPay<< "City and Federal income tax withheld:"<<calculateTaxes(cityTaxes,fedTaxes) <<endl;
	return 0;
}

double calculateTaxes(double city,double federal)


{ 
	double cityTaxes , fedTaxes, numDepends, totalWitheld;
	double       grossPay = 0;
	return cityTaxes = 1.15 * grossPay;
         fedTaxes = (grossPay - (numDepends * 50.00 )) * .20;
		 totalWitheld = cityTaxes + fedTaxes;

}
Last edited on
1. Why are you setting unused local variables in calculateTaxes? This makes no difference in the actual program, as the variables are discarded after the function is called.
2. You are returning early; No calculations are done after the return statement.

SOLUTIONS:
I think you want to set some values in your main program, so use pointers, or a struct:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//OUTSIDE A FUNCTION
struct Taxes {
    double cityTaxes , fedTaxes, numDepends, totalWitheld, grossPay=0;
}
//IN MAIN
    struct Taxes taxes;
    taxes.cityTaxes=taxes.fedTaxes=0; //You will want to change this later, probably
    /*SNIP*/
    cout << /*SNIP*/calculateTaxes(&taxes);
//Replace
double calculateTaxes(struct Taxes *taxes)
{
    taxes->grossPay=0;
    taxes->cityTaxes = 1.15 * taxes->grossPay;
    taxes->fedTaxes = (taxes->grossPay - (taxes->numDepends * 50.0)) * 0.2;
    taxes->totalWitheld = taxes->cityTaxes + taxes->fedTaxes;
    return taxes->cityTaxes;
}


Explanation:
struct Taxes: declares a structure called Taxes with the members above.
struct Taxes taxes: declares a variable of type struct Taxes.
&taxes: give a POINTER to taxes so the function can modify it.
struct Taxes *taxes: takes a POINTER to a struct Taxes object.
taxes->(something): accesses the member of taxes.

ANOTHER way is to use global variables, but this is widely discouraged.
NOTE: This is C style code. If you don't care about C, just use Taxes instead of struct Taxes (not in the declaration though).
And if you want to be OOP, use classes instead.
Last edited on
Ohhh! So u do the calculations BEFORE the return statement!
I would edit my post, but you might not read it.
You could also pass pointers instead of a struct, but I find that a struct is easier to read.
firefly, just a note, in C++ you do not specify struct Taxes *taxes, instead it's just Taxes *taxes because struct is actually the same as class but with public defaults instead of private defaults.
Ok, here is the revision...the function is only outputting 0..When I uninitialize it, the program won't run...I have also been trying to figure out how to get the function to return more than one result.

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
#include "stdafx.h"
#include <iostream>


using namespace std;

double calculateTaxes(double numDepends, double hourlyPay, double hoursWorkd,double grossPay);


int main()
{   
	double empNum,numDepends,hourlyPay,hoursWorkd,grossPay;

	
	
	cout << "Enter your employee number. "<< endl;
	cin >> empNum;
	cout << "Enter the number of dependents. "<< endl;
	cin >> numDepends;
	cout << "Enter your hourly pay rate. "<< endl;
	cin >> hourlyPay;
	//cout << "Enter the city income tax witheald to date. "<< endl;
	//cin >> cityTaxes;
	//cout << "Enter the federal income tax withheld to date. "<< endl;
	//cin >> fedTaxes;
	cout << "Enter the hours worked this period. "<< endl;
	cin >> hoursWorkd;

	grossPay=hoursWorkd * hourlyPay;

	cout << "Employee Number:  " <<empNum<< " Gross Pay:  "<< grossPay << "  City and Federal income tax withheld:  "<<calculateTaxes(numDepends,hourlyPay, hoursWorkd, grossPay) <<endl;
	return 0;
}

double calculateTaxes(double numDepends, double hourlyPay, double hoursWorkd,double grossPay )


{ 
	double cityTaxes , fedTaxes, numDepends1=0, totalWitheld;
	double      grossPay1=0,netPay;
	     cityTaxes = 1.15 * grossPay1;
         fedTaxes = (grossPay1 - (numDepends1 * 50.00 )) * .20;
		 totalWitheld = cityTaxes + fedTaxes;
		 netPay = grossPay1-totalWitheld;
		 return cityTaxes, fedTaxes, totalWitheld, netPay;
}
return cityTaxes, fedTaxes, totalWitheld, netPay;
In C++ when you are not using commas for separating parameters, they have a different meaning: they evaluate each expression, and only result in the last right-most expression. Thus, you are only returning netPay.

If you want to return more than one value or 'give back' more than one value, use a data structure such as a struct, or use references as parameters.
Thank you for clearing that up for me LB! :-)
1
2
3
4
5
6
7
8
9
10
11
12
13
double calculateTaxes(double numDepends, double hourlyPay, double hoursWorkd,double grossPay )
//^^^You should use the variable names listed up here and not create numDepends1,grossPay1
// Also since hourlyPay and hoursWorkd are not used in this function you don't need them

{ 
	double cityTaxes , fedTaxes, numDepends1=0, totalWitheld; /// numDepends1 = 0
	double      grossPay1=0,netPay; /// grossPay1 = 0
	     cityTaxes = 1.15 * grossPay1; ///  = 1.15*0 = 0  // BTW 1.15% equals 0.0115
         fedTaxes = (grossPay1 - (numDepends1 * 50.00 )) * .20; /// = (0 - (0*50.00))*.20 = 0
		 totalWitheld = cityTaxes + fedTaxes; ///  = 0+0 = 0
		 netPay = grossPay1-totalWitheld; /// = 0-0 = 0
		 return cityTaxes, fedTaxes, totalWitheld, netPay; /// returns netPay = 0 //employer loves you
}


Instead of returning many results at once you could write a separate function returning each of the amounts.
Last edited on
LOL..I know right Vin! Thank you! This is what I have so far and I believe it's pretty much done minus desk checking and aesthetics...

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
#include "stdafx.h"
#include <iostream>


using namespace std;

double calculateTaxes(double numDepends, double hourlyPay, double hoursWorkd,double grossPay);


int main()
{   
	double empNum,numDepends,hourlyPay=0,hoursWorkd =0,grossPay= hoursWorkd * hourlyPay;

	
	
	cout << "Enter your employee number. "<< endl;
	cin >> empNum;
	cout << "Enter the number of dependents. "<< endl;
	cin >> numDepends;
	cout << "Enter your hourly pay rate. "<< endl;
	cin >> hourlyPay;
	//cout << "Enter the city income tax witheald to date. "<< endl;
	//cin >> cityTaxes;
	//cout << "Enter the federal income tax withheld to date. "<< endl;
	//cin >> fedTaxes;
	cout << "Enter the hours worked this period. "<< endl;
	cin >> hoursWorkd;

	

	cout << "  **Employee Number:  " <<empNum<< endl;
	cout <<endl;
	cout << "The output is: " <<calculateTaxes( numDepends, hourlyPay, hoursWorkd, grossPay );
	//cout << "  **Gross Pay: $  "<< grossPay <<endl;
	//cout <<endl;
	//calculateTaxes (numDepends, hourlyPay, hoursWorkd, grossPay);
	//cout<<endl;

	return 0;
}

double calculateTaxes(double numDepends, double hourlyPay, double hoursWorkd,double grossPay )


{ 
	double cityTaxes , fedTaxes, totalWitheld, netPay;
	
         grossPay = hoursWorkd * hourlyPay * 12 ;
		 if (grossPay <= 40000)
         cityTaxes = .0115 * grossPay;
		 else cityTaxes = 40000 * .0115;

         fedTaxes = (grossPay - (numDepends * 50.00 )) * .20;

		 totalWitheld = cityTaxes + fedTaxes;

		 netPay = grossPay-totalWitheld;

		 cout << "  **Gross Pay: $  "<< grossPay <<endl;
		 cout << "  **City taxes withheld: $ " << cityTaxes <<endl; 
		 cout<< endl;
         cout << "  **Federal taxes witheld: $ " << fedTaxes<< endl;
		 cout<<endl;
		 cout << "  **Total withheld: $ " << totalWitheld<<endl;
		 cout<<endl;
		 cout << "  **Net pay: $ " << netPay<< endl;


		 //return 0;
		 return cityTaxes, fedTaxes, totalWitheld, netPay;
}
I cant undestand that code. I cant understand Frame... >.< This forum should clear things up!!
If you don't need to return a value from calculateTaxes() declare it to return void and not double. But if it returns void you can't pass it to cout so I changed the lines which display the output too.

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
#include "stdafx.h"
#include <iostream>


using namespace std;

void calculateTaxes(double numDepends, double hourlyPay, double hoursWorkd,double grossPay);


int main()
{   
	double empNum,numDepends,hourlyPay=0,hoursWorkd =0,grossPay= hoursWorkd * hourlyPay;

	
	
	cout << "Enter your employee number. "<< endl;
	cin >> empNum;
	cout << "Enter the number of dependents. "<< endl;
	cin >> numDepends;
	cout << "Enter your hourly pay rate. "<< endl;
	cin >> hourlyPay;
	//cout << "Enter the city income tax witheald to date. "<< endl;
	//cin >> cityTaxes;
	//cout << "Enter the federal income tax withheld to date. "<< endl;
	//cin >> fedTaxes;
	cout << "Enter the hours worked this period. "<< endl;
	cin >> hoursWorkd;

	

	cout << "  **Employee Number:  " <<empNum<< endl;
	cout <<endl;
	///cout << "The output is: " <<calculateTaxes( numDepends, hourlyPay, hoursWorkd, grossPay );
	//cout << "  **Gross Pay: $  "<< grossPay <<endl;
	//cout <<endl;
	calculateTaxes (numDepends, hourlyPay, hoursWorkd, grossPay);
	//cout<<endl;

	return 0;
}
void calculateTaxes(double numDepends, double hourlyPay, double hoursWorkd,double grossPay )


{ 
	double cityTaxes , fedTaxes, totalWitheld, netPay;
	
         grossPay = hoursWorkd * hourlyPay * 12 ;
		 if (grossPay <= 40000)
         cityTaxes = .0115 * grossPay;
		 else cityTaxes = 40000 * .0115;

         fedTaxes = (grossPay - (numDepends * 50.00 )) * .20;

		 totalWitheld = cityTaxes + fedTaxes;

		 netPay = grossPay-totalWitheld;

		 cout << "  **Gross Pay: $  "<< grossPay <<endl;
		 cout << "  **City taxes withheld: $ " << cityTaxes <<endl; 
		 cout<< endl;
         cout << "  **Federal taxes witheld: $ " << fedTaxes<< endl;
		 cout<<endl;
		 cout << "  **Total withheld: $ " << totalWitheld<<endl;
		 cout<<endl;
		 cout << "  **Net pay: $ " << netPay<< endl;


		 //return 0;
		 ///return cityTaxes, fedTaxes, totalWitheld, netPay;
}
Last edited on
best way to change the value of multiple variable inside the function is by pointer .

void calculateTaxes(double *numDepends, double *hourlyPay, double *hoursWorkd,double *grossPay )

Cheers.
Thank you Vin!
@bluecoder it is better to use references in that case.
1
2
3
4
5
6
7
8
9
10
11
12
13
struct w2Tax
{
   double dependents;
   double hourlyPay;
   double hoursWorked;
   double grossPay;
}

void calculateTaxes(w2Tax &tax)
{
   //...
}


Disregard namings used lol
Last edited on
Why make it more annoying to program?
Man, reading this thread makes ME confused, and I know by heart how to do this!

First things first, don't ever use a struct if you're going to use methods on the objects. Use a class. Second, you don't need a struct OR a class in this case, since it's just a simple evaluation.

Now I saw two different highly useable suggestions posted, so here I'm going to clear it up a little bit. First suggestion was to write multiple functions to perform the tasks necessary.
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
double calcGross(double hourlyPay, double hoursWorkd)
{
    return hoursWorkd * hourlyPay * 12;
}

double calcCityTax(double grossPay, double& netPay)
{
    double cityTax;

    if(grossPay <= 40000.0) cityTax = 0.0115 * grossPay;
    else cityTax = 0.0115 * 40000;
    netPay -= cityTax;
    return cityTax;
}

double calcFedTax(double grossPay, int numDepends, double& netPay)
{
    double fedTax;
    fedTax = (grossPay - (numDepends * 50.00)) * 0.2;
    netPay -= fedTax;
    return fedTax;
}

int main()
{
    int numDepends;
    double empNum, hourlyPay, hoursWorkd, grossPay, netPay;
    
    cout << "Enter your employee number. "<< endl;
    cin >> empNum;
    cout << "Enter the number of dependents. "<< endl;
    cin >> numDepends;
    cout << "Enter your hourly pay rate. "<< endl;
    cin >> hourlyPay;
    cout << "Enter the hours worked this period. "<< endl;
    cin >> hoursWorkd;

    cout << "  **Employee Number:  " << empNum << endl;
    cout << endl;
    grossPay = calcGross(hourlyPay, hoursWorkd);
    netPay = grossPay;
    cout << "Gross Pay: $" << grossPay << endl;
    cout << "City Taxes: $" << calcCityTax(grossPay, netPay) << endl;
    cout << "Federal Taxes: $" << calcFedTax(grossPay, numDepends, netPay) << endl;
    cout << "Net Pay: $" << netPay << endl;
}


The second suggestion was just to pass everything by reference:
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
void calculateTaxes(int numDepends, double hourlyPay, double hoursWorkd, _
        double& grossPay, double& cityTax, double& fedTax, double& netPay)
{
    grossPay = hoursWorkd * hourlyPay * 12;
    if (grossPay <= 40000) cityTax = grossPay * 0.0115;
    else cityTax = 40000 * 0.0115;
    fedTax = (grossPay - (numDepends * 50)) * 0.2;
    netPay = grossPay - (fedTax + cityTax);
}
int main()
{
    int numDepends;
    double empNum, hourlyPay, hoursWorkd, grossPay, netPay, cityTax, fedTax;
    
    cout << "Enter your employee number. "<< endl;
    cin >> empNum;
    cout << "Enter the number of dependents. "<< endl;
    cin >> numDepends;
    cout << "Enter your hourly pay rate. "<< endl;
    cin >> hourlyPay;
    cout << "Enter the hours worked this period. "<< endl;
    cin >> hoursWorkd;

    cout << "  **Employee Number:  " << empNum << endl;
    cout << endl;
    calculateTaxes(numDepends, hourlyPay, hoursWorkd, grossPay, cityTax, fedTax, netPay);
    cout << "Gross Pay: $" << grossPay << endl;
    cout << "City Taxes: $" << cityTax << endl;
    cout << "Federal Taxes: $" << fedTax << endl;
    cout << "Net Pay: $" << netPay << endl;
}


As you can see once you test it, you'll be able to handle your functions quite readily. I highly recommend that you use the first one, because it produces cleaner, more readable code. Also, if you need to change something, you can change only one function without risking it having a major effect on the others.
First things first, don't ever use a struct if you're going to use methods on the objects. Use a class. Second, you don't need a struct OR a class in this case, since it's just a simple evaluation.
The purpose for the struct was value type semantics, nothing else.
First things first, don't ever use a struct if you're going to use methods on the objects. Use a class.


They're identical but for default privacy (and inheritance thereof).
Pages: 12