470,811 Members | 1,101 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

comparing multiple strings

I'm working on a project for my c++ class and I am having trouble
comparing one string to two others using the or operator. It looks
something like this:
if(answer3 == answer1 || answer2)
Is there another command I need to through in there or should I do it a
completely different way?
Any suggestions would be most appreciated.

Feb 24 '06 #1
19 8454
<ni**********@yahoo.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com
I'm working on a project for my c++ class and I am having trouble
comparing one string to two others using the or operator. It looks
something like this:
if(answer3 == answer1 || answer2)
Is there another command I need to through in there or should I do it
a completely different way?
Any suggestions would be most appreciated.


if(answer3 == answer1 || answer3 == answer2)
--
John Carson
Feb 24 '06 #2
ni**********@yahoo.com wrote:
I'm working on a project for my c++ class and I am having trouble
comparing one string to two others using the or operator. It looks
something like this:
if(answer3 == answer1 || answer2)


You can't do that because the or (||) is evaluated before the ==.

You have to use two comparisons (one for each answer) and or the result.

--
Ian Collins.
Feb 24 '06 #3

John Carson wrote:
<ni**********@yahoo.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com
I'm working on a project for my c++ class and I am having trouble
comparing one string to two others using the or operator. It looks
something like this:
if(answer3 == answer1 || answer2)
Is there another command I need to through in there or should I do it
a completely different way?
Any suggestions would be most appreciated.


if(answer3 == answer1 || answer3 == answer2)
--
John Carson


The || is a short cut operator hence if the first expression is true
the second expression would not be calculated. So, if (answer3 ==
answer1) turns out to be true the (answer3 == answer2) would not be
carried out. Not sure if you want it that way or not.

Also, I would suggest you to use strcmp for string comparison instead
of the == operator unless of-course if you have overloaded the ==
operator.

Feb 24 '06 #4
"Jaspreet" <js***********@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com
John Carson wrote:
<ni**********@yahoo.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com
I'm working on a project for my c++ class and I am having trouble
comparing one string to two others using the or operator. It looks
something like this:
if(answer3 == answer1 || answer2)
Is there another command I need to through in there or should I do
it a completely different way?
Any suggestions would be most appreciated.
if(answer3 == answer1 || answer3 == answer2)
--
John Carson


The || is a short cut operator hence if the first expression is true
the second expression would not be calculated. So, if (answer3 ==
answer1) turns out to be true the (answer3 == answer2) would not be
carried out. Not sure if you want it that way or not.


I certainly would. There is no point in doing the second comparison unless
there is more to the OP's intentions than appears to be the case.
Also, I would suggest you to use strcmp for string comparison instead
of the == operator unless of-course if you have overloaded the ==
operator.


std::string defines operator==. strcmp is relevant if these are C-style
strings.

--
John Carson

Feb 24 '06 #5
Well if answer3 == answer1 then I don't need it to or the second one,
but if it is false I do. What I am doing is getting a yes or no answer
from the user and I want them to be able to type Yes or yes and still
have it exit the program. Like I said I'm really new and the furthest
we have gotten in class is while and do-while loops.

Feb 24 '06 #6
ni**********@yahoo.com posted:
I'm working on a project for my c++ class and I am having trouble
comparing one string to two others using the or operator. It looks
something like this:
if(answer3 == answer1 || answer2)
Is there another command I need to through in there or should I do it a
completely different way?
Any suggestions would be most appreciated.


You've written that because English is your native language, and that's
how you would have said it:

If answer3 is equal to answer1 or answer2, then blah blah blah...

C++ doesn't talk like that. It talks like so:

If answer3 is equal to answer1, or if answer3 is equal to answer2, then
blah blah blah...

if ( (answer3 == answer1) || (answer3 == answer2) ) blah blah...

The obvious difference is that we have two separate equality tests. I'll
show you what your original code does:

if(answer3 == answer1 || answer2)

The equality operator has higher precedence than the Logical OR operator,
and so it's evaluated first. It returns a value which is "bool" (ie.
"true" if they're equal, "false" if they're not equal).

So first it compares answer3 to answer1, and yields a "bool" value. Let's
say this value is "true".

Next thing is that the Logical OR operator is evaluated. On the left of
it, we have "true", and on the right we have "answer2". "answer2" must be
able to convert to a "bool" in order for this operation to take place. If
it can't, you'll get a compile error. But let's say it can, and that it's
converted to "false".

Then you'll have (false || true) which turns out to be "true".

(In actual fact, there was no need to carry out the processing of the
second part, because the first part was found to be "true", and so the
entire expression had to evaluate to "true", regardless of the second
value involved).

-Tomás
Feb 24 '06 #7
Thank you guys for all your help, I tried if(answer3 == answer1 ||
answer3 == answer2) and it worked fine. I dunno if this is the best way
to do it, but it will do for now til I learn a better way. Thank you so
much.

Feb 24 '06 #8
On 2006-02-24, ni**********@yahoo.com <ni**********@yahoo.com> wrote:
Well if answer3 == answer1 then I don't need it to or the second one,
but if it is false I do. What I am doing is getting a yes or no
answer
In many languages the second expression will not be evaluated UNLESS
the first one evaulates to FALSE. For an AND (&&) then the second is
only evaluted if the first is TRUE. Do some tests along the lines of

if((a==b)||(a=c)) ...
It can confuse if the comparisons are replaced with assigns since
the naive reader might assume that the following

if(a==b||a=c)

will always result in c==a : not necessarily the case and always
something to be aware of as you move between programming languages.

