473,804 Members | 2,962 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 1916
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
20539
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
14811
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
3982
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
2810
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
5939
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
2807
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
11309
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
9704
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
9571
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
10561
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
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5505
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...
1
4277
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
3803
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.