| View previous topic :: View next topic |
| Author |
Message |
William Guest
|
Posted: Tue Feb 07, 2006 7:00 pm Post subject: String Comparison |
|
|
I have a vector<string> opt with the following contents:
"CADKRW"
"CADUSD"
"KRWUSD"
I am trying to compare the contents of this vector with:
for ( int i = 0; i < opt.size(); i++ ) {
cout << opt.at(i) + "test" << endl;
cout << opt.at(i).compare( "CADKRW" ) << endl;
if ( 0 == opt.at(i).compare( "CADKRW" ) ) {
pipeOutputFile.write( "|KRW|CAD|", 9 );
}
}
current output:
"CADKRW"test
-33
"CADUSD"test
-33
"KRWUSD"test
-33
expected output:
"CADKRW"test
0
"CADUSD"test
-33
"KRWUSD"test
-33
with contents written to the pipeOutputFile.
Question: why does opt.at(i).compare( "CADKRW" ) gives -33 and not 0?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Valentin Samko Guest
|
Posted: Wed Feb 08, 2006 11:00 am Post subject: Re: String Comparison |
|
|
William wrote:
| Quote: | I have a vector<string> opt with the following contents:
"CADKRW"
"CADUSD"
"KRWUSD"
for ( int i = 0; i < opt.size(); i++ ) {
cout << opt.at(i) + "test" << endl;
cout << opt.at(i).compare( "CADKRW" ) << endl;
}
current output:
"CADKRW"test
-33
Question: why does opt.at(i).compare( "CADKRW" ) gives -33 and not 0?
Because you are comparing "\"CADKRW\"" with "CADKRW". |
--
Valentin Samko - http://www.valentinsamko.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Thomas Tutone Guest
|
Posted: Wed Feb 08, 2006 11:00 am Post subject: Re: String Comparison |
|
|
William wrote:
| Quote: | I have a vector<string> opt with the following contents:
"CADKRW"
"CADUSD"
"KRWUSD"
I am trying to compare the contents of this vector with:
for ( int i = 0; i < opt.size(); i++ ) {
cout << opt.at(i) + "test" << endl;
cout << opt.at(i).compare( "CADKRW" ) << endl;
if ( 0 == opt.at(i).compare( "CADKRW" ) ) {
pipeOutputFile.write( "|KRW|CAD|", 9 );
}
}
current output:
"CADKRW"test
-33
|
Note the quotation marks around "CADKRW" above - that indicates that
the quotation marks are actually part of the contents of opt[0] and the
other strings in opt.
[snip]
| Quote: | expected output:
"CADKRW"test
0
|
[snip]
| Quote: | Question: why does opt.at(i).compare( "CADKRW" ) gives -33 and not 0?
|
Because unlike in opt[0], the quotation marks in the above line are
delimeters, not part of the character array itself that is passed to
the string::compare() function.
In other words, you are comparing "CADKRW" to CADKRW - see the
difference?
Best regards,
Tom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Feb 08, 2006 11:00 am Post subject: Re: String Comparison |
|
|
William wrote:
| Quote: | I have a vector<string> opt with the following contents:
"CADKRW"
"CADUSD"
"KRWUSD"
|
So, your strings contain double quotes too, eh?
| Quote: | I am trying to compare the contents of this vector with:
for ( int i = 0; i < opt.size(); i++ ) {
cout << opt.at(i) + "test" << endl;
cout << opt.at(i).compare( "CADKRW" ) << endl;
|
You should expect to compare them equal (result 0) if you do
cout << opt.at(i).compare( "\"CADKRW\"" ) << endl;
| Quote: | if ( 0 == opt.at(i).compare( "CADKRW" ) ) {
pipeOutputFile.write( "|KRW|CAD|", 9 );
}
}
current output:
"CADKRW"test
|
Presence of the double quotes in the output should be the clue...
| Quote: | -33
"CADUSD"test
-33
"KRWUSD"test
-33
expected output:
"CADKRW"test
0
"CADUSD"test
-33
"KRWUSD"test
-33
with contents written to the pipeOutputFile.
Question: why does opt.at(i).compare( "CADKRW" ) gives -33 and not 0?
|
Because you're comparing the eight-character string:
<doublequote> <C> <A> <D> <K> <R> <W> <doublequote>
to the six-character string
<C> <A> <D> <K> <R> <W>
The first two characters are different and the difference is reported to
you.
V
--
Please remove capital As from my address when replying by mail
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Meador Inge Guest
|
Posted: Wed Feb 08, 2006 12:00 pm Post subject: Re: String Comparison |
|
|
William wrote:
| Quote: | I have a vector<string> opt with the following contents:
"CADKRW"
"CADUSD"
"KRWUSD"
I am trying to compare the contents of this vector with:
for ( int i = 0; i < opt.size(); i++ ) {
cout << opt.at(i) + "test" << endl;
cout << opt.at(i).compare( "CADKRW" ) << endl;
if ( 0 == opt.at(i).compare( "CADKRW" ) ) {
pipeOutputFile.write( "|KRW|CAD|", 9 );
}
}
current output:
"CADKRW"test
-33
"CADUSD"test
-33
"KRWUSD"test
-33
expected output:
"CADKRW"test
0
"CADUSD"test
-33
"KRWUSD"test
-33
with contents written to the pipeOutputFile.
Question: why does opt.at(i).compare( "CADKRW" ) gives -33 and not 0?
From what you have provided it seems that your input contains quotes,
your comparison |
does not. I believe your compare should look like:
opt.at(i).compare( "\"CADKRW\"" )
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Wed Feb 08, 2006 12:00 pm Post subject: Re: String Comparison |
|
|
William wrote:
| Quote: | I have a vector<string> opt with the following contents:
"CADKRW"
"CADUSD"
"KRWUSD"
I am trying to compare the contents of this vector with:
for ( int i = 0; i < opt.size(); i++ ) {
cout << opt.at(i) + "test" << endl;
cout << opt.at(i).compare( "CADKRW" ) << endl;
if ( 0 == opt.at(i).compare( "CADKRW" ) ) {
pipeOutputFile.write( "|KRW|CAD|", 9 );
}
}
current output:
"CADKRW"test
-33
"CADUSD"test
-33
"KRWUSD"test
-33
expected output:
"CADKRW"test
0
"CADUSD"test
-33
"KRWUSD"test
-33
with contents written to the pipeOutputFile.
Question: why does opt.at(i).compare( "CADKRW" ) gives -33 and not 0?
|
The program fails to find a matching string because the string used in
the comparison:
CADKRW
does not match any of the strings stored in the vector, including this
one:
"CADKRW"
Either the quotation marks were not meant to be stored with the string,
or they will have to be taken into account by the comparison. For
example:
opt.at(i).compare( "\"CADKRW\"" );
would match one of the strings in the vector.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Wed Feb 08, 2006 12:00 pm Post subject: Re: String Comparison |
|
|
William <wh2leung (AT) student (DOT) cs.uwaterloo.ca> writes:
| Quote: | Question: why does opt.at(i).compare( "CADKRW" ) gives -33 and not 0?
|
That's hard to tell if you don't show us the exact program you are
running.
This program
#include <vector>
#include <string>
#include <iostream>
#include <ostream>
int main()
{
char const *initializers[] = {
"CADKRW",
"CADUSD",
"KRWUSD"
};
std::vector<std::string> opt(initializers,initializers+3);
for ( int i = 0; i < opt.size(); i++ ) {
std::cout << opt.at(i) + "test" << std::endl;
std::cout << opt.at(i).compare( "CADKRW" ) << std::endl;
}
}
compiled with gcc 3.3.5, writes
CADKRWtest
0
CADUSDtest
1
KRWUSDtest
1
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Paul Floyd Guest
|
Posted: Wed Feb 08, 2006 12:00 pm Post subject: Re: String Comparison |
|
|
On 7 Feb 2006 12:56:28 -0500, William <wh2leung (AT) student (DOT) cs.uwaterloo.ca> wrote:
| Quote: | I have a vector<string> opt with the following contents:
"CADKRW"
"CADUSD"
"KRWUSD"
I am trying to compare the contents of this vector with:
for ( int i = 0; i < opt.size(); i++ ) {
cout << opt.at(i) + "test" << endl;
cout << opt.at(i).compare( "CADKRW" ) << endl;
if ( 0 == opt.at(i).compare( "CADKRW" ) ) {
pipeOutputFile.write( "|KRW|CAD|", 9 );
}
}
current output:
"CADKRW"test
-33
"CADUSD"test
-33
"KRWUSD"test
-33
expected output:
"CADKRW"test
0
"CADUSD"test
-33
"KRWUSD"test
-33
with contents written to the pipeOutputFile.
Question: why does opt.at(i).compare( "CADKRW" ) gives -33 and not 0?
|
First thing, compare is not defined to return any particular values for
greater than and for less than, just that they will be >0 and <0.
Next, you didn't post all of your code. I suspect that you are including
extra quotes (like
vector<string> opt;
opt.push_back("\"CADKRW\"");
opt.push_back("\"CADUSD\"");
opt.push_back("\"KRWUSD\"");
)
This gives me
"CADKRW"test
-1
"CADUSD"test
-1
"KRWUSD"test
-1
If I change that to
vector<string> opt;
opt.push_back("CADKRW");
opt.push_back("CADUSD");
opt.push_back("KRWUSD");
then I get
CADKRWtest
0
CADUSDtest
1
KRWUSDtest
1
as 'expected'.
A bientot
Paul
--
Paul Floyd http://paulf.free.fr (for what it's worth)
Surgery: ennobled Gerald.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Wed Feb 08, 2006 12:00 pm Post subject: Re: String Comparison |
|
|
William wrote:
| Quote: | I have a vector<string> opt with the following contents:
"CADKRW"
"CADUSD"
"KRWUSD"
I am trying to compare the contents of this vector with:
for ( int i = 0; i < opt.size(); i++ ) {
cout << opt.at(i) + "test" << endl;
cout << opt.at(i).compare( "CADKRW" ) << endl;
}
current output:
"CADKRW"test
-33
"CADUSD"test
-33
"KRWUSD"test
-33
|
This is enough to see the real problem.
The strings in your vector are 8 characters long, including two quote
characters.
The string you are comparing to is 6 characters long, with no quote
characters.
Try this instead:
cout << opt.at(i).compare( "\"CADKRW\"" ) << endl;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Wed Feb 08, 2006 2:00 pm Post subject: Re: String Comparison |
|
|
On 7 Feb 2006 12:56:28 -0500, William
<wh2leung (AT) student (DOT) cs.uwaterloo.ca> wrote in comp.lang.c++.moderated:
| Quote: | I have a vector<string> opt with the following contents:
"CADKRW"
"CADUSD"
"KRWUSD"
I am trying to compare the contents of this vector with:
for ( int i = 0; i < opt.size(); i++ ) {
cout << opt.at(i) + "test" << endl;
cout << opt.at(i).compare( "CADKRW" ) << endl;
if ( 0 == opt.at(i).compare( "CADKRW" ) ) {
|
Replace the two lines above with:
cout << opt.at(i).compare( "\"CADKRW\"" ) << endl;
if ( 0 == opt.at(i).compare( "\"CADKRW\"" ) )
| Quote: | pipeOutputFile.write( "|KRW|CAD|", 9 );
}
}
|
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Sumit Rajan Guest
|
Posted: Wed Feb 08, 2006 2:00 pm Post subject: Re: String Comparison |
|
|
"William" <wh2leung (AT) student (DOT) cs.uwaterloo.ca> wrote in message
news:Pine.GSO.4.64.0602071119400.2778 (AT) cpu02 (DOT) student.cs.uwaterloo.ca...
| Quote: | I have a vector<string> opt with the following contents:
"CADKRW"
"CADUSD"
"KRWUSD"
I am trying to compare the contents of this vector with:
for ( int i = 0; i < opt.size(); i++ ) {
cout << opt.at(i) + "test" << endl;
cout << opt.at(i).compare( "CADKRW" ) << endl;
if ( 0 == opt.at(i).compare( "CADKRW" ) ) {
pipeOutputFile.write( "|KRW|CAD|", 9 );
}
}
|
The following code works on does the "expected" thing for me:
#include <iostream>
#include <vector>
#include <string>
int main()
{
using namespace std;
vector<string> opt;
opt.push_back("CADKRW");
opt.push_back("CADUSD");
opt.push_back("KRWUSD");
for ( int i = 0; i < opt.size(); i++ ) {
cout << opt.at(i) + "test" << endl;
cout << opt.at(i).compare( "CADKRW" ) << endl;
}
}
Output:
CADKRWtest
0
CADUSDtest
1
KRWUSDtest
1
You problem probably lies elsewhere. Can you post *compile-able* code that
demonstrates your the issue? It would possibly help in identifying what is
wrong.
Regards,
Sumit.
--
Sumit Rajan <sumitr (AT) msdc (DOT) hcltech.com>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Attila Feher Guest
|
Posted: Wed Feb 08, 2006 2:00 pm Post subject: Re: String Comparison |
|
|
William wrote:
| Quote: | I have a vector<string> opt with the following contents:
"CADKRW"
"CADUSD"
"KRWUSD"
I am trying to compare the contents of this vector with:
for ( int i = 0; i < opt.size(); i++ ) {
cout << opt.at(i) + "test" << endl;
cout << opt.at(i).compare( "CADKRW" ) << endl;
[SNIP]
Question: why does opt.at(i).compare( "CADKRW" ) gives -33 and not 0?
|
Perhaps I have missed soemthing obvious (as usual), but until I see that
I have just one advice: try to also print the .size() of the elements.
It might be that your CADKRW in the vector actually has an "invisible"
(in printing) closing NUL (zero) character at the end?
WW aka Attila
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Wed Feb 08, 2006 4:00 pm Post subject: Re: String Comparison |
|
|
William wrote:
| Quote: | I have a vector<string> opt with the following contents:
"CADKRW"
[...]
cout << opt.at(i).compare( "CADKRW" ) << endl;
[...]
current output:
"CADKRW"test
-33
[...]
expected output:
"CADKRW"test
0
|
As you see in the output, your vector contains the strings enclosed by
double quotes while you are comparing it with non-quoted strings. Try
cout << opt[i].compare("\"CADRKW\"") << endl;
or if you only need to check for equality use
opt[i] == "\"CADRKW\""
BTW: do you really trust yourself so little that you are afraid of getting a
simple loop over a vector wrong? The only reason for me to use vector::at()
is when I can't trust the given index, e.g. when it's user-input.
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|