What's missing?

I was making a program that should replace a letter or two(or more) or a phrase when you enter a replacement character or phrase.

This should be the output:
***
Enter a message: hi sir


Enter a character or phrase to be replaced: i
Enter the replacement character or phrase: z
The message is: hz szr
***

But mine shows up as: "The message is: hz sir".
It only replaced one 'i'.
What am I missing?

Here are the codes I used:

#include<iostream>
#include<string>
using namespace std;

int main()
{
string msg = "";
string dmsg = "";
string rep = "";
int pos = 0;

cout << "Enter a message: ";
getline(cin, msg);
cout << endl;


cout << "Enter a character or phrase to be replaced: ";
getline(cin, dmsg);
pos = msg.find(dmsg, 0);

while(msg.size() < pos)
{cout << "Enter a character or phrase to be replaced: ";
getline(cin, dmsg);}

cout << "Enter the replacement character or phrase: ";
getline(cin, rep);
msg.replace(pos, dmsg.size(), rep);

cout << "The message is: " << msg;

cout << "\n\n";
system("pause");
return 0;
}

Any help would be appreciated. Thank you in advance!
Your changes are path dependent meaning that a previous change could invalidate later matches that would have been found if the original string was unchanged. One way to avoid this is to use std::set:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>
#include <map>

constexpr auto changes = 3;//change as required

int main()
{
    std::cout << "Enter input string \n";
    std::string input{};
    getline(std::cin, input);
    std::string input_copy{input};//keep a copy since original string will be changed shortly
    std::map<std::string,std::string> replacements{};//std::unordered::map would make changes path-dependent
    int i{};
    while (i < changes)
    {
        std::cout << "Enter a character or phrase to be replaced: \n";
        std::string original{};//use string since it can be character or phrase
        getline(std::cin, original);
        std::cout << "Enter the replacement character or phrase: \n";
        std::string replacement{};
        getline(std::cin, replacement);
        replacements[original] = replacement;
        ++i;
    }
    for (const auto& elem : replacements)
    {
        std::string::size_type index = 0;
        while (index != std::string::npos)
        {
            index = input.find(elem.first, index);
            if(index == std::string::npos)
            {
                break;
            }
            else
            {
                input.replace(index, elem.first.size(), elem.second);
            }
            index += elem.first.size();
        }
    }
    std::cout << "Original string: \n";
    std::cout << input_copy << '\n';
    std::cout << "Replaced string: \n";
    std::cout << input << '\n';
}
/*Sample Output:
Enter input string
how are you today
Enter a character or phrase to be replaced:
o
Enter the replacement character or phrase:
A
Enter a character or phrase to be replaced:
re
Enter the replacement character or phrase:
ER
Enter a character or phrase to be replaced:
to
Enter the replacement character or phrase:
A7
Original string:
how are you today
Replaced string:
hAw aER yAu tAday//note the 'to' of 'today' is no longer picked up due to the first change*/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<string>
using namespace std;

int main()
{
   string msg = "";
   string del = "";
   string rep = "";
   int pos = 0;

   cout << "Enter a message: ";
   getline( cin, msg );
   cout << endl;

   cout << "Enter a character or phrase to be replaced: ";
   getline( cin, del );

   cout << "Enter the replacement character or phrase: ";
   getline( cin, rep );

   int delLength = del.size();                   // store size of deleted part
   int repLength = rep.size();                   // store size of replacement

   while ( pos < msg.size() )
   {
      pos = msg.find( del, pos );
      if ( pos == string::npos ) break;
      msg.replace( pos, delLength, rep );
      pos += repLength;
   }

   cout << "The message is: " << msg;
}


Enter a message: Hi Sir
Enter a character or phrase to be replaced: i
Enter the replacement character or phrase: z
The message is: Hz Szr


Enter a message: Tweedledum and Tweedledee agreed to have a battle; for Tweedledum said Tweedledee had spoiled his nice new rattle.
Enter a character or phrase to be replaced: e
Enter the replacement character or phrase: oo
The message is: Twoooodloodum and Twoooodloodoooo agrooood to havoo a battloo; for Twoooodloodum said Twoooodloodoooo had spoilood his nicoo noow rattloo.


Topic archived. No new replies allowed.