473,387 Members | 1,603 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,387 software developers and data experts.

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

Similar topics

6
by: Iain Bishop | last post by:
I'm trying to model objects for the following problem: A building site contains assemblies, each of which can contain other assemblies and/or materials. I have modelled this using a Site...
11
by: sklett | last post by:
I have created a series of classes for my business layer, data retrieval is working fine and things seem good. However I have quickly discovered a design flaw in my system in that my objects...
2
by: Praveen K | last post by:
I have a problem in communicating between the C# and the Excel Interop objects. The problem is something as described below. I use Microsoft Office-XP PIA dll’s as these dll’s were been...
1
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
5
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or...
16
by: shapper | last post by:
Hello, I have a generic list as follows: Dim rows As New Generic.List(Of row) Now I have a row: Dim myRow As row I tried to check, further in my code, if the row is nothing: If myRow Is...
9
by: David | last post by:
Hi all, I posted my question two days ago, and tried to solve this problem. but until now I didn't solve that. and I cut my codes so maybe this time it is more readable. ...
0
by: shahiz | last post by:
This the error i get when i try to run my program Error: Unable to realize com.sun.media.amovie.AMController@18b81e3 Basically i have a mediapanel class that initialize and play the media as...
43
by: John | last post by:
Hi This .net is driving me crazy!! In VB6 I had a type which contained a couple of multi-dimentional arrays which i used to create and read records: Type AAA : Array1(10,10,2) as Integer
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.