Testing Multiple Conditions

This is a discussion on Testing Multiple Conditions within the C++ Programming forums, part of the General Programming Boards category; Searched, couldn't find it.. I'm looking to simplify this: if ((menuSelection != 'A') || (menuSelection != 'B') || (menuSelection != ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    12

    Testing Multiple Conditions

    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

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    If it's continuous:
    if (menuSelection >= 'A' && menuSelection <= 'X')
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    12
    it's not but thanks. Anyone else?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    12
    oops.. sorry folks, it should be:

    if ((menuSelection != 'A') && (menuSelection != 'B') && (menuSelection != 'C') && (menuSelection != 'D') && (menuSelection != 'X'))

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    you could put it into a small function:
    Code:
    bool checkInput( const char& input )
    {
        //perform if statement to make sure a valid character is entered.
        return true;
    }
    this kind of function is good if you are using the same menu more than once and need to frequently validate the input
    Couldn't think of anything interesting, cool or funny - sorry.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Code:
    bool 
     is_true(char ch) {
      
      switch(tolower(ch)) {
    
       case 'a': case 'x': case 'b' : case 't':
       return true;
    
      }
    
      return false;
     }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    12
    Thanks Guys...

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    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)))

  9. #9
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    .... perhaps you should implement it differently then

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by BMJ
    .... perhaps you should implement it differently then
    hey, for my simple chess program it works. What would you suggest?

  11. #11
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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
    hello, internet!

  12. #12
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    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
    Thats diferent than a compound if statement, though. What you've just proposed is a replacement for multiple if statements.

  13. #13
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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.
    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
    hello, internet!

  14. #14
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    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
    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".

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Arrays come to mind...
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple conditions - if statement
    By dibble in forum C Programming
    Replies: 8
    Last Post: 03-28-2009, 12:41 PM
  2. Replies: 1
    Last Post: 09-22-2008, 01:38 PM
  3. checking inputs meets multiple conditions
    By 60beetle60 in forum C Programming
    Replies: 5
    Last Post: 04-19-2008, 08:25 AM
  4. Multiple conditions for the while loop?
    By Olidivera in forum C++ Programming
    Replies: 6
    Last Post: 04-24-2005, 03:47 AM
  5. tips for testing conditions
    By Silvercord in forum Game Programming
    Replies: 12
    Last Post: 04-10-2003, 01:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21