need HELP correcting this program

I am trying to write an if else program that gives the tax and child credit based on three options: if your income is less than 30,000, (no tax, child credit 1000*num of children) , greater than 100,000 (20% tax, no child credit), and between 30,000 and 100,000 (10% tax, 500*num of children). I get the idea of it but I need help correcting/placing things in the right order. Also, I have trouble choosing the correct formula. This is what I have so far. Any help will be greatly appreciated.

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

using namespace std;
float i, t, cc, c, d;

int main()
{
	cout << "please enter your income" << endl;
	cin >> i;
	cout << "how many children" << endl;
	cin >> c;

if (i<=30000)
t=0.0;
if (c>0)
cc=1000*c;

else if ((i>30000)&&(i<=100000))
	t=(i-30000)*0.1;
if (c>0)
cc=c*500;

else
 	t=(i-100000)*0.2;
cout << "tax equals  " << t << endl;
cout << "child credit equals  " << cc << endl;
return 0;
}
Last edited on
Please use code tags.
you also need to document your code so that we can see what these values stand for.
float i, t, cc, c, d;
Edited with code tags.
An if affects either one statement or statements within braces {}. You don't have any braces.

One can have braces around the single statement too. I'll add them to your code (and touch indentation):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  if (i<=30000) {
    t=0.0;
  }

  if (c>0) {
    cc=1000*c;
  }
  else if ((i>30000)&&(i<=100000)) {
    t=(i-30000)*0.1;
  }

  if (c>0) {
    cc=c*500;
  }
  else {
    t=(i-100000)*0.2;
  }

  cout << "tax equals  " << t << endl;
  cout << "child credit equals  " << cc << endl;
  return 0;
}

Does anything in that look suspicious?


Note, all your variables are float. What does 3.142 kids look like?


Why are all your variables global? They don't need to be.
When I run the code, it picks up the income on the first (t=0.0) and on the second statement (cc=c*500) no matter the input value. Any tips on how I can rework that to make it choose which statement fits the income?
You did not try with negative children and your uninitialized t happens to be 0.

Case one: small income and some kids. (Line numbers from my code version.)
* Line 1 if is true. t is set 0.
* Line 5 if is true (has kids). Line 8 is skipped, for line 5 was true.
* Line 12 if is true. cc is set (and overwrites line 6). Line 16 is skipped, for line 12 was true.

Case two: large income and some kids.
* Line 1 is false.
* Line 5 if is true (has kids). Line 8 is skipped, for line 5 was true.
* Line 12 if is true. cc is set (and overwrites line 6). Line 16 is skipped, for line 12 was true.
* t remains whatever uninitialized value it had.

Braces, properly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int c = 0; // accept only whole kids
std::cout >> c;
if ( c < 0 ) c = 0; // sanity check

if ( i <= 30000 ) {
  // set t and cc
}
else if ( i <= 100000 ) {
  // i cannot be less than 30k, because previous if was false
  // set t and cc
}
else {
  // set t and cc
}
// show values 
Topic archived. No new replies allowed.