0

My load function sometimes overlooks certain words in the dictionary. For example it jumps over the word "nodes" instead of loading it into the hash table.

bool load(const char *dictionary) { // Open and check if NULL FILE *dictionaries = fopen(dictionary, "r");

if (dictionaries == NULL)
{
    return false;
}

//read strings until EOF
int x = 0;
char words[LENGTH + 1];
node *current;

while (x == 0)
{
    fscanf(dictionaries, "%s", words);
    word_count++;

    //create new node
    current = malloc(sizeof(node));
    if (current == NULL)
    {
        return false;
    }

    strcpy(current->word, words);
    current->next = NULL;

    //hash word
    h = hash(current->word);


    //insert node into hash table
    if (table[h] == NULL)
    {
        table[h] = current;
    }
    else
    {
        current->next = table[h];
        table[h] = current;
    }

    if (fscanf(dictionaries, "%s", words) == EOF)
    {
        x = 1;
    }
}
return true;

return false;

}

1

Every time you call the 'fscan' function, you take a word from the dictionary and store it in the variable 'words'. So in your code you're calling it two times per iteration in the while loop, but you're only processing it once. Instead of calling it, you could check the return value of the 'fscan' function to check for the end of the file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .