Program isn't printing correctly

后端 未结 5 1366
醉酒成梦
醉酒成梦 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: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'.

提交回复
热议问题