Void function problems

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


//functions
double getSalary();
void calcFedTaxes(double salary, int FWT, int FICA, double &totNetPay); 
void calcNetPay(double netPay, int FWT, int FICA, double &totNetPay);
void displayInfo(int FWT, int FICA, double &totNetPay);
void totNetPay(double &totNetPay, double netPay);
int main()
{
	//declare variables
	double salary		= 0.0;
	const double FWT	= 0.2;
	const double FICA	= 0.08;
	double totNetPay	= 0.0;
	double netPay		= 0.0;

	salary = getSalary();

	while (salary > 0)
	{
		calcFedTaxes(salary, FWT, FICA, totNetPay);
		calcNetPay(netPay, FWT, FICA, totNetPay);
		displayInfo(FWT, FICA, totNetPay);
		salary = getSalary();
	}//end while

	cout << "Total net pay is: " << totNetPay << endl;

	system("pause");
	return 0;
}

//defining functions

double getSalary()
{
	double salaryAmt = 0.0;
	cout << "Enter salary amount: (-1 to stop) ";
	cin >> salaryAmt;
	return salaryAmt;
}

void calcFedTaxes(double salary, int FWT, int FICA, double &totNetPay)
{
	FWT = salary * .2;
	FICA = salary *.08;
}// end of calcFedTaxes

void calcNetPay(double netPay, int FWT, int FICA, double &totNetPay)
{
	totNetPay = netPay + FWT + FICA;
}// end of CalcNetPay

void displayInfo(int FWT, int FICA, double &totNetPay)
{
	cout << FWT << endl;
	cout << FICA << endl;
	cout << totNetPay << endl;
}

void totNetPay(double &totNetPay, double netPay)
{
	totNetPay += netPay;
}


The problem I am having is the calculations. When I run the program, doesn't do any of the calculations
At the first view, it should work properly. Try to put a part of the code on comment and see if work all the functions one by one. You will find the mistake more fast.
I figured it out, or maybe a way for it to work. I changed this:
1
2
3
4
5
{
	cout << FWT << endl;
	cout << FICA << endl;
	cout << totNetPay << endl;
}


to this:

1
2
3
4
5
6
7
8
{
	FWT = salary *.2;
	cout << "FWT is: " << FWT << endl;
	FICA = salary * .08;
	cout << "FICA is: " << FICA << endl;
	totNetPay = salary - FWT - FICA;
	cout << "Net pay is: " << totNetPay << endl;
}



After this change it worked perfect!
Advice: don't use system("pause") any more. It slows your application and can make some damage at the computer's intern structure.
What difference does it make if system("pause") is slow? It's not slow enough to be noticeable. And what is the intern structure that is damaged? I don't understand.
I don't know what CosminNTG is talking about, but there are some good reasons to not use system(...); in production code. One simple example would be that system("pause"); only works in windows, and there are non-OS specific ways to do the same thing. Read: http://cplusplus.com/forum/beginner/1988

Also note that chances are if your writing code that uses the standard streams (cout, cin, and cerr) and not a text based UI, you don't want to pause the program before exit anyway. These programs may (and arguable should) be run directly from the command prompt or a batch file/shell script, in which case you don't want to hard code a statement like that in the executable.
Last edited on
1
2
3
4
5
void calcFedTaxes(double salary, int FWT, int FICA, double &totNetPay)
{
	FWT = salary * .2;
	FICA = salary *.08;
}// end of calcFedTaxes 


Pass the variables to be changed, by reference. So they are changed in the function that called it.

1
2
3
4
5
void calcFedTaxes(double salary, int &FWT, int &FICA, double &totNetPay)
{
	FWT = salary * .2;
	FICA = salary *.08;
}// end of calcFedTaxes 


EDIT:
Also, You're doing FWT = salary * .2;. And you're saving to an int. You wont have the correct number.
Last edited on
Topic archived. No new replies allowed.