开发者

Program isn't printing correctly

开发者 https://www.devze.com 2023-02-27 23:55 出处:网络
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 with the id. Buts its pr

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 with the id. Buts its printing the valid statement above the the id and still displaying the taxableIncome and taxRate and TaxAmount. while(fscanf(tax,"\n%d %c %d %d ",&taxpayerId, &fillingStatus, &grossIncome, &taxExemptions) != EOF) {

                    fillingStatus = toupper(fillingStatus);

                    if ( fillingStatus == 'S')
                    {
                        taxableIncome = grossIncome - 3000 - 1000 * taxExemptions;

                    }
                    else if ( fillingStatus == 'M')
                    {
                        taxableIncome = grossIncome - 3000 - 1000 * taxExemptions;

                    }   
                    else if ( fillingStatus == 'J')
                    {
                        taxableIncome = grossIncome - 6000 - 1000 * taxExemptions;

                    }

                    if (taxableIncome < 0)
                    {
                        taxableIncome = 0;
                    }


                    if ( fillingStatus == 'S')
                    {
                            if (taxableIncome < 5000)
                            {
                                taxRate = 0.15;
                            }
                            if (taxableIncome  > 20000)
                            {
                                taxRate = 0.31;
                            }
                            else
                            {
                                taxRate = 0.22;
                            }
                    }     
                    else if ( fillingStatus == 'M')
                    {
                            if (taxableIncome < 10000)
                            {
                                taxRate = 0.15;

                            }   
                            if (taxableIncome > 40000)
                            {
                                taxRate = 0.31;
                            }
                            else
                            {
                                taxRate = 0.22;
                            }
                    }
                    else if ( fillingStatus == 'J')
                    {
                            if (taxableIncome < 7000)
                            {
                                taxRate = 0.17;
                            }
                            if (taxableIncome > 25000)
                            {    
                                taxRate = 0.33;
                            }
                            else
                            {
                                taxRate = 0.24;
                            } 
                    }
                    else
                    {   
                            printf("\n %d     **** Invalid filling status ****",taxpayerId);
                            continue;
                    }

                    if (taxExemptions > 12  || taxExemptions < 0) 
                    {
                        printf("\n %d     **** Invalid number of exemptions ****", taxpayerId);
                    } 
                    else 
                    {
                        taxAmount = taxableIncome * taxRate;
                        printf("\n %d %15.2f  %15.2f  %18.2f",taxpayerId,taxableIncome,taxRate,taxAmount);
                    }  

Output is suppose to look like this:

Taxpayer ID    Taxable Income    Tax Rate        Tax Amount
-----------    --------------    --------        ----------

111111111           4000.00        0.15            600.00
222222222          36500.00        0.22           8030.00
333333333          19152.00        0.24           4596.48
444444444    **** Invalid filing status ****
555555555          67000.00        0.31          20770.00
666666666          53197.00        0.33          17555.01
777777777          19000.00        0.22           4180.00
888888888    **** Invalid number of exemptions ****
999999999          46308.00        0.33          15281.64
101010101          91602.00        0.31          28396.62
121212121           9525.00        0.15           1428.75
131313131              0.00        0.17              0.00

My out put look like this:

Taxpayer ID Taxable Income

Tax Rate Tax Amount ----------- -------------- -------- ----------

111111111           4000.00        0.22            880.00
222222222          36500.00        0.31          11315.00
333333333          19152.00        0.24           4596.48
444444444          **** Invalid filing status ****           
555555555          67000.00        0.31          20770.00
666666666          53197.00        0.24          12767.28
777777777          **** Invalid number of exemptions ****        
888888888           3543.00        0.22
999999999          46308.00        0.24            779.46
101开发者_StackOverflow社区010101          91602.00        0.22          11113.92
121212121           9525.00        0.31           2952.75
131313131              0.00        0.24              0.00


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'.


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.


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;
    } 


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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消