?
Solved

String Replace - int goes to garbage!

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

764 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