Problem with output

Hi, i am new to programming and i have a problem with my programe

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class TaxReturn
{
private:
int taxId;
string name;
double income;
int dependents;
double taxAmt;
void computeTax();

public:
TaxReturn (int = 0, string = "" , double = 0.0, int = 0);
void setAll (int, string, double, int);
void display();
};

TaxReturn :: TaxReturn (int TAXID, string NAME, double INCOME, int DEPENDENTS)
{
taxId = TAXID;
name = NAME;
income = INCOME;
dependents = DEPENDENTS;
}

void TaxReturn :: setAll (int TAXID, string NAME, double INCOME, int DEPENDENTS)
{
taxId = TAXID;
name = NAME;
income = INCOME;
dependents = DEPENDENTS;
computeTax();
}

void TaxReturn :: display()
{
cout << "\nID #" << taxId << "\t" << name << "," << endl;
cout << "      " << dependents << " dependents" << endl;
cout << "      " << "Income $"  << income << "\tTax Owned $"  << taxAmt << endl;
}

void TaxReturn :: computeTax()
{
	if (income > 60000.00)
	{
		if (dependents > 4)
		{
			taxAmt = 0.20 * income;
		}
		else
		{
			taxAmt = 0.30 * income;
		}
	}
	else if (income >= 40000 && income <=  60000)
	{
		if (dependents > 4)
		{
			taxAmt = 0.10 * income;
		}
		else
		{
			taxAmt = 0.20 * income;
		}

	}
	else if (income >= 20000 && income < 40000)
	{
		if (dependents > 4)
		{
			taxAmt = 0.05 * income;
		}
		else
		{
			taxAmt = 0.10 * income;
		}
	}
	else if (income < 20000) ;
	{
		taxAmt = 0;
	}
}

int main()
{
TaxReturn a, b, c;
a.setAll (1, "John Smith", 60000.00, 5);
b.setAll (2, "Tan Ah Kow", 19000.00, 2);
c.setAll (3, "Sing Sum San", 40500.00, 1);

cout << "\nSummary" << endl; 
	a.display();
	b.display();
	c.display();
	cin.ignore();

return 0;
}


Some how, the output never calculates the taxAmt and always shows 0.
Can anyone help me point out what i did wrong?
closed account (o3hC5Di1)
Hi there,

You have a semicolon that shouldn't be there on line 83:

else if (income < 20000) ; <--

What happens is that the else if statement is ended, and what's next between the curly braces is always executed, independent of the condition. Blocks like those are called anonymous code blocks and are sometimes used to limit the scope (and lifetime) of a variable:

1
2
3
4
5
{
    int b;
}

//int b doesn't exist here anymore. 


All the best,
NwN
Also, it's probably a good idea to set taxAmt to zero in the constructor.
1
2
3
4
5
6
7
8
TaxReturn :: TaxReturn (int TAXID, string NAME, double INCOME, int DEPENDENTS)
{
    taxId      = TAXID;
    name       = NAME;
    income     = INCOME;
    dependents = DEPENDENTS;
    taxAmt     = 0;
}

The function computeTax() does not need to check for the same value twice, in both the if and else conditions. Doing so is unnecessary duplication and is inviting the two values to become out of sync if you later need to change the code but forget to change it in both places.

1
2
3
4
5
6
7
8
9
    if (income > 60000.00)
    {

    }
    // Below, We already know income must be <= 60000
    else if (income >= 40000 && income <=  60000) 
    {

    }


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
void TaxReturn :: computeTax()
{
    if (income > 60000.00)
    {
        if (dependents > 4)
            taxAmt = 0.20 * income;
        else
            taxAmt = 0.30 * income;
    }
    else if (income >= 40000)
    {
        if (dependents > 4)
            taxAmt = 0.10 * income;
        else
            taxAmt = 0.20 * income;
    }
    else if (income >= 20000)
    {
        if (dependents > 4)
            taxAmt = 0.05 * income;
        else
            taxAmt = 0.10 * income;
    }
    else 
    {
        taxAmt = 0;
    }
}
OMG! thanks guys... i was scratching my head for the past few hrs about this >.< Thanks NwN and Chervil!
Topic archived. No new replies allowed.