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;
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.
#include <iostream>
usingnamespace 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