To make this work, increment n only when you don't call erase. Otherwise, let it stay where it was: after you delete the 5th character (comma), you want n to stay 5 to point at what used to be 6th and is now 5th character, the space.
Thanks Chervil and Cubbi, it's working fine now =). I included this as the condition for ++n;.
1 2 3 4
I'm studying from C++ Primer Cubbi, so I didn't know about erase-remove. Also I'm trying to keep with the book's structure, so didn't even think about using boost. The code you posted is over my head at the moment, many thanks though I'll be saving it.