Solved

String Replace - int goes to garbage!

Posted on 2003-11-13
3
396 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now