Searched, couldn't find it.. I'm looking to simplify this:
if ((menuSelection != 'A') || (menuSelection != 'B') || (menuSelection != 'C') || (menuSelection != 'D') || (menuSelection != 'X'))
Can anyone help?
Thanks in advance..
Andrew
Printable View
Searched, couldn't find it.. I'm looking to simplify this:
if ((menuSelection != 'A') || (menuSelection != 'B') || (menuSelection != 'C') || (menuSelection != 'D') || (menuSelection != 'X'))
Can anyone help?
Thanks in advance..
Andrew
If it's continuous:
if (menuSelection >= 'A' && menuSelection <= 'X')
it's not :D but thanks. Anyone else?
oops.. sorry folks, it should be:
if ((menuSelection != 'A') && (menuSelection != 'B') && (menuSelection != 'C') && (menuSelection != 'D') && (menuSelection != 'X'))
you could put it into a small function:
this kind of function is good if you are using the same menu more than once and need to frequently validate the inputCode:bool checkInput( const char& input )
{
//perform if statement to make sure a valid character is entered.
return true;
}
Code:
bool
is_true(char ch) {
switch(tolower(ch)) {
case 'a': case 'x': case 'b' : case 't':
return true;
}
return false;
}
Thanks Guys...
Here's a doozy of a compound boolean statement (its movement checking for a knight in my chess game):
Code:if((x2 == (x1 - 2) && y2 == (y1 -1)) || (x2 == (x1 - 2) && y2 == (y1 + 1)) ||
(x2 == (x1 -1) && y2 == (y1-2)) || (x2 == (x1 - 1) && y2 == (y1 + 2)) ||
(x2 == (x1 + 1) && y2 == (y1 -2)) || (x2 == (x1 + 1) && y2 == (y1 + 2)) ||
(x2 == (x1 + 2) && y2 == (y1 - 1)) || (x2 == (x1 + 2) && y2 == (y1 + 1)))
.... perhaps you should implement it differently then :)
hey, for my simple chess program it works. What would you suggest? :)Quote:
Originally posted by BMJ
.... perhaps you should implement it differently then :)
use a case statement, have switches for 'A' 'B' 'C' 'D' and 'X', and have the code under the
if ((menuSelection != 'A') || (menuSelection != 'B') || (menuSelection != 'C') || (menuSelection != 'D') || (menuSelection != 'X'))
be exectued in the default: thingy instead
Thats diferent than a compound if statement, though. What you've just proposed is a replacement for multiple if statements.Quote:
Originally posted by moi
use a case statement, have switches for 'A' 'B' 'C' 'D' and 'X', and have the code under the
if ((menuSelection != 'A') || (menuSelection != 'B') || (menuSelection != 'C') || (menuSelection != 'D') || (menuSelection != 'X'))
be exectued in the default: thingy instead
i made the assumption (wrongly i guess) that he would want to be processing entry for the valid keys at the same time that he elimiates bad entriesQuote:
Originally posted by Captain Penguin
Thats diferent than a compound if statement, though. What you've just proposed is a replacement for multiple if statements.
Hey whaddya know, that IS what he was doing. I sort of ignored his main post and concentrated on the phrase of the subject, "Testing multiple conditions". :)Quote:
Originally posted by moi
i made the assumption (wrongly i guess) that he would want to be processing entry for the valid keys at the same time that he elimiates bad entries
Arrays come to mind...