Wynd
Dec 15th, 2001, 03:12 PM
I want to erase from a string all puctuation characters. This is my code so far:string input;
string bad = "!@#$%^&*()~`[]{}-=_+|\"\'\\<>,.?/ ";
cout << "Enter a string: ";
getline(cin, input, '\n');
for (int i = 0; i <= input.length(); i++)
for (int j = 0; j <= bad.length() - 1; j++)
if (input[i] == bad[j])
{
cout << "Bad character found: \"" << bad[j] << "\"" << endl;
input.erase(i, 1);
}
cout << input << endl;The problem is this: if I enter "he&&&llo" for example, it only erases two ampersands, and the result is "he&llo". How can I fix this?
string bad = "!@#$%^&*()~`[]{}-=_+|\"\'\\<>,.?/ ";
cout << "Enter a string: ";
getline(cin, input, '\n');
for (int i = 0; i <= input.length(); i++)
for (int j = 0; j <= bad.length() - 1; j++)
if (input[i] == bad[j])
{
cout << "Bad character found: \"" << bad[j] << "\"" << endl;
input.erase(i, 1);
}
cout << input << endl;The problem is this: if I enter "he&&&llo" for example, it only erases two ampersands, and the result is "he&llo". How can I fix this?