delete rest of a string

Hey folks,

I'm having trouble with the switch case function lately.

I want to compare a character from a string with a bunch of signs and after it found a ; or % it's supposed to delete the rest of the string

I tried it this way:

for (string::iterator itString=currentString.begin() ; itString!=currentString.end() ; ++itString){
char currentCharacter = *itString;
switch(currentCharacter)
case ' ': currentString.erase(itString);
break;
case ';': currentString.erase(itString, currentString.length());
break;
case '%': currentString.erase(itString, currentString.length());
break;

but that gives me an error: error: no matching function for call to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::erase(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >&, size_t)'|

Thanks in advance

Lenni
Last edited on
your switch is missing { and }
oh yeah that fixed it. Again the smallest mistake made me increidably upset

the lines

case ';': currentString.erase(itString, currentString.length());
break;
case '%': currentString.erase(itString, currentString.length());
break;

are still threwing errors. How can I delete the rest of the string starting at the % or ; sign??? Is there a nice way without creating another loop?
Last edited on
the function is erase(int pos, int len). itString is an iterator. there is no conversion (sort of..). anyway, you can access string elements without iterators. use [].
NARGH I'm just blind or something.

It's actually pretty nice with the iterator since it makes the code pretty small and nice to read.
The problem just was that I godda use string.end() while this is an iterator. string.length() is and int I guess???
Atleast that would explain the error.

If you're interested just check out the cplusplus.com reference for string::erase

Thanks for the help!!

Peace out,

Lenni
Last edited on
You're not passing an iterator as the second parameter. It should be:
str.erase(it,str.end());
Ah written at the same time eh!

Thanks Althar. I guess I would have had it at least now ;)
Topic archived. No new replies allowed.