Tax bracket function

So I am trying to write a program where the user inputs their income then the program tells them which bracket they are in. I want a function that computes the tax bracket and then I want main to prompt the user for their income and tell them which tax bracket they are in.

I Thought I was on the right track with this but it won't compile and I am not sure exactly what I am doing wrong. I am just now becoming familiar with bool statements so I haven't quite got the hang of them yet.

Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool computeTax()
{
   int income;
   
   if income > 0 || < 1510
      return 10%;
}

int main()
{
   int income;
   
   cout << "Income: ";
   cin  >> income;
   
   computeTax();

   return 0;
}
Why are you using bool datatype? Are trying to achieve something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int computeTax(int a) {
	if (a > 0 || a < 1510)
		return 10;
	//else if {.... other tax bracket.
}

int main() {
	int income;
	
	cout << "Income: ";
	cin  >> income;
	
	cout << computeTax(income) << "%";
	
	return 0;
}
Last edited on
yea sorry I have about 5 other brackets that I need to put in there. So when the user enters their income it will assign them a tax bracket.

So I would be wanting to issue an else statement for the other tax brackets I think.
Yeah, that's correct.
1
2
3
4
5
6
7
int computeTax(int a) {
	if (a > 0 || a < 1510) {
		return 10;
	} else if(condition...)
		return 15;
	//etc...
}
How would I enter that there is no limit to how large the number is to fall in a 35% tax bracket? Would I just leave out the a < no limit?

also does the rest of it look right?
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
int computeTax(int a)
{
   if (a > 0 || a < 15100)
      return 10;
   else if (a > 15100 || a < 61300)
      return 15;
   else if (a > 61300 || a < 123700)
      return 25;
   else if (a > 123700 || a < 188450)
      return 28;
   else if (a > 188450 || a < 336550)
      return 33;
   else if (a > 336550 || a < no limit)
      return 35;
}

int main()
{
   int income;

   cout << "Income: ";
   cin  >> income;

   cout << "Your tax bracket is " << computeTax(income) << "%";

   return 0;
}
How would I enter that there is no limit to how large the number is to fall in a 35% tax bracket? Would I just leave out the a < no limit?

Yes, you can omit it. Also, the way you have the conditions written if a = 15100 nothing would be returned. So in one of your statements you will want to have a >= 15100 or a <= 15100 depending on how the ranges work out. The same goes for 61300, 123700, etc.
ok so I've got this, but now when I enter the income as say 100000 it still tells me that I am in the 10% bracket when I should be in the 25%.

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
int computeTax(int a)
{
   if (a > 0 || a <= 15100)
      return 10;
   else if (a > 15100 || a <= 61300)
      return 15;
   else if (a > 61300 || a <= 123700)
      return 25;
   else if (a > 123700 || a <= 188450)
      return 28;
   else if (a > 188450 || a <= 336550)
      return 33;
   else if (a > 336550)
      return 35;
}

int main()
{
   int income;

   cout << "Income: ";
   cin  >> income;

   cout << "Your tax bracket is " << computeTax(income) << "%";

   return 0;
}
That's because your a > 0 conditional is always true.
You'll need something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int computeTax(int a) {
		if(a > 336550)
			return 35;
		else if(a < 336550 && a > 188450)
			return 33;
		else if(a < 188450 && a > 123700)
			return 28;
		else if(a < 123700 && a > 61300)
			return 25;
		else if(a < 61300 && a > 15100)
			return 15; 
		else
			return 10;
	}
Last edited on
oh ok that did the trick and makes a lot of sense. Would I be able to do it the reverse order that you have and have it work out the same way or is the order set? Thanks to all who helped out as always I have learned a lot from this.
It should work in either order.
Yeah, you just need to change your comparison operator from || to &&
1
2
3
4
5
int computeTax(int a) {
   if (a > 0 && a <= 15100)
      return 10;
   // ect...
}
Topic archived. No new replies allowed.