from the user and I want them to be able to type Yes or yes and still
have it exit the program. Like I said I'm really new and the furthest
we have gotten in class is while and do-while loops.


good luck,
--
Remove evomer to reply
Feb 24 '06 #9

ni**********@yahoo.com wrote:
I'm working on a project for my c++ class and I am having trouble
comparing one string to two others using the or operator. It looks
something like this:
if(answer3 == answer1 || answer2)
Is there another command I need to through in there or should I do it a
completely different way?
Any suggestions would be most appreciated.


hello..
its good that u r working on the project..
see dear i have read all the sugetion but they might have not solved ur
problum.
ur problum is that u r using "==" operator in the string. it do not
work in this way. u shoud be using the "strcom(str1,str2)" which return
the value zero if the string r equal.. if ur problum is solved plz
reply.........

Feb 24 '06 #10
Jaspreet wrote:
<snip>
Also, I would suggest you to use strcmp for string comparison instead
...but thus you enter the Dark Land of C-style char* strings,
traveller beware.

<instead> of the == operator unless of-course if you have overloaded the ==
operator.


Why, the one std::string provides works fine, no?

- J.
Feb 24 '06 #11
c++master wrote:
ur problum is that u r using "==" operator in the string. it do not
work in this way. u shoud be using the "strcom(str1,str2)" which return
the value zero if the string r equal.. if ur problum is solved plz
reply.........


"c++master" you are? Assuming the type of 'answer3' and/or the other
variables is 'std::string', it will work very well to use the equality
operator. Of course, if they are just pointers to 'char' arrays, it
indeed won't work. Of course, 'strcom()' won't work on 'char' pointers
either because the function to compare C-strings is called 'strcmp()'.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 24 '06 #12
c++master <ma***********@gmail.com> wrote:

ni**********@yahoo.com wrote:
I'm working on a project for my c++ class and I am having trouble
comparing one string to two others using the or operator. It looks
something like this:
if(answer3 == answer1 || answer2)
Is there another command I need to through in there or should I do it a
completely different way?
Any suggestions would be most appreciated.


hello..
its good that u r working on the project..
see dear i have read all the sugetion but they might have not solved ur
problum.
ur problum is that u r using "==" operator in the string. it do not
work in this way. u shoud be using the "strcom(str1,str2)" which return
the value zero if the string r equal.. if ur problum is solved plz
reply.........


As John Carson has mentioned, if the variables are of type std::string,
then using operator== is fine. If they are C-style strings (char*),
then strcmp() (not strcom()) must be used.

--
Marcus Kwok
Feb 24 '06 #13
Hello

c++master wrote:
hello..
its good that u r working on the project..
see dear i have read all the sugetion but they might have not solved ur
problum.
ur problum is that u r using "==" operator in the string. it do not
work in this way. u shoud be using the "strcom(str1,str2)" which return
the value zero if the string r equal.. if ur problum is solved plz
reply.........


Please, do us a favor and
a) buy a book on the English language
b) buy a book on C++
c) read them both (thoroughly)

Or maybe this is some sort of joke I don't understand...

Thanks a lot
Markus

Feb 24 '06 #14
sorry dear,
it was just a typing mistake.. it is strcmp()...

Feb 25 '06 #15
sorry dear,
its just the typing mistake.. it is strcmp()...

Feb 25 '06 #16
c++master wrote:
sorry dear,
its just the typing mistake.. it is strcmp()...


No context, posted twice, jerky attitude, that's a plonk.

Brian

--
If televison's a babysitter, the Internet is a drunk librarian who
won't shut up.
-- Dorothy Gambrell (http://catandgirl.com)
Feb 25 '06 #17
ni**********@yahoo.com wrote:
Well if answer3 == answer1 then I don't need it to or the second one,
but if it is false I do. What I am doing is getting a yes or no answer
from the user and I want them to be able to type Yes or yes and still
have it exit the program. Like I said I'm really new and the furthest
we have gotten in class is while and do-while loops.


Sounds like you'd be better off with a case-insensitive comparison.

For that you have a few options, but as far as I know, they pretty much
all involve a loop.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 25 '06 #18
On 25 Feb 2006 18:45:15 GMT, "Default User" <de***********@yahoo.com>
wrote:
c++master wrote:
sorry dear,
its just the typing mistake.. it is strcmp()...


No context, posted twice, jerky attitude, that's a plonk.
Brian


I'm actually laughing out loud. I hear a sniveling little toady typing
on his sticky keyboard: "That's a plonk. Oooo, ooo that's a plonk."

Like anyone on this rotating dirtball swirling through space and time
gives a shite.

"Republics decline into democracies and democracies degenerate into
despotisms." - Aristotle
Feb 26 '06 #19
On 2006-02-25, Default User <de***********@yahoo.com> wrote:
c++master wrote:
sorry dear,
its just the typing mistake.. it is strcmp()...


No context, posted twice, jerky attitude, that's a plonk.

Brian


Do you ever post anything other than school reports on peoples posting
styles?

Do you really think for one minute that anyone gives a damn on who you
have or have not killfiled?

Change the record.

--
Remove evomer to reply
Feb 27 '06 #20

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by beliavsky | last post: by
26 posts views Thread by William Park | last post: by
5 posts views Thread by Curtis Gilchrist | last post: by
4 posts views Thread by agent349 | last post: by
41 posts views Thread by Odd-R. | last post: by
88 posts views Thread by William Krick | last post: by
2 posts views Thread by Manny Chohan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.