I'm attempting to load a dictionary file into a binary search tree. I would like to load each node with a combination of letters until it forms a word, thus attaching a definition to that word. Example:
C:
Ca:
Cat: a mammal. Closely related to a Liger.
Currently, I'm attempting to load the sample file and I keep receiving my inFile.fail() conditions. Any help, advice, and/or code-review is greatly appreciated.
Here are the two functions which I think may be causing my issue:
bool Dictionary::insertTrie(string word, string def)
{
Node* newNode = createTrie();
string tempW;
bool retVar;
bool found = searchNode(root, word);
if(found)
retVar = false;
while(!found){
newNode->word = word;
newNode->definition = def;
insert(root, newNode);
retVar = true;
}
/*while(!found){
for(int i = 0; i < word.length(); i++){ //loop to iterate through the string
for(int j = 0; j < ALPHABET_SIZE; j++){ //loop to iterate the nodes
tempW += word[i];
newNode->word = word;
newNode->definition = def;
newNode = newNode->next[j];
}
retVar = true;
}*/
return retVar;
}
bool Dictionary::loadDictionary(string fileName)
{
fstream inFile;
string file;
string words;
string defs;
string tempW;
bool retVar;
inFile.open(fileName, ios::in); // opens
cout << "\nLoading Dictionary...\n";
if(inFile.fail()){
retVar = false;
cout << "ERROR: Dictionary file failed to load. Please try again.\n";
}
else{
while(!inFile.eof()){ //loop reads in words and definitions until the end of file bit is received
for(int i = 0; i < words.length(); i++){
getline(inFile, words, ':'); //receives input of the words stopping at the ':' delimiter
tempW += words[i];
getline(inFile, defs, '\n'); //receives input of the defs stopping at the '\n' delimiter
insertTrie(tempW, defs); //inserts the words and the definition
}
}
retVar = true;
}
inFile.close(); //closes the file
return retVar;
}