I was wondering if there was a shorter way to write
if (test != 'e' || test != 'd') I want to write it like
if (test != ('e' || 'd')) Or something like this so i don't have to repeat "test !="
Thanks
That's the syntax of the language. There's not much you can do about it... If you don't like the way it looks, you can make a boolean function that contains the tests and then just call that function:
bool isEOrD(char test) { return (test != 'e' || test != 'd') } ... if (isEOrD(test)) EDIT: There are other ways to write this code (see the comments to this answer), but your original way is probably the cleanest approach.
You could use the old C function strchr:
if (!strchr("de", test)) { // test is not 'd' or 'e' } But I don't know whether it is any nicer to look at… personally I would probably just have the two !=.
C or C++ must evaluate the expressions you write in the syntax of the language. The expression ('e' or 'd') always returns true because the 'or-ing' is done by comparing the bits of the values which will never be the same. How is the compiler to know what you want since in C/C++ a raw character is simply an interpretation of an integer. That's why you can legally write:
char aChar = 'a'; // or char aChar = 0x41; // hex 41 = ascii 'a' and have them both work.
if (true). You might want to take a look at DeMorgan's Laws though. In case you're wondering, if test isd, it won't bee, and if it'se, it won't bed. Therefore your condition will always be true. You probably want&&, not||.