Need help with tax program

I have to define a c++ function to calculate the marginal tax rate on a given amount of income. We're given the function double federalTax(double income) and this must be used in the program. The little bit of code I have written returns 0 when it should return the correct tax amount each time I run it, and I'm not sure why. The code is here:

#include <iostream>
using namespace std;

double federalTax(double income);

double salary;

double tax;

int main()
{
cout << "Enter your income here: " << endl;
cin >> salary;

cout << tax << endl;

}

double federalTax(double income)
{

if(salary > 0 & salary < 46605)
{
tax=salary*.15;
}
}
Put a breakpoint before cout<<tax and see watch tax in a watchboard if it has been changed.
tax is an uninitialized variable. What value do you think will be output by your cout statement?

federalTax() says it returns a double. Where is your return statement?

Where is your call of federalTax() ?


PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Last edited on
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
#include <iostream>

using namespace std;


double federalTax(double);


int main()
{
	double salary;
	double tax;
	
	cout << "Enter your income here: ";
	cin >> salary;

	tax = federalTax(salary);

	cout << "\n Your tax is: " << tax;

	return (0);
}

double federalTax(double salary)
{
	if(salary > 0 && salary < 46605)
	{
		return (salary * .15);
	}
	else
		cout << "\n Error, income is either too low or too high...";
	
}


_You should avoid global variables, and even more non const ones, just put them in your main.
_your main is missing a return statement.
_in your if statement, insode your federalTax() function, to use the logical operator "and", it's a double a double &&, not just one.

The rest has already been pointed out by AbstractionAnon
Last edited on
Topic archived. No new replies allowed.