I believe my program is still going through the if statements after it is declared invalid. It should print invalid filling status or invalid exemption status on the same line w
You can't use commas inside numbers in C++. The comma is the comma operator.
So instead of 7,000 write 7000.
if(taxable_income>7,000)
{
x;
}
Would never execute x;
You might consider doing the following after the scanf:
fillingStatus = toupper(fillingStatus);
Then only test for the uppercase characters:
fillingStatus == 'S'
You need to fix your 'ifs' as suggested and print output like the following. It won't be quite right until you've fixed the 'ifs', though.
if (taxExemptions > 12 || taxExemptions < 0) {
printf("\n %d \t**** Invalid exemptions status ****", taxpayerId);
} else {
taxAmount = taxableIncome * taxRate;
printf("\n %d \t %.2f \t\t %.2f \t %15.2f",taxpayerId,taxableIncome,taxRate,taxAmount);
}
The more I look at this code, the more I'm confused too. What should the tax rate be for someone with taxableIncome of 1 and fillingStatus == 'S'. I think it's going to be 0.31, since it's less than 20000. Probably not what you're going for. I think you're missing some 'else if' statments.
fillingStatus == 's' || 'S'
The above condition does not do what you appear to think it does. Try:
fillingStatus == 's' || fillingStatus == 'S'
The reason is that the ==
operator only compares one pair of values at a time. So your expression reads something like:
if (fillingStatus is 's') or ('S' is nonzero)
which is always true (since 'S'
is always nonzero). The corrected expression reads like:
if (fillingStatus is 's') or (fillingStatus is 'S')
It might be worth noting that some other languages have less verbose ways of expressing this condition than the above C++ code. For example, in Python you can write:
if fillingStatus in ('s', 'S'):
which tests fillingStatus
for either 's'
or 'S'
.
Edit:
based on the updated question, it is clear that the problem lies not with the horrendous if
statements, but with the input data - you are picking the wrong record as having invalid number of exemptions. This means two things:
you should have posted this earlier and saved people a lot of time looking in the wrong place
you need to show the data that you are consuming if you want an opinion on why you are flagging the wrong record.
I've deleted my original answer. Next time please post ALL the relevant details.
change this
else
{
printf("\n**** Invalid filling status ****");
}
if (taxExemptions > 12 || taxExemptions < 0)
{
printf("\n**** Invalid exemptions status ****");
}
to
else
{
printf("\n**** Invalid filling status ****");
continue;
}
if (taxExemptions > 12 || taxExemptions < 0)
{
printf("\n**** Invalid exemptions status ****");
continue;
}