if(myInputString == "&&") fails, why?

T

theBestFriend

If I type in the vualue && from the standard input and store it into
myInputString variable, I expected that expression if(myInputString ==
"&&") will evaluate to true, but it doesn't.
Can you please explain why and what do I need to change to make it
evaluate to true. I want to treat && as character string, not as a
special characters but don't know how.
When I type in >= or <=, both expressions
if(myInputString == ">=") and if(myInputString == "<=") evaluate to
true.
Thank you for you help and your time.
 
J

Joona I Palaste

theBestFriend said:
If I type in the vualue && from the standard input and store it into
myInputString variable, I expected that expression if(myInputString ==
"&&") will evaluate to true, but it doesn't.

As your friendly C textbook should have explained to you, == is pretty
much useless for comparing strings. Look up strcmp.
 
R

Russell Hanneken

theBestFriend said:
If I type in the vualue && from the standard input and store it into
myInputString variable, I expected that expression if(myInputString ==
"&&") will evaluate to true, but it doesn't.
Can you please explain why and what do I need to change to make it
evaluate to true.

Read this:

http://www.eskimo.com/~scs/C-faq/q8.2.html
When I type in >= or <=, both expressions
if(myInputString == ">=") and if(myInputString == "<=") evaluate to
true.

That's surprising. I would have expected both to evaluate to false. In any
case, as the FAQ says, use strcmp, not the == operator.
 
J

Jens.Toerring

theBestFriend said:
If I type in the vualue && from the standard input and store it into
myInputString variable, I expected that expression if(myInputString ==
"&&") will evaluate to true, but it doesn't.
Can you please explain why and what do I need to change to make it
evaluate to true. I want to treat && as character string, not as a
special characters but don't know how.
When I type in >= or <=, both expressions
if(myInputString == ">=") and if(myInputString == "<=") evaluate to
true.

You can't use '==', '!=' etc to compare strings. When you use these
comparision operators all you do is comparing the pointers to the
first characters of the strings but not their contents. You need
to use the function strcmp() - it returns 0 if both strings are
identical, and a positive or negative integer if they are different
(a positive value if the first string is "less" than the second
and a negative if the second string "less" than the first). You
also have to include <string.h> for the declaration of the
function.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
R

Russell Hanneken

You need to use the function strcmp() - it returns 0 if both strings are
identical, and a positive or negative integer if they are different
(a positive value if the first string is "less" than the second
and a negative if the second string "less" than the first).

I think your parenthetical statement has it backwards--strcmp returns a
negative value if the first string is less than the second, and it returns a
positive value if the second string is less than the first. Or to put it in
another way (that I find easier to remember), strcmp returns a negative
value if the first string is less than the second, and it returns a positive
value if the first string is greater than the second.
 
J

Jens.Toerring

I think your parenthetical statement has it backwards--strcmp returns a
negative value if the first string is less than the second, and it returns a
positive value if the second string is less than the first. Or to put it in
another way (that I find easier to remember), strcmp returns a negative
value if the first string is less than the second, and it returns a positive
value if the first string is greater than the second.

Thanks for the correction, I guess I need to further increase my dose
of caffeine ,-)
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
N

Novak Lalovic

Thank you everyone, Jens in particular.
I did try using strcmp, even before trying the if statement, but I expected
that the expresion will return 1 (true) when the strings are identical, but
in fact it returns 0 and that is what tricked me.
 
M

Mike Wahler

Novak Lalovic said:
Thank you everyone, Jens in particular.
I did try using strcmp, even before trying the if statement, but I expected
that the expresion will return 1 (true) when the strings are identical, but
in fact it returns 0 and that is what tricked me.

Always read the instructions before using an
unfamiliar tool. Assumptions can and will bite you. :)

BTW please don't top post.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top