Thread: c++ if statement using !=

  1. #1
    Unregistered
    Guest

    c++ if statement using !=

    HI,

    I am trying to use not equal (!=) with or (||) in an if statement.

    cout << "enter operator: +, -, /, *:";
    cin >> oper

    If (oper != '=' || '-' || '*' || '\')
    cout << "Please choose +, -, /, * ";

    This will not work. Is there a way to us "andor"?

    Thanks Paul

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    if ((oper != '=') || (oper != '-') || (oper != '*') || (oper != '/'))

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    if ( oper != '+' || oper != '-' || oper != '*' || oper != '\' )

    but that's kind of ugly for such a simple test, perhaps a few else ifs would suit your purpose better.
    Code:
    if ( oper == '+' )
      /* Calculate for '+' */
    else if ( oper == '-' )
      /* Calculate for '-' */
    else if ( oper == '*' )
      /* Calculate for  '*' */
    else if ( oper == '/' )
      /* Calculate for  '/' */
    else
      cout<<"Please choose +, -, / or * "<<endl;
    -Prelude
    My best code is written with the delete key.

  4. #4
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    >>PROGRAM: A magic spell cast over a computer allowing it to turn one's input into error messages.

    Funny! LOL
    Blue

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    if ((oper<42&&oper>47)||oper!=44||oper!=46) ....
    a needlessly complicated way to do it
    Last edited by ygfperson; 02-13-2002 at 06:34 PM.

  6. #6
    Unregistered
    Guest
    How about using a switch, it it not 'elegant' programing?

    switch( oper )
    {
    case '+':
    //stuff
    break;
    case '-':
    //more stuff
    break;

    etc
    etc

    default: //covers all other possibilities
    break;
    }

  7. #7
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    Switch is ugly and annoying.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM