Program isn't printing correctly

后端 未结 5 1286
醉酒成梦
醉酒成梦 2021-01-28 04:52

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

相关标签:
5条回答
  • 2021-01-28 05:26

    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;

    0 讨论(0)
  • 2021-01-28 05:28

    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.

    0 讨论(0)
  • 2021-01-28 05:45
    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'.

    0 讨论(0)
  • 2021-01-28 05:47

    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 讨论(0)
  • 2021-01-28 05:52

    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;
        } 
    
    0 讨论(0)
提交回复
热议问题