470,849 Members | 1,144 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Problem with find for string objects

I was trying a function that read an string (which previously had
attached two special characters & for the beginning of the word and #
for the end of the word) and if it was "<word>ize" it changed into
"change into <word>". I did this function:

bool Ize(string& s)
{
bool is=false;
string eval="ize";
int end=s.find('#');
int idx=s.rfind(eval,fin);
if(idx==end-eval.size())
{
is=true;
s.erase(idx,eval.size());
s.insert(s.find('&'),"change into ");
}
return is;
}

The problem I'm running into is with the string "z". For whatever the
reason s.rfind(eval,fin) returns -1 so it enters the if and when I try
to erase all hell breaks lose.

Why does it return -1 and not string::npos?

Thanks.

May 30 '06 #1
2 1615
"Gaijinco" <ga******@gmail.com> schrieb im Newsbeitrag
news:11**********************@j55g2000cwa.googlegr oups.com...
I was trying a function that read an string (which previously had
attached two special characters & for the beginning of the word and #
for the end of the word) and if it was "<word>ize" it changed into
"change into <word>". I did this function:

bool Ize(string& s)
{
bool is=false;
string eval="ize";
int end=s.find('#');
int idx=s.rfind(eval,fin);
if(idx==end-eval.size())
{
is=true;
s.erase(idx,eval.size());
s.insert(s.find('&'),"change into ");
}
return is;
}

The problem I'm running into is with the string "z". For whatever the
reason s.rfind(eval,fin) returns -1 so it enters the if and when I try
to erase all hell breaks lose.

Why does it return -1 and not string::npos?


-1 IS string::npos -- when you make the mistake to convert it to an int. The
result of string::find is string::size_type. If that's too much typing, you
could eventually use size_t, but NEVER int.

Heinz

May 30 '06 #2

Gaijinco wrote:
I was trying a function that read an string (which previously had
attached two special characters & for the beginning of the word and #
for the end of the word) and if it was "<word>ize" it changed into
"change into <word>". I did this function:

bool Ize(string& s)
{
bool is=false;
string eval="ize";
int end=s.find('#');
int idx=s.rfind(eval,fin);
if(idx==end-eval.size())
{
is=true;
s.erase(idx,eval.size());
s.insert(s.find('&'),"change into ");
}
return is;
}

The problem I'm running into is with the string "z". For whatever the
reason s.rfind(eval,fin) returns -1 so it enters the if and when I try
to erase all hell breaks lose.

Why does it return -1 and not string::npos?


It doesn't return string::npos because you are converting it to an int,
and npos is not representable as an int on your system.

Neither "find" nor "rfind" return ints, they return string::size_type.
Even if you change the code though, I don't think it will fix your
problem. rfind is returning "-1" because the value doesn't exist in the
string.

May 30 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Iain Bishop | last post: by
11 posts views Thread by sklett | last post: by
1 post views Thread by tangus via DotNetMonster.com | last post: by
6 posts views Thread by TPJ | last post: by
16 posts views Thread by shapper | last post: by
43 posts views Thread by John | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.