iam creating a program that the user will enter (upper/lower case) from A-H or (a-h) and someone came up with this code which i dont understand why is it (or ||), and not (and &&)
Code:(((c >= 'A') && (c <= 'H')) || ((c >= 'a') && (c <= 'h')))
iam creating a program that the user will enter (upper/lower case) from A-H or (a-h) and someone came up with this code which i dont understand why is it (or ||), and not (and &&)
Code:(((c >= 'A') && (c <= 'H')) || ((c >= 'a') && (c <= 'h')))
Last edited by joker_tony; 03-21-2008 at 05:32 PM.
Well do you know of the ASCII format?
If not, go to www.asciitable.com
The char, declared as c is being compared to the ASCII value of the letter.
So if the user enters a character that is (greater than or equal to the value of 'A' AND less than/equal to 'H') OR ( greater than or equal to 'a' AND less than/equal to 'h')
it will do what that if statement suggests.
When doing a check for characters entered by the user, you may want to compare the ASCII values of them, because that's how the computer recognizes alphabets, they are assigned an ASCII value, for e.g. 'A' maybe assigned a value of 65.
The only way to access that ASCII value is to put the alphabet in single quotes: 'A'
=========================================
Everytime you segfault, you murder some part of the world
You should have put this in the other thread, not started a new one.
=========================================
Everytime you segfault, you murder some part of the world
thank for your help now i undertand
That is what you put earlier. You haven't specified the range there. That will only check if the character is equal to A or equal to H, you need to specify a range, hence >= or <=Code:if((a==A)||(a==H)&&(a==a)||(a==h))
Also your && || operands had to be switched around, say it out in your head, it will make sense that way.
=========================================
Everytime you segfault, you murder some part of the world
if you need to convert upper or lower case check out the string.h header for tolower() and toupper()
probably easier then rewriting the whole thing.
Too many unnecessary brackets. That code is equivalent to this:If you prefer having unnecessary brackets, then there's certainly no need to go furthur than this:Code:(c >= 'A' && c <= 'H' || c >= 'a' && c <= 'h')Code:((c >= 'A' && c <= 'H') || (c >= 'a' && c <= 'h'))
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"