C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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:
  #1  
Old September 1st, 2012, 11:15 AM
arbitraryproc arbitraryproc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 20 arbitraryproc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 13 m 2 sec
Reputation Power: 0
Not equal not working!!

I can never get the != right in any of my code. Everything compiles without error. I also debug but still cant figure out why i cant skip over this loop. I enter 1 or 2 and it still runs the code inside the while loop. Any help?

[CODE]
int main(){
printf("Please select an option:\n1) Find on what day a specific date falls\n2) Display the twelve month calendar for a given year\n");
int choice = 0;
scanf("%d", &choice);
while((choice != 1) || (choice != 2)){
printf("Entry invalid please enter either 1 or 2\n");
scanf("%d", &choice);
}
printf("Choice is complete: %d", choice);
return 0;
}

Reply With Quote
  #2  
Old September 1st, 2012, 12:35 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 2 m 3 sec
Reputation Power: 1774
> while((choice != 1) || (choice != 2))
How do you make this false?

If choice is 1, then it can't be 2, so you get false || true
If choice is 2, then it can't be 1, so you get true || false

Either way, you loop forever.

Try
while ( !( choice == 1 || choice == 2) )
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old September 1st, 2012, 03:31 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,824 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
It != that is "not working" - that is hardly plausible! Rather it is your boolean logic lat his flawed. See here and here.

Boolean logic and algebra are the fundamental mathematics of computing. You need at least an intuitive of not a rigorous mathematical understanding of it.

Quote:
Originally Posted by salem
Try
while ( !( choice == 1 || choice == 2) )


Which is equivalent also to:

Code:
while( choice != 1 && choice != 2 )


You can take your pick, I suggest the latter since it only requires a single change of operator to correct your code. Intuitively you might describe the first as:

"while neither 1 nor 2",

and the second as:

"while not 1 and not 2"

you can see that in English they mean the same thing, whereas your (arbitraryproc's, not salem's) code reads

"while not 1 or not 2",

which is true for any value, because when its 1 its not 2, and when 2 its not 1, and when it is anything else its neither 1 nor 2!

Consider the following:

NOT(A) AND NOT(B) == NOT(A OR B)
NOT(A) OR BOT(B) == NOT(A AND B)

these are known as De Morgan's Laws

Last edited by clifford : September 1st, 2012 at 03:37 PM.

Reply With Quote
  #4  
Old September 1st, 2012, 05:25 PM
arbitraryproc arbitraryproc is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 20 arbitraryproc User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 13 m 2 sec
Reputation Power: 0
Damn I'm stupid....... Sorry guys. lol Ive definitely wasted some board space on this question

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Not equal not working!!

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap