Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

String Replace - int goes to garbage!

Posted on 2003-11-13
3
Medium Priority
?
410 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

610 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