0
votes

I'm getting some kind of infinite loop on my check function for Speller in CS50x, and I'm also getting some bizarre results from the cursor in the terminal via printf. I'm sure what to make of this, but nothing I've tried has helped. This issue doesn't seem to happen if I use the small dictionary, but the spell check doesn't work in that case anyway...

Lastly, I do think I'm loading the dictionary into the hash table correctly (via the load function). I.e. I'm not getting any errors to the contrary.

// Returns true if word is in dictionary, else false
bool check(const char *word)
{

    if (isalnum(word[0]))
    {
        // Create a cursor to move through the linked list by hashing the word to get the bucket location
        node *cursor = table[hash(word)];
        if (cursor == NULL)
        {
            return false;
        }

        while (cursor != NULL)
        {
            printf("Fire5\n");
            printf("word from text source: %s\n", word);
            printf("word at cursor: %s\n", cursor->word);

            // Check if the word at that point in the linked list is a match
            if (strcasecmp(word, cursor->word) == 0)
            {
                return true;
            }

            // If the word is not a match, take the cursor and move it to the next node on the linked list.
            cursor = cursor->next;
        }
    }
    
    return false;
}

Text

EDIT: This is how I initialize the table:

typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Hash table
node *table[N];
When you initialize your table, did you remember to initialize all the links and other fields? The infinite looping indicates to me that next isn't set to a valid value. The random text there indicates to me that cursor is pointing to garbage or maybe the word is not initialized and/or null-terminated. - Jeff Mercado
Thanks for the reply. The table is being initialized as a "node" struct which has two properties, word and next. I'll add a breakdown of that to my original question. But to respond to your comment, I think that word is initialized and not null. - MothraVsMechaBilbo
As a followup, I went back to the load function to see if the words weren't getting loaded into the table there, and using this function: printf("new node word: %s\n", new_node->word); I saw that all the words from the loaded dictionary made it into new_nodes. - MothraVsMechaBilbo