| | |
exiting loop with switch
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
[code=c++]
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 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.
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
while (choice != 'Q' || choice != 'q');
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.
Last edited by chizy2; Oct 30th, 2007 at 10:16 pm. Reason: add text
you need to use the && operator, not ||
C++ Syntax (Toggle Plain Text)
while (choice != 'Q' && choice != 'q');
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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.
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.
Last edited by WaltP; Oct 31st, 2007 at 12:17 am.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
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:
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?
c++ Syntax (Toggle Plain Text)
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?
Last edited by stymiee; Nov 1st, 2007 at 4:00 pm. Reason: fixed code tags
![]() |
Similar Threads
- Need help with do while loop (Java)
- Looking for a better switch without strings is there one? (C++)
- Infinite Loop in the switch statements (C++)
- Gliven, I need an idea for a program... help anyone? (Pascal and Delphi)
- No idea how to do this problem... (C++)
- program w/ switch AND nested if. six error messages (C++)
- Need some help with my do-while and while loops (Java)
- Getting words (PHP)
Other Threads in the C++ Forum
- Previous Thread: c++ struct
- Next Thread: Fibonacci number series
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






