|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
||||
|
||||
|
while loop not cooperating damnit!
I would like my program to stop when I press 'n' or 'N' but it doesn't. Here is my code:
#include <iostream> using namespace std; int main() { int quantity; int product_order; float PRODUCT; char flag = 'f'; char quit; do { do { flag = 'f'; cout << "Here are your product options:\n\n"; cout << "1. Pizza\n2. Soda\n3. Burgers\n4. Steak\n5. Eggs\n\nPlease make your selection using options 1-5.\n\n"; cout << "What product would you like to order?\n"; cin >> product_order; if (product_order < 1 || product_order > 5) { flag = 't'; cout << "Type in a number between 1-5 please!!\n\n\n\n\n\n"; } } while (flag == 't'); if (flag == 'f') { switch(product_order) { case 1: PRODUCT = 2.98f; cout << "You have selected product 1 which is $2.98!\n"; break; case 2: PRODUCT = 4.50f; cout << "You have selected product 2 which is $4.50!\n"; break; case 3: PRODUCT = 9.98f; cout << "You have selected product 3 which is $4.50!\n"; break; case 4: PRODUCT = 4.49f; cout << "You have selected product 4 which is $4.49!\n"; break; case 5: PRODUCT = 6.87f; cout << "You have selected product 5 which is $6.87!\n"; break; } cout << "How many of product " << product_order << " would you like?\n"; cin >> quantity; float total = quantity * PRODUCT; cout << "Your total is: "<< total << "\n\n\n"; cout << "Would you like to do this again? (N for NO, and any other key for YES) \n"; cin >> quit; } } while (quit != 'N' || quit != 'n'); return 0; }
__________________
hmmm... Last edited by andy3109 : February 27th, 2003 at 03:21 PM. |
|
#2
|
|||
|
|||
|
It's been a while since I've used cin but I'm guessing your quit variable is holding whitespace ie. "Carriage return" 0x13 or at least a space or something.
put a Code:
cout << (int) quit; inside the end of the loop and you should find out. You probably have to Code:
cin >> quit; cin >> quit; once to eat the whitespace and the next to get the 'y' or 'n'
__________________
-- ngibsonau |
|
#3
|
|||
|
|||
|
"It's been a while since I've used cin but I'm guessing your quit variable is holding whitespace"
cin skips over whitespace, so that's not the problem. Your loop control is faulty: while (quit != 'N' || quit != 'n'); If I type in 'N', then the condition quit != 'n' is true, and with an OR conditional if one of the conditions is true, it returns true. Last edited by 7stud : February 27th, 2003 at 03:41 PM. |
|
#4
|
||||
|
||||
|
I think your problem is that this:
Code:
while (quit != 'N' || quit != 'n'); needs to be changed to this: Code:
while (quit != 'N' && quit != 'n'); Whether the user enters 'N' or 'n', one of the conditions in your while loop test will always be true, and thus the loop will continue. Using '&&', the loop will end when either 'N' or 'n' is entered. |
|
#5
|
||||
|
||||
|
It is "and"....AND it makes sense to me now. Thanks for all your replies guys!
-andy |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > while loop not cooperating damnit! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|