exiting loop with switch

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2005
Posts: 61
Reputation: chizy2 is an unknown quantity at this point 
Solved Threads: 3
chizy2's Avatar
chizy2 chizy2 is offline Offline
Junior Poster in Training

exiting loop with switch

 
0
  #1
Oct 30th, 2007
[code=c++]
  1. case 'q' :
  2. case 'Q' :
  3. cout << "Computing your totals" << endl;
  4. break;
  5. default : cout << "Invalid choice!";
  6. }
  7. }
  8. /* This is where it should break out of but it is not */
  9.  
  10. // while (choice != 'Q' || choice != 'q'); - this is what I was using
  11. while ( choice != 'Q' )
  12.  
  13. cout << "\n\nCUSTOMER BILL\n";
  14. cout << "-------------\n";
  15. cout << "Name:\t" << name << endl;
  16. cout << "Phone:\t" << phone << endl;
  17. 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
  1. 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.
Last edited by chizy2; Oct 30th, 2007 at 10:16 pm. Reason: add text
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: exiting loop with switch

 
0
  #2
Oct 30th, 2007
you need to use the && operator, not ||
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: exiting loop with switch

 
0
  #3
Oct 31st, 2007
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.
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 61
Reputation: chizy2 is an unknown quantity at this point 
Solved Threads: 3
chizy2's Avatar
chizy2 chizy2 is offline Offline
Junior Poster in Training

Re: exiting loop with switch

 
0
  #4
Oct 31st, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: exiting loop with switch

 
0
  #5
Oct 31st, 2007
Originally Posted by chizy2 View Post
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 61
Reputation: chizy2 is an unknown quantity at this point 
Solved Threads: 3
chizy2's Avatar
chizy2 chizy2 is offline Offline
Junior Poster in Training

Re: exiting loop with switch

 
0
  #6
Nov 1st, 2007
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:

  1. do
  2. {
  3. // Display menu
  4. cout << "\n\n**********PRODUCTS MENU**********" << endl;
  5. cout << "Items\tProducts\t\tUnitPrice($)" << endl;
  6. cout << "-----\t--------\t\t------------" << endl;
  7. cout << left << setw(2) << "A)\t" << setw (15) << "FlashDrive" << right << setw(9) << "19.99" << endl;
  8. cout << left << setw(2) << "B)\t" << setw (15) << "IPOD" << right << setw(9) << "300.00" << endl;
  9. cout << left << setw(2) << "C)\t" << setw (15) << "Computer" << right << setw(9) << "1499.99" << endl;
  10. cout << left << setw(2) << "D)\t" << setw (15) << "DVDPlayer" << right << setw(9) << "300.00" << endl;
  11. cout << left << setw(2) << "E)\t" << setw (15) << "TV" << right << setw(9) << "999.00" << endl;
  12. cout << left << setw(2) << "F)\t" << setw (15) << "RemoteControl" << right << setw(9) << "9.99" << endl;
  13. cout << left << setw(2) << "Q)\t" << setw (15) << "QUIT" << endl;
  14. cout << "Enter your choice?:";
  15. cin >> choice;
  16.  
  17. switch (choice)
  18. {
  19. case 'a' :
  20. case 'A' : cout << "Enter quantity to be purchased: ";
  21. cin >> flashQuantity;
  22.  
  23. while ( flashQuantity < 0)
  24. {
  25. cout << "You must enter a positive number\n";
  26. cout << "Enter quantity to be purchased: ";
  27. cin >> flashQuantity;
  28. }
  29. break;
  30. case 'b' :
  31. case 'B' : cout << "Enter quantity to be purchased: ";
  32. cin >> ipodQuantity;
  33.  
  34. while ( ipodQuantity < 0)
  35. {
  36. cout << "You must enter a positive number\n";
  37. cout << "Enter quantity to be purchased: ";
  38. cin >> ipodQuantity;
  39. }
  40. break;
  41. case 'c' :
  42. case 'C' : cout << "Enter quantity to be purchased: ";
  43. cin >> computerQuantity;
  44.  
  45. while ( computerQuantity < 0)
  46. {
  47. cout << "You must enter a positive number\n";
  48. cout << "Enter quantity to be purchased: ";
  49. cin >> computerQuantity;
  50. }
  51. break;
  52. case 'd' :
  53. case 'D' : cout << "Enter quantity to be purchased: ";
  54. cin >> dvdQuantity;
  55.  
  56. while ( dvdQuantity < 0)
  57. {
  58. cout << "You must enter a positive number\n";
  59. cout << "Enter quantity to be purchased: ";
  60. cin >> dvdQuantity;
  61. }
  62. break;
  63. case 'e' :
  64. case 'E' : cout << "Enter quantity to be purchased: ";
  65. cin >> tvQuantity;
  66.  
  67. while ( tvQuantity < 0)
  68. {
  69. cout << "You must enter a positive number\n";
  70. cout << "Enter quantity to be purchased: ";
  71. cin >> tvQuantity;
  72. }
  73. break;
  74. case 'f' :
  75. case 'F' : cout << "Enter quantity to be purchased: ";
  76. cin >> remoteQuantity;
  77.  
  78. while ( remoteQuantity < 0)
  79. {
  80. cout << "You must enter a positive number\n";
  81. cout << "Enter quantity to be purchased: ";
  82. cin >> remoteQuantity;
  83. }
  84. break;
  85. case 'q' :
  86. case 'Q' :
  87. cout << "Computing your totals" << endl;
  88. break;
  89. default : cout << "Invalid choice!";
  90. }
  91. }
  92. 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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,675
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: exiting loop with switch

 
0
  #7
Nov 1st, 2007
Doesn't Ancient Dragon's answer in post #2 of this thread do the trick?
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 61
Reputation: chizy2 is an unknown quantity at this point 
Solved Threads: 3
chizy2's Avatar
chizy2 chizy2 is offline Offline
Junior Poster in Training

Re: exiting loop with switch

 
0
  #8
Nov 1st, 2007
don't i feel like a dumb bunny, I did the == && ==, not the != && !=.

i works.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC