0

There is a problem with my load function which i cannot wrap my head around. I can run speller however because of this one problem, my misspelled words is always more than the actual value. What is wrong with my load? I am sure the problem is in load.

bool load(const char *dictionary)

{

root = calloc(1, sizeof(node));
total_words = 0;

FILE *fp = fopen(dictionary, "r");

if (fp == NULL)

{

printf("Could not open %s\n", dictionary);
    return false;
}
node *trav = root;
for (int c = fgetc(fp); c != EOF; c = fgetc(fp))
{
    if (c == '\n')
    {
        trav->is_word = true;
        total_words++;
        trav = root;
    }
    else
    {
        int index = getIndex(c);
        if (trav->children[index] == NULL )
        {
            trav->children[index] = calloc(1, sizeof(node));
        }
        else
        {
            trav = trav->children[index];
        }

    }
}
fclose(fp);

return true;

}

UPDATE*** After removing else function from if (trav->children[index]==NULL) i can run the program. Meaning i just type trav=trav->children[index] without putting it in else. But i dont understand how that makes the program work. It seems to me that its the same

0

In the case where trav->children[index] == 0 you allocate a new node put you do not set your current trav to the children. This means you skip a character if the node doesn't exist.

You must log in to answer this question.

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