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;
}
unsigned int
fromunsigned long
; changingreturn word_count; return 0;
by removing return 0, castingreturn (int) word_count;
, and movingword_count++;
to different parts of myload
function.