C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old February 27th, 2003, 03:12 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
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.

Reply With Quote
  #2  
Old February 27th, 2003, 03:23 PM
ngibsonau ngibsonau is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 138 ngibsonau User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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

Reply With Quote
  #3  
Old February 27th, 2003, 03:28 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
"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.

Reply With Quote
  #4  
Old February 27th, 2003, 03:40 PM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 24 sec
Reputation Power: 9
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.

Reply With Quote
  #5  
Old February 27th, 2003, 04:29 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
It is "and"....AND it makes sense to me now. Thanks for all your replies guys!
-andy

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > while loop not cooperating damnit!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway