473,394 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 8695
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: beliavsky | last post by:
By mistake I coded something like print ("1" > 1) and got the result "True". Comparing an integer and a string seems meaningless to me, and I would prefer to have an exception thrown. Can...
26
by: William Park | last post by:
How do you compare 2 strings, and determine how much they are "close" to each other? Eg. aqwerty qwertyb are similar to each other, except for first/last char. But, how do I quantify that? ...
5
by: Curtis Gilchrist | last post by:
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
2
by: Manny Chohan | last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny
8
by: Frost | last post by:
Hi All, I am a newbie i have written a c program on unix for line by line comparison for two files now could some one help on how i could do word by word comparison in case both lines have the...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...

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.