Solved

String Replace - int goes to garbage!

Posted on 2003-11-13
3
399 Views
Last Modified: 2010-04-01
How do I work this piece of code, so that when the find hits the end of string it stops?

Right now, when it hits the end of the string... it feeds garbage (4294967295) into "place".

I have put ** next to the problem code.

for(pos; pos < speech1.max_size(); pos)
{
place = speech1.find(match1, pos);

if(place != speech1.max_size())
               {
**      speech1.replace(place, match1.length(), " he or she ");
      pos = place + match1.length();
      pos = pos + 6;
      place = pos;
      }
0
Comment
Question by:puckerhoop
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
Sandra-24 earned 500 total points
ID: 9745381
Just glancing at your code your problem seems to me to be that find() returns npos when it doesn't find what it was looking for.

It will put -1 into place if it didn't find match1;

if place is an unsigned int or unsigned long then -1 becomes 4294967295.

so change the line "place = speech1.find(match1, pos);"

to

if((place = speech1.fine(match1,pos)) == std::string::npos)
break;

this will exit the loop when match1 is no longer found in speech1

I'm going to test your code now and see if I'm correct, but try that in the interim.

-Sandra
0
 
LVL 3

Expert Comment

by:Sandra-24
ID: 9745416
According to the msdn documentation on basic_string.find()

"[the function returns] The index of the first character of the substring searched for when successful; otherwise npos."

I tested the code and it works fine (at least in my understanding of what it is meant to do) with that line modified as I showed you.

you can replace std::string::npos with (unsigned long)-1 or std::basic_string::npos or std::wstring::npos or whatever will suit your situation best (I can't see the type of string speech1 is)

Have a good day!

-Sandra

0
 

Author Comment

by:puckerhoop
ID: 9745443
Yahoo!  That did it!

That leads me to another problem, but I will list is separate!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question