| View previous topic :: View next topic |
| Author |
Message |
nitro_punk85@yahoo.com Guest
|
Posted: Fri Feb 24, 2006 5:06 am Post subject: 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. |
|
| Back to top |
|
 |
Jaspreet Guest
|
Posted: Fri Feb 24, 2006 5:06 am Post subject: Re: comparing multiple strings |
|
|
John Carson wrote:
| Quote: | nitro_punk85 (AT) yahoo (DOT) com> wrote in message
news:1140754458.000552.236300 (AT) v46g2000cwv (DOT) googlegroups.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. |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Fri Feb 24, 2006 5:06 am Post subject: Re: comparing multiple strings |
|
|
nitro_punk85 (AT) yahoo (DOT) com wrote:
| Quote: | 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. |
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Fri Feb 24, 2006 5:06 am Post subject: Re: comparing multiple strings |
|
|
<nitro_punk85 (AT) yahoo (DOT) com> wrote in message
news:1140754458.000552.236300 (AT) v46g2000cwv (DOT) googlegroups.com
| Quote: | 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 |
|
| Back to top |
|
 |
John Carson Guest
|
Posted: Fri Feb 24, 2006 6:06 am Post subject: Re: comparing multiple strings |
|
|
"Jaspreet" <jsingh.oberoi (AT) gmail (DOT) com> wrote in message
news:1140756962.329293.296370 (AT) z34g2000cwc (DOT) googlegroups.com
| Quote: | John Carson wrote:
nitro_punk85 (AT) yahoo (DOT) com> wrote in message
news:1140754458.000552.236300 (AT) v46g2000cwv (DOT) googlegroups.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.
| Quote: | 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 |
|
| Back to top |
|
 |
nitro_punk85@yahoo.com Guest
|
Posted: Fri Feb 24, 2006 8:06 am Post subject: Re: comparing multiple strings |
|
|
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. |
|
| Back to top |
|
 |
nitro_punk85@yahoo.com Guest
|
Posted: Fri Feb 24, 2006 10:06 am Post subject: Re: comparing multiple strings |
|
|
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. |
|
| Back to top |
|
 |
Tomás Guest
|
Posted: Fri Feb 24, 2006 10:06 am Post subject: Re: comparing multiple strings |
|
|
nitro_punk85 (AT) yahoo (DOT) com posted:
| Quote: | 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 |
|
| Back to top |
|
 |
Richard G. Riley Guest
|
Posted: Fri Feb 24, 2006 12:06 pm Post subject: Re: comparing multiple strings |
|
|
On 2006-02-24, nitro_punk85 (AT) yahoo (DOT) com <nitro_punk85 (AT) yahoo (DOT) com> wrote:
| Quote: | 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.
| Quote: | 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 |
|
| Back to top |
|
 |
Marcus Kwok Guest
|
Posted: Fri Feb 24, 2006 3:46 pm Post subject: Re: comparing multiple strings |
|
|
c++master <mastersaurabh (AT) gmail (DOT) com> wrote:
| Quote: |
nitro_punk85 (AT) yahoo (DOT) 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 |
|
| Back to top |
|
 |
Markus Moll Guest
|
Posted: Fri Feb 24, 2006 4:06 pm Post subject: Re: comparing multiple strings |
|
|
Hello
c++master wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Dietmar Kuehl Guest
|
Posted: Fri Feb 24, 2006 4:06 pm Post subject: Re: comparing multiple strings |
|
|
c++master wrote:
| Quote: | 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:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence |
|
| Back to top |
|
 |
Jacek Dziedzic Guest
|
Posted: Fri Feb 24, 2006 4:06 pm Post subject: Re: comparing multiple strings |
|
|
Jaspreet wrote:
<snip>
| Quote: | 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>
| Quote: | of the == operator unless of-course if you have overloaded the ==
operator.
|
Why, the one std::string provides works fine, no?
- J. |
|
| Back to top |
|
 |
c++master Guest
|
Posted: Fri Feb 24, 2006 4:06 pm Post subject: Re: comparing multiple strings |
|
|
nitro_punk85 (AT) yahoo (DOT) com wrote:
| Quote: | 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......... |
|
| Back to top |
|
 |
c++master Guest
|
Posted: Sat Feb 25, 2006 6:06 pm Post subject: Re: comparing multiple strings |
|
|
sorry dear,
it was just a typing mistake.. it is strcmp()... |
|
| Back to top |
|
 |
|