invalid lvalue in assignment for the while loop in my code

Fandango21

Junior Member
Feb 8, 2011
23
0
0
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);
                }
        }
}
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
When checking for equality you need to use the == operator, not =.

== is a boolean operator
= is an assignment operator
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
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;
	}
}
 

Fandango21

Junior Member
Feb 8, 2011
23
0
0
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.
 

iCyborg

Golden Member
Aug 8, 2008
1,329
53
91
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.
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
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.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |