case 'q' :
        case 'Q' : 
            cout << "Computing your totals" << endl;
            break;
        default : cout << "Invalid choice!";
    }
}
/* This is where it should break out of but it is not */

// while (choice != 'Q' || choice != 'q'); - this is what I was using
while ( choice != 'Q' ) 

cout << "\n\nCUSTOMER BILL\n";
cout << "-------------\n";
cout << "Name:\t" << name << endl;
cout << "Phone:\t" << phone << endl;
cout << "Email:\t" << email << endl << endl;

I have a menu that is using switch, when you enter q or Q you need to exit the loop (do while) and then start printing the info. I can not get the program to exit. I was using the while (choice != 'Q' || choice != 'q'); at first, but it will never exit. How do I exit this using the while ( xxxxx ) code? Any hints, I need to use both cases of Q to test.

Thanks,
Greg

edit: let me know if you need to see the entire code, this is for a project at school, and I do have all the code except for this working.

Recommended Answers

All 7 Replies

you need to use the && operator, not ||

while (choice != 'Q' && choice != 'q');

Rule of thumb -- if you need both, use && (and). If you need either, use || (or).

Test it by using English (or your native language): is it "if != Q or q" or is it "if != Q and q" and you get the operator to use.

it should be if the user hits either the Q or q then exit the loop., but when I was using the or, it would not exit the loop.

it should be if the user hits either the Q or q then exit the loop., but when I was using the or, it would not exit the loop.

Yes, but this statement is "if == " not "if !=" which is what you were using in the code.

Ok, still can not get it to work right, if I change the code to == , then it exits the loop after one selection, and does not wait for the customer to press q. Here is the code for the loop:

do
	{
	// Display menu
		cout << "\n\n**********PRODUCTS MENU**********" << endl;
		cout << "Items\tProducts\t\tUnitPrice($)" << endl;
		cout << "-----\t--------\t\t------------" << endl;
		cout << left << setw(2) << "A)\t" << setw (15) << "FlashDrive" << right << setw(9) << "19.99" << endl;
		cout << left << setw(2) << "B)\t" << setw (15) << "IPOD" << right << setw(9) << "300.00" << endl;
		cout << left << setw(2) << "C)\t" << setw (15) << "Computer" << right << setw(9) << "1499.99" << endl;
		cout << left << setw(2) << "D)\t" << setw (15) << "DVDPlayer" << right << setw(9) << "300.00" << endl;
		cout << left << setw(2) << "E)\t" << setw (15) << "TV" << right << setw(9) << "999.00" << endl;
		cout << left << setw(2) << "F)\t" << setw (15) << "RemoteControl" << right << setw(9) << "9.99" << endl;
		cout << left << setw(2) << "Q)\t" << setw (15) << "QUIT" << endl;
		cout << "Enter your choice?:";
		cin >> choice;
		
		switch (choice)
		{
			case 'a' :
			case 'A' : cout << "Enter quantity to be purchased: ";
					   cin >> flashQuantity;
					
					while ( flashQuantity < 0)
					{
						cout << "You must enter a positive number\n";
						cout << "Enter quantity to be purchased: ";
						cin >> flashQuantity;
					}
				break;
			case 'b' :
			case 'B' : cout << "Enter quantity to be purchased: ";
					   cin >> ipodQuantity;
					
					while ( ipodQuantity < 0)
					{
						cout << "You must enter a positive number\n";
						cout << "Enter quantity to be purchased: ";
						cin >> ipodQuantity;
					}
				break;
			case 'c' :
			case 'C' : cout << "Enter quantity to be purchased: ";
					   cin >> computerQuantity;
					
					while ( computerQuantity < 0)
					{
						cout << "You must enter a positive number\n";
						cout << "Enter quantity to be purchased: ";
						cin >> computerQuantity;
					}
				break;
			case 'd' :
			case 'D' : cout << "Enter quantity to be purchased: ";
					   cin >> dvdQuantity;
					
					while ( dvdQuantity < 0)
					{
						cout << "You must enter a positive number\n";
						cout << "Enter quantity to be purchased: ";
						cin >> dvdQuantity;
					}
				break;
			case 'e' :
			case 'E' : cout << "Enter quantity to be purchased: ";
					   cin >> tvQuantity;
					
					while ( tvQuantity < 0)
					{
						cout << "You must enter a positive number\n";
						cout << "Enter quantity to be purchased: ";
						cin >> tvQuantity;
					}
				break;
			case 'f' :
			case 'F' : cout << "Enter quantity to be purchased: ";
					   cin >> remoteQuantity;
					
					while ( remoteQuantity < 0)
					{
						cout << "You must enter a positive number\n";
						cout << "Enter quantity to be purchased: ";
						cin >> remoteQuantity;
					}
				break;
			case 'q' :
			case 'Q' : 
				cout << "Computing your totals" << endl;
				break;
			default : cout << "Invalid choice!";
		}
	}
	while (choice == 'Q' || choice == 'q');

I was using ( choice != 'Q' || choice != 'q' ) before, and it would loop ok, but would not exit the loop when I pressed q or Q. any hints please?

Doesn't Ancient Dragon's answer in post #2 of this thread do the trick?

don't i feel like a dumb bunny, I did the == && ==, not the != && !=.

i works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.