• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

invalid lvalue in assignment for the while loop in my code

Fandango21

Junior Member
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);
                }
        }
}
 
When checking for equality you need to use the == operator, not =.

== is a boolean operator
= is an assignment operator
 
Your loop condition is kind of weird. For example, if the user enters an invalid command, your program will terminate.

I would do something like this:

Code:
boolean exit = false;

while (!exit) {
	input = getInput();
	
	switch (input) {
		case A: 
			A();
			break;
		case B: 
			B();
			break;
		case X: 
			exit = true;
			break;
		default: 
			print("invalid command");
			break;
	}
}
 
Wow. Thanks for catching that Crusty. Totally missed the second = there. Good point there too Leros. I was trying to make sure that it would terminate if they pressed anything other than available chars but having it run anyway then terminate would probably be better and less complicated. Thanks for the advice.
 
It's a good habit to put rvalues (i.e. expressions that cannot appear on the left side of an assignment) to the left side of a comparison. E.g.:
if ('B' == userInput) ...
if (NULL == pData)

This way if you accidentally put = instead of ==, it will be a compile-time error.
 
Wow. Thanks for catching that Crusty. Totally missed the second = there. Good point there too Leros. I was trying to make sure that it would terminate if they pressed anything other than available chars but having it run anyway then terminate would probably be better and less complicated. Thanks for the advice.

Usually if the user enters something unexpected, you want to display an error message or a help message. That is what the default part of the switch statement in my example does.
 
Back
Top