c++ - Program stops parsing when last position of string is punctuation -


so i'm trying read words file, , rid of punctuation that. here logic stripping punctuation:

edit: program stops running altogether, want make clear

ifstream file("text.txt");  string              str; string::iterator    cur;  for(file>>str; !file.eof(); file>>str){     for(cur = str.begin(); cur != str.end(); cur++){          if (!(isalnum(*cur))){             cur = str.erase(cur);          }     } cout << str << endl; ... } 

say have text file reads:

this program. has trouble (non alphanumeric chars)  it's own , love it... 

when cout , endl; string right after bit of logic, i'll get

this program has trouble non alphanumeric 

and that's folks. there wrong iterator logic? how fix this?

thank you.

the main logical problem iterators see non-alphanumeric characters iterator gets increased twice: during erase moves next symbol , cur++ for loop increases it, skips every symbol after non-alphanumeric one.

so along lines of:

string              next; string::iterator    cur;  cur = next.begin() while(cur != next.end()){     if (!(isalnum(*cur))){         cur = next.erase(cur);     } else {         cur++;     } } 

this removes non-alphanumeric characters. if need tokenize input, have implement bit more, i.e. remember, whether you're inside word (have read @ least 1 alphanumeric character) or not , act accordingly.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -