0

My spell checker compiles, but doesn't appear to be producing anything when I attempt to run it. It prints "Mispelled Words" and then nothing else, so I suspect my error is in check. Here is what that function looks like:

bool check(const char *word)
{
    char lower_word[LENGTH+1];
    for (int i = 0; i < LENGTH+1; i++)
    {
        lower_word[i] = 0;
    }

    for (int j = 0, n = strlen(word); j < n; j++)
    {
        if (isupper(word[j]))
        {
            lower_word[j] = tolower(word[j]);
        }
        else
        {
            lower_word[j] = word[j];
        }
    }

    int bucket = hash_function(lower_word);
    node *cursor = hashtable[bucket];
    while (cursor != NULL)
    {
        if (strcmp(lower_word, cursor->word)==0)
        {
            return true;
        }
        else
        {
            cursor = cursor->next;
        }
    }
    return false;
}
int word_count = 0;
bool load(const char *dictionary)
{
    FILE* fp = fopen(dictionary, "r");
    if (fp==NULL)
    {
        return false;
    }

    int index;
    char word [LENGTH+1];
    while (fscanf(fp, "%s", word) != EOF)
    {
        node *new_node = malloc(sizeof(node));
        if (new_node == NULL)
        {
            unload();
            return false;
        }
        strcpy(new_node->word, word);
        index = hash_function(new_node->word);
        //int word_count = 0;
        if (hashtable[index] == NULL)
        {
            hashtable[index] = new_node;
            word_count++;
        }
        else
        {
            new_node->next = hashtable[index];
            hashtable[index] = new_node;
            word_count++;
        }
        free(new_node);
    }

    fclose(fp);
    return true;
}

Here is the hash function:

unsigned int hash_function(char* str)
{
    int total = 0;
    for (int i = 0; str[i] != '\0'; i++)
    {
        total += str[i];
    }
    return total % HASHSIZE;
}
5
  • Don't see anything there that would cause it to stop running. Have you tried debug50?
    – curiouskiwi
    Aug 3 '17 at 21:13
  • I have not yet. At first I was thought it was just processing slowly, but I waited awhile and still was getting no output. Aug 8 '17 at 15:12
  • Yeah I can't seem to run debug50. It's just not giving me any output. Aug 11 '17 at 13:41
  • If your program is just hanging (that's what it sounds like), then I'd suspect an infinite loop. The code you've shown here would only reach that situation if, for some reason, cursor->next never ends up NULL. This points to an issue in your load. Can you edit your question to show us your load function?
    – curiouskiwi
    Aug 13 '17 at 1:14
  • Sure can. I will add that now. Aug 17 '17 at 15:32
1

In your load function, you are created a new_node, adding it to your linked list, and then freeing it free(new_node);. You've just thrown away your node.

You shouldn't free your nodes until you are finished (in the unload function).

1
  • Woah thanks! It worked right away. Sep 6 '17 at 18:31

You must log in to answer this question.

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