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.
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;
}
elseif (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;
}
elseif (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:
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.