C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 2nd, 2008, 01:00 PM
XtremeDry XtremeDry is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Location: kent, uk
Posts: 4 XtremeDry User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m
Reputation Power: 0
Little bit of help please [problem with 'while' loop]

Hey, im bulding a program for an assigment which converts numbers to roman nurmerals, i have it all working except at the begining where the user inputs which mode he wants to run the program in.

Code:
void main(){ 
 char answer;  
printf("\nWelcome to Roman numeral convertor by Mike Pledge!\n\nWould you like to run this application in (B)atch or (I)nput mode??\n\n"); 
scanf("%c%*c", &answer);  

while(answer != 'B' || answer != 'b' || answer != 'i' || answer != 'I')
{ 
printf("\nPlease make sure you enter either B for Batch or I for Input\n\n"); 
scanf("%c%*c", &answer); }   

if(answer == 'I' || answer == 'i')
 input();

if(answer == 'B' || answer == 'b') 
batch();   }



the while loop doesn't seem to work, and will just keep running whatever you put in, however if i change it to :
Code:
while(answer != 'B' )

it works fine (when i enter B)


Any help will be great,

Thanks

Reply With Quote
  #2  
Old December 2nd, 2008, 02:48 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information.
 
Join Date: Sep 2005
Posts: 790 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 4 Days 41 m 4 sec
Reputation Power: 4
Change the || to && ?
__________________
Current project: roborally

Reply With Quote
  #3  
Old December 2nd, 2008, 03:57 PM
XtremeDry XtremeDry is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Location: kent, uk
Posts: 4 XtremeDry User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m
Reputation Power: 0
the problem with changing it to && is that it will look to satisfy all the requirments, i only need one of the requirments to be true for it to break the loop

Reply With Quote
  #4  
Old December 2nd, 2008, 04:02 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information.
 
Join Date: Sep 2005
Posts: 790 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 4 Days 41 m 4 sec
Reputation Power: 4
The loop will continue as long as the whole condition is true. Your condition is always true. Did you try using &&?

Reply With Quote
  #5  
Old December 2nd, 2008, 04:16 PM
XtremeDry XtremeDry is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Location: kent, uk
Posts: 4 XtremeDry User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m
Reputation Power: 0
you were right, can i ask somthing though, i was taught that using && with multiple conditions meant all of them had to be satisfied, but while using || meant only one of them had to be true?

but either way thank you soo much for your help

Reply With Quote
  #6  
Old December 2nd, 2008, 05:24 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information.
 
Join Date: Sep 2005
Posts: 790 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 4 Days 41 m 4 sec
Reputation Power: 4
Yes that is right. In your case each condition is an inequality, so they are sateisfieds when the operands are not equal. You could also have rewritten the while like this:
Code:
while( ! (answer == 'B' || answer == 'b' || answer == 'i' || answer == 'I') ) ...

The negation now applies to the whole expression.
Comments on this post
XtremeDry agrees: Cheers

Reply With Quote
  #7  
Old December 2nd, 2008, 06:09 PM
XtremeDry XtremeDry is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Location: kent, uk
Posts: 4 XtremeDry User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 15 m
Reputation Power: 0
Thumbs up

ahhh lol i never thought of it like that, seems it can be a bit of a pit trap, i will be more aware for the future

again thank you for all your help

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Little bit of help please [problem with 'while' loop]


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



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

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





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT