Why doesn't this if statement work (C)?

Associate
Joined
8 Nov 2005
Posts
56
Code:
if (inputAnswer != 'A' || inputAnswer != 'B' || inputAnswer != 'C' || inputAnswer != 'D') { printf("error"); }

Even if 'inputAnswer' equals A, B, C or D it still displays 'error', i can't figure out why it's doing this. If i take out the ||(or) and just have the one inputAnswer !='A' then it works, but not when i add more than one.

Anyone able to shed some light on this please?

Thank you for reading! :)
 
Think about what the statement is saying.

A simplified version is:
Code:
if (inputAnswer != 'A' || inputAnswer != 'B') { do something }
If inputAnswer is not 'A' OR inputAnswer is not 'B', then do something. Is one of those statements still true if inputAnswer is 'A'?


Mick.
 
*edit* dammit, didnt read the last paragraph above :)

I'm not a C programmer, but shouldn't it be...
Code:
if (inputAnswer != 'A' && inputAnswer != 'B' && inputAnswer != 'C' && inputAnswer != 'D') { printf("error"); }
As if inputAnswer is 'A', the != 'B'/'C'/'D' conditions are true, hence you get an error?
 
Thank you both for your help. Changed them to 'ands' and it does the job i want it to do. :)

Edit: I should have known this, but it seemed i was getting myself confused
 
Back
Top Bottom