I'm having trouble with cleaning a string from nonchars correctly. When the string contians for example "test&set" the function deletes the "&" and sticks both words together (testset). I'm using getline() with a space as delimiter. This is my code:
And yeah your right, basicly I don't know how to split a string into words. I first used the space delimiter to solve it but that just doesn't cut it for semi weird strings...
You might have a problem if the string starts with a 'punctuation' character, like "&Eat Quiche".
There is nothing else wrong with your syntax that I can see.
I presume that append() takes a char* to something that is delete[]ed later. (Otherwise you'd have a memory leak.)
I'm not sure your program is well-organized though... but that is just a gut-wrench reaction to cleanword() doing a bunch of file I/O with externally-parameterized streams.
What exactly is not working for you?
[edit] Hmm, take a second to help one's sick wife and miss the whole conversation ...
Actually "&Eat Quiche" is cleaned just fine, "Eat&Quiche" is where the problems start. The string "word" is not actually a word, but the string it takes from getline() until a space is introduced.
And yes this function is still a bit messy :P The rest is not that bad though.
Ah, you will need a loop to find each occurrance of punctuation and extract the word before it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// So long as there are words left in our string...
while (!word.empty())
{
// Find the first occurrance of any punctuation in the word, if any
string::size_type n = 0;
while ((n < word.length()) && !ispunctuation( word[ n ] )) ++n;
// If we found a non-empty string, add it to our list
if (n > 0)
{
newword = newchar[ n ];
strcpy( newword, word.substr( 0, n ).c_str() );
append( newword );
}
// Remove what we found from the word
word.erase( 0, n + 1 );
}
Alas it was too early to celebrate... After nesting Duoas function in my own function, for an unknow reason, it sometimes just stops in the middle of the function. Not with all text, all of my own typed test files work while copy'd text fail all the time... What am I doing wrong?
void linklist::cleanword()
{
string word;
cout << "File contents:\n" << endl;
/*while file is not end of file*/
while (!infile.eof())
{
getline(infile, word, (' '));
cout << word << endl;
for (string::size_type i=0; i<word.length(); i++)
{
word[i]=tolower(word[i]);
cout << word << endl;
}
/*while there is a string in word*/
while (!word.empty())
{
/*look for punctuation*/
string::size_type n = 0;
while ((n < word.length()) && !ispunctuation( word[n] )) ++n;
/*if there is a word, append*/
if (n > 0)
{
newword = newchar[n];
strcpy(newword, word.substr( 0, n ).c_str());
append(newword);
}
/*erase findings*/
word.erase(0, n + 1);
}
}
}
Appareantly it has trouble with the getline() 3rd parameter, wich is a space. If I don't use that parameter getline() will automaticly use '\n' as a delimiter. So I still get the malfunction but stops at a different line...