invalid lvalue in assignment for the while loop in my code

This is a discussion on invalid lvalue in assignment for the while loop in my code within the C++ Programming forums, part of the General Programming Boards category; Hello. I need some help with a program I'm writing. It needs to define a function named menu that returns ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    13

    invalid lvalue in assignment for the while loop in my code

    Hello. I need some help with a program I'm writing. It needs to define a function named menu that returns a char and does not accept parameters. Any help is appreciated.

    Currently I have this written in a while loop so that it keeps asking the user to input a char until they input X and then it will stop the loop. I haven't written all the code inside of the case structure thats in the while loop yet just because I didn't want to get to far before I knew if what I'm trying to do would work. What I would like to do is set it up so that the while loop runs only for B, D, Q, and X chars and anything else will break the loop. I'm pretty sure the way I set the while statement here is the problem but I don't know how to fix it. Code so far is

    Code:
    char menu (){
            char 'B';
            char 'C';
            char 'Q';
            char 'X';
    
            char userInput;
    
            cout << "(B)est Price\n(D)iscount\n(Q)uantity\ne(X)it" << endl;
            cout << endl;
            cout << "Please enter the option (B, D, Q, or X):  ";
            cin >> userInput;
            cout << endl;
    
            //Set while statement to run program while user doesn't enter X
    
            while(userInput != 'X' && (userInput = 'B' || userInput = 'D' || userInput = 'Q'))      {
    
                    switch(userInput) {
    
                            case 'B':
                                    float price1,price2,price3;
    
                                    cout << "Please enter 3 prices:  ";
                                    cin >> price1 >> price2 >> price3;
                                    bestPrice (price1, price2, price3);
                    }
            }
    }

  2. #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 11: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, 03:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07: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, 10:16 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

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