Thread: invalid lvalue in assignment for the while loop in my code

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    The reason you are getting the error is because || has a higher precedence than =. The following expression:

    Code:
    userInput = 'B' || userInput = 'D' || userInput = 'Q'
    is equivalent to:

    Code:
    userInput = ('B' || userInput) = ('D' || userInput) = 'Q'
    And because the = operator has right-to-left associativity, the compiler complains that:

    Code:
    ('D' || userInput)
    which results in a boolean value, is an invalid lvalue. (It's like trying to say true = 'X'.)


    Of course, what you probably meant was to use the == operator, which produces the much more reasonable expression:

    Code:
    userInput == 'B' || userInput == 'D' || userInput == 'Q'
    The following lines should also be producing errors though:

    Code:
            char 'B';
            char 'C';
            char 'Q';
            char 'X';
    Not sure what you had in mind when you wrote that.
    Last edited by Mozza314; 04-02-2011 at 10:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help on this school assignment on for loop
    By spyropyro in forum C Programming
    Replies: 1
    Last Post: 12-01-2010, 02:07 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 09:16 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM