0

I am counting one word too many every time my dictionary loads, what could be the cause?

I initialize the variable 'word_count' here:

bool load(const char* dictionary)
{
    // Open the "dictionary".
    FILE* speller = fopen(dictionary, "r");

    // Initialize 'Hashtable' to point to NULL.
    for (unsigned long i = 0; i < HASHTABLE; i++)
    {
        hashtable[i] = NULL;
    } 

    // Keep track of the number of words in the dictionary.
    word_count = 0;

    // Scan the whole file for new words.
    while (feof(speller) == 0)

...

and then I track the number of words after each has been inserted in my hashtable. I can't imagine why there would be an extra one, unless feof has something to do with it?

    // Keep track of the number of words in the dictionary.
    word_count += 1;     
}
fclose(speller);
return true;

}

1
  • Some things I tried: changing "word_count" to unsigned int from unsigned long; changing return word_count; return 0; by removing return 0, casting return (int) word_count;, and moving word_count++; to different parts of my load function. Commented Dec 28, 2014 at 20:01

2 Answers 2

1

I did not check the status of fscanf. I suspect it was because the loop ran once, without reading a word, and then exit at EOF. I used the following, and I get all green lights :)

    // Get word and check that it does not exceed 'LENGTH'.
    if (fscanf(speller, "%s", new_node->word) == 1)
    {
        // Keep track of the number of words in the dictionary.
        word_count += 1;    
    }
-1

Have you updated the vm with update50. I remember having a similar problem until I updated it.

You must log in to answer this question.

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