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<