0

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;
}
  • 1
    Hi! I must admit that this is a hefty piece of code. Is this **really** the **minimal** verifiable example of misbehaviour? I think all the "input handling" logic is totally unnecessary to test the functionality. Please reduce your example to the bare minimum to necessary to reproduce the error. – Marcus Müller May 08 '16 at 01:08

1 Answers1

0
     while(!inFile.eof()){

This is always a bug.

            getline(inFile, words, ':');  //receives input of the words stopping at the ':' delimiter

And if the input line does not contain a semicolon, this will swallow the entire line, and start reading the next line. And the next line. As long as it takes to find a semicolon. This is obviously wrong.

You need to make two changes here.

  • Check for end of file, properly

  • Use std::getline() to read a single line of text into a std::string, and after the whole line is read, check if the read std::string contains a semicolon.

Furthermore, you asked "I'm attempting to load the sample file I keep receiving inFile.fail() conditions". If so, then posting hundreds of lines of code that had absolutely nothing to do, whatsoever, with reading from the file, served absolutely no purpose, and will likely earn you a few downvotes.

Community
  • 1
  • 1
Sam Varshavchik
  • 84,126
  • 5
  • 57
  • 106
  • My input lines all contain colons. Thank you for your very quick reply and help. I never knew about eof() bug conditions inside of a loop, so thank you for the link to that as well. The only reason I posted my entire Dictionary.cpp was because I didn't know if the error was in my insertTrie function or not, and figured it might help to have scope of the entire code. Once again thank you. –  May 08 '16 at 01:32