comparing multiple strings

N

nitro_punk85

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.
 
J

John Carson

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)
 
I

Ian Collins

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.
 
J

Jaspreet

John said:
if(answer3 == answer1 || answer3 == answer2)

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.
 
J

John Carson

Jaspreet said:
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.
 
N

nitro_punk85

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.
 
T

Tomás

(e-mail address removed) 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
 
N

nitro_punk85

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.
 
R

Richard G. Riley

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,
 
C

c++master

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.........
 
J

Jacek Dziedzic

Jaspreet wrote:
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.

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

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

- J.
 
D

Dietmar Kuehl

c++master said:
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()'.
 
M

Marcus Kwok

c++master said:
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.
 
M

Markus Moll

Hello

c++master said:
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
 
B

Ben Pope

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
 
J

JustBoo

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
 
R

Richard G. Riley

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.
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top