[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

String Replace - int goes to garbage!

Posted on 2003-11-13
3
Medium Priority
?
414 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

834 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