I have the following code. As noted, the top part(before the loop) works perfectly (yay!), but once I enter the loop, it does not work. I am trying to replace "his" with "his or her" in this sentence: "It is his money, his time, and his life!"
The first part of the code gives me this:
"It is his or her money, his time, and his life!"
The loop gives me:
"It is hhis or heror her money, his time, and his life!"
How can I fix this?
Thanks in advance!
Also, I have to use find and replace - no substitutes are allowed.
char message[100];
cout<<"Enter the message to be changed: "<<endl;
cin.getline(message,100);
cout<<"Original message is as follows: "<<message<<endl;
cout<<" "<<endl;
string changed=message;
int start2=changed.find(" his",0);
string secret;
secret=changed.replace(start2+1,3,"his or her");
cout<<secret<<endl;
cout<<" "<<endl;
//above code works fine
//Loop has problems :(
string final;
start2=0;
do
{
start2=changed.find("his",0);
string final;
final=changed.replace(start2+1,3,"his or her");
cout<<"The new message is as follows: "<<final<<endl;
char message[100];
cout<<"Enter the message to be changed: "<<endl;
cin.getline(message,100);
cout<<"Original message is as follows: "<<message<<endl;
cout<<" "<<endl;
string changed=message;
int start2;
do
{
int start2=changed.find("his",0);
string final;
final=changed.replace(start2,3,"his or her");
cout<<"The new message is as follows: "<<final<<endl;
No, it is not solved.
My problem is that it is only entering the code once. I forced it to loop a few times, and it gave me this:
"his or her car, his life, his money" (Different words, but same idea)
"his or her or her car, his life, his money"
"his or her or her or her car, his life, his money"
It SHOULD be:
"his or her car, his or her life, his or her money"
QUESTION: How do I make it look how it should?
And sorry about the code tags - this is only my second time posting in here, and I didn't know how to do that. sorry!