0

I'm trying to make a code that backspaces for every time the character < is found in the string.

The code I have only seems to do it the first time it appears and nothing happens in the succeeding times it appears.

Any tips on how to fix please? Is there a more efficient loop I can use?

void decodeback(string orig, string search, string replace) {
  size_t pos = 0;
  do {
    pos = orig.find(search, pos);
    if (pos == string::npos)
      break;
    orig.erase(pos, search.length());
    orig.erase(pos - 1, search.length());

    cout << orig << endl;
  } while (pos += replace.length());
}

int main() {
  // input the question
  string question = "What is the message? ";
  // output the answer
  string answer = "The real message is ";

  // special characters
  string shift = "^";
  string back = "<";
  string test = "a";

  // input the coded message
  string answer1;

  // output decoded message
  string answer2;

  cout << question;
  cin >> answer1;
  cout << "decoding ";

  decodeback(answer1, back, test);

  return 0;
}

Example input:

hello<<

Desired output:

hel

Actual output:

hell<
Biffen
  • 6,249
  • 6
  • 28
  • 36
  • 3
    It is not really clear what your `decodeback` is supposed to do. For example the method does not use the `replace` parameter, other than adding its length to the `pos`, which might be the reason for finding only the first occurence – 463035818_is_not_an_ai Aug 27 '18 at 12:38
  • I suggest you learn how to use your debugger. You should be abkle to find this kind of simple problems yourself. I also suggest you read [this](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Jabberwocky Aug 27 '18 at 12:48

3 Answers3

1

This part does not do what you want :

while(pos += replace.length());

Furthermore, pos is not adjusted after erasing two characters.

These two mean that you can skip over '<' characters.

Instead, try :

while (true) {
    pos = orig.find(search, pos);
    if(pos == string::npos)
        break;
    orig.erase(pos--, search.length());
    orig.erase(pos, search.length());

    cout<<orig<<endl;
}

Note that this code assumes there will never be a '<' character as the first character of the string, or bad things will happen.

It also assumes that search.length() will always be 1, or the second erase call will not do what's expected. That second erase call should probably just be :

orig.erase(pos, 1);
Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
0

Error is that your loop condition is broken

while(pos += replace.length())

On a higher level, i would suggest using boost for example, they have some neat string algorithms.

See for example, this answer: How do I Search/Find and Replace in a standard string?

darune
  • 10,480
  • 2
  • 24
  • 62
0

If I understand your intention right, the first thing to do is to move the following line out of the do-while loop in decodeback function.

cout << orig << endl;

This is because you want to print the string after removing all the < s that have been found.
If you do not want the < to be replaced by anything else, you can also remove the second erase, i.e. the following line from the do-while loop.

orig.erase(pos - 1, search.length());
P.W
  • 26,289
  • 6
  • 39
  • 76