Calling Values from a Function to Another

Hi, I'm having trouble passing a value from one function to another.
However, I am supposed to pass the calculated grossSalary from the function getGrossSalary over to getNetSalary.

I've tried to use static variables and change the return values unsuccessfully. Appreciate the time and help.

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

using namespace std;

double getGrossSalary(double hoursWorked, double payRate);
double getNetSalary(double grossSalary, double taxRate);

int main()
{
	double hoursWorked = 0.00;
	double payRate = 0.00;
	double grossSalary = 0.00;
	double taxRate = 0.00;
	double netSalary = grossSalary;

	cout << "Enter hours worked: ";
	cin >> hoursWorked;
	cout << "Enter hourly pay: ";
	cin >> payRate;
	cout << "Tax Rate: " << "% ";
	cin >> taxRate;

	cout << setprecision(2) << fixed;
	cout << "Gross Salary: $ " << getGrossSalary(hoursWorked, payRate) << endl;
	cout << "Tax Rate: " << taxRate << " %" << endl;
	cout << "Net Salary: $ " << /*getGrossSalary(hoursWorked, payRate) - getGrossSalary(hoursWorked, payRate) * (taxRate / 100) */ netSalary<< endl; //This mess in the /**/ is a current work around but not allowed.
	return 0;
}

double getGrossSalary(double hoursWorked, double payRate)
{
	double grossSalary = 0.00;
	if (hoursWorked <= 40)
	{
		double grossSalary = hoursWorked * payRate;
		return grossSalary;
	}

	else if (hoursWorked > 40)
	{
		double grossSalary = (((hoursWorked - 40) * (1.5 * payRate) + (40 * payRate)));
		return grossSalary;
	}
	return grossSalary;
}

double getNetSalary(double grossSalary, double taxRate)// Area I need help with 
{
	double netSalary = (taxRate * grossSalary) - (grossSalary);
	return netSalary;
}
1
2
3
4
5
6
double getNetSalary(double hoursWorked, double payRate, double taxRate)
{
	double grossS = getGrossSalary(hoursWorked, payRate);
	double netSalary = (taxRate * grossS) - (grossS);
	return netSalary;
}


I would do it like this, you're calling your function getGrossSalary from getNetSalary.

Otherwise you could declare a new double variable in your main function and save your first calcution to it like double grossS = getGrossSalary(hoursWorked, payRate);. This variable can be given to your next function.
The main point, no matter where you call the function, is that you store the value returned by a function into a variable that you can use (multiple times):
1
2
3
4
5
snafu = getGrossSalary(hoursWorked, payRate);
// use snafu:
std::cout << snafu;
auto fubar = getNetSalary( snafu, tax );
std::cout << fubar;

Ahhh, I see I fixed the code and it's now working perfectly.
I really appreciate the help, thank you so much guys!
Topic archived. No new replies allowed.