473,790 Members | 3,246 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

if(myInputStrin g == "&&") fails, why?

If I type in the vualue && from the standard input and store it into
myInputString variable, I expected that expression if(myInputStrin g ==
"&&") 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(myInputStrin g == ">=") and if(myInputStrin g == "<=") evaluate to
true.
Thank you for you help and your time.
Nov 13 '05 #1
7 1915
theBestFriend <no**********@y ahoo.com> scribbled the following:
If I type in the vualue && from the standard input and store it into
myInputString variable, I expected that expression if(myInputStrin g ==
"&&") 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.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
Nov 13 '05 #2
"theBestFri end" <no**********@y ahoo.com> wrote in message
news:e7******** *************** ***@posting.goo gle.com...

If I type in the vualue && from the standard input and store it into
myInputString variable, I expected that expression if(myInputStrin g ==
"&&") 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(myInputStrin g == ">=") and if(myInputStrin g == "<=") 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.

--
Russell Hanneken
rg********@pobo x.com
Remove the 'g' from my address to send me mail.
Nov 13 '05 #3
theBestFriend <no**********@y ahoo.com> wrote:
If I type in the vualue && from the standard input and store it into
myInputString variable, I expected that expression if(myInputStrin g ==
"&&") 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(myInputStrin g == ">=") and if(myInputStrin g == "<=") 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
--
_ _____ _____
| ||_ _||_ _| Je***********@p hysik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oe rring
Nov 13 '05 #4
<Je***********@ physik.fu-berlin.de> wrote in message
news:bl******** ****@uni-berlin.de...
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.

--
Russell Hanneken
rg********@pobo x.com
Remove the 'g' from my address to send me mail.
Nov 13 '05 #5
Russell Hanneken <rg********@pob ox.com> wrote:
<Je***********@ physik.fu-berlin.de> wrote in message
news:bl******** ****@uni-berlin.de...
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.


Thanks for the correction, I guess I need to further increase my dose
of caffeine ,-)
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@p hysik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oe rring
Nov 13 '05 #6
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.
<Je***********@ physik.fu-berlin.de> wrote in message
news:bl******** ****@uni-berlin.de...
Russell Hanneken <rg********@pob ox.com> wrote:
<Je***********@ physik.fu-berlin.de> wrote in message
news:bl******** ****@uni-berlin.de...
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.


Thanks for the correction, I guess I need to further increase my dose
of caffeine ,-)
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@p hysik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oe rring

Nov 13 '05 #7
"Novak Lalovic" <nl******@comca st.net> wrote in message
news:m6******** ************@co mcast.com...
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
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
11445
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a href="mailto:DrTebi@yahoo.com">DrTebi</a> This would work like a regular mailto link in any browser, but wouldn't be visible to spiders if they don't have a function to decode it.
2
20535
by: Eric Osman | last post by:
Hi, I'm looking for a javascript function that will convert input such as this: <CLUB Code=" into this: &lt;CLUB Code=&quot;
4
14808
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following expression: .... snip ...
1
3981
by: st | last post by:
Hi, I'm using xmlDocument.Save(xmlTextWriter) to create an Excel-readable file. All works well, except where I've replaced the carriage return chars in the .innertext to XML-compliant " "; It gets changed to "&amp;#10" and doesn't render new lines in the Excel sheet. Can anyone help? Many thanks,
5
3450
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot; &lt;me@mydomain.com&gt;</Address> </Addresses>
7
2808
by: DC Gringo | last post by:
I am having a bear of a time with setting a URL query string as a text value in a dropdownlist and Server.URLEncode does not seem to do its job. theFullLink = theLinkPrefix & theImageryTypeTrimmed & Server.URLEncode("&f=") ddlMyDropDownList.Items.Add(New ListItem("MyTextValue", theFullLink & "af")) Which puts out the following HTML: <option value="~/folder/page.aspx?param1=value1%26f%3daf">-
14
5936
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only because of this, so I hope some of you gurus can enlighten me with this :) In what circumstances can the "&amp;" in the source code be involuntary changed to "&" by a browser when or other software, when editing and uploading the file to the web...
13
2805
by: Ragnar | last post by:
Hi, 2 issues left with my tidy-work: 1) Tidy transforms a "&amp;" in the source-xml into a "&" in the tidied version. My XML-Importer cannot handle it 2) in a long <title>-string a wrap is produced like: <title>my very long title blab la blab la Blabla bla </title> Importer also has got problems with it
2
11300
by: sophia | last post by:
why scanf("%d. %d. %d",&d,&m,&y); is NOT reading the i/p 22 4 1972 correctly, whereas it is reading the i/p 22. 4. 1972 correctly ?. Is it because of the dot in the format string of scanf ?
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10419
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9987
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9023
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5424
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4100
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3709
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.