The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Not equal not working!!
Discuss Not equal not working!! in the C Programming forum on Dev Shed. Not equal not working!! C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 1st, 2012, 11:15 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 20
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;
}
|

September 1st, 2012, 12:35 PM
|
 |
Contributed User
|
|
|
|
|
> 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) )
|

September 1st, 2012, 03:31 PM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
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.
|

September 1st, 2012, 05:25 PM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 20
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|