messing around with if statements, a couple questions

C++ is my first language and I'm just experimenting here with if statements. I'm trying to write a program that calculates taxes based on the first income tax law in the united states where your tax up by 1% every bracket.

Couple of questions here:

1) While running my program I get this error message after compilation when I run the program and enter 500000 as the income.
"Run-Time Check Failure #3 - The variable 'totaltax' is being used without being initialized."

Then when I move the totaltax variable as global variable and enter my income as 500000 it now goes to 0.

2) Is there an easier way of doing this? I feel only that I found a really dumb way of doing this.


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

using namespace std;

int main()
{
	double income;
	double totaltax;
	double rate1 = 0.01;
	double rate2 = 0.02;
	double rate3 = 0.03;
	double rate4 = 0.04;
	double rate5 = 0.05;
	double rate6 = 0.06;
	double limit1 = 50000;
	double limit2 = 75000;
	double limit3 = 100000;
	double limit4 = 250000;
	double limit5 = 500000;

	cout << "Enter income: ";
	cin >> income;
	cout << endl;

	if (income <= 50000.00)
	{
		totaltax = income * rate1;
	}
	else if (income > 50000 && income <= 75000)
	{
		totaltax = ((income - 50000) * rate2) + 500;
	}
	else if (income > 75000 && income <= 100000)
	{
		totaltax = ((income - 75000) * rate3) + 1000;
	}
	else if (income > 100000 && income <= 250000)
	{
		totaltax = ((income - 100000) * rate4) + 1750;
	}
	else if (income < 250000 && income <= 500000)
	{
		totaltax = ((income - 250000) * rate5) + 7750;
	}
//	else if (income < 500000)
	//	totaltax = ((income - 500000) * rate6) +
	cout << "Taxes owed: " << totaltax << endl;
	system("PAUSE");
	return 0;
}
On line 41, you have the comparison operator the wrong way round. Also, without getting too complicated, I can't really think of any better way of doing this.
The if statements are unnecessarily testing the same value twice. For example here,
25
26
27
28
29
    if (income <= 50000.00)
    {
        totaltax = income * rate1;
    }
    else if (income > 50000 && income <= 75000)

when we get to line 29, it isn't necessary to check income > 50000 as that condition has already been taken care of by the previous if.
25
26
27
28
29
    if (income <= 50000.00)
    {
        totaltax = income * rate1;
    }
    else if (income <= 75000)

Having redundant code like this not only makes the code longer, but introduces scope for errors should there be a need to change a figure and the person making the change might not notice that it needs to be done multiple times.

Also, you have a lot of unused variables linit1, limit2 etc. Either use these in the code or delete them. (or use a comment to document the limits ).

Values which you don't want to change should be declared as constant, for example:
 
    const double rate1 = 0.01;
Regarding the error message "Run-Time Check Failure #3 - The variable 'totaltax' is being used without being initialized."

That highlights a serious issue. it isn't so much that totaltax has not been initialised - though that is true, it was not given any initial value. But the real problem is that there is no final else clause to catch the case where none of the previous if conditions were satisfied.
Topic archived. No new replies allowed.