I'll try to explain the changes. :)
Moving the file handle (myfile) to the class definition allows me to use one handle, and keep the file open for output during the recursive loop.
Originally, the file was being reopened for each word you were adding to the file. Also, by not overwriting the file, every additional run would add more duplicates.
As for the removePunct, I changed the assignments to only occur if root was not null. The position of the removePunct needs to be so that any compares or inserts we do ensures the word is clean.
For removePunct method, the assigning the variable to string::size_type will allow us to correctly iterate the string in memory. Assuming integer may be incorrect and lead to problems compiling on different systems.
http://cplusplus.com/reference/std/locale/ispunct/
ispunct compares if a letter belongs to punctuation group of characters. Each character is compared and if its a punctuation character, its erased from the word. When we erase a character, we need to adjust the index to reflect the change so the next iteration will point to the next letter.
Another approach would be to create a tmp string, and add non-punctuation characters to the string then return the tmp string.
1 2 3 4 5 6 7 8 9 10 11
|
void BST::removePunct(string &word)
{
string str;
for ( string::size_type ix = 0; ix < word.size(); ++ix )
{
if ( ! ispunct(word[ix]) ) {
str.push_back(word[ix]); // append chr to temp string
}
}
word.assign(str); // reassign temp string to word
}
|
Hope that helps. :)