0

I've exhausted all my options which is why I'm making this post. When I simply run the program it prints every word as misspelled. The problem itself probably lies in load or check. Running the debugger I've noticed that it always finds that the cursor is NULL. But I can't tell if that's an issue with my check function or my load function. There's also apparently a memory leak in load which I assume is part of the problem. If anyone can help me figure this out I would be very thankful.

global declarations:

#include <stdbool.h>
#include <strings.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "dictionary.h"

/**
 * Hash function from reddit user 'delipty'.
 * URL: https://www.reddit.com/r/cs50/comments/1x6vc8/pset6_trie_vs_hashtable/cf9nlkn/
 */
int hash_it(const char *needs_hashing)
{
    unsigned int hash = 0;
    for (int i=0, n=strlen(needs_hashing); i<n; i++)
        hash = (hash << 2) ^ needs_hashing[i];
    return hash % HASHTABLE_SIZE;
}

// declare node struct
typedef struct node
{
    char string[LENGTH+1];
    struct node *next;
}
node;

// make hashtable size dynamic later
int HASHTABLE_SIZE = 85000;

// global hashtable declaration
node *hashTable[HASHTABLE_SIZE];

// multiple functions need access to word count in dictionary
int wordCount;

// indicates if dictionary is loaded
int isLoaded;

load function:

bool load(const char *dictionary)
{
    FILE *fp = fopen(dictionary, "r");
    if (fp == NULL)
    {
        printf("Could not open dictionary!");
        return false;
    }

    for (int i = 0, k = HASHTABLE_SIZE; i < k; i++)
    {
        hashTable[i] = NULL;
    }

    char buffer[LENGTH+1];

    while (fscanf(fp, "%s", buffer) != EOF)
    {
        int nodeNumber = hash_it(buffer);

        if(hashTable[nodeNumber] == NULL)
        {
            hashTable[nodeNumber] = malloc(sizeof(node));

            if (hashTable[nodeNumber] == NULL)
            {
                unload();
                return false;
            }
            else
            {
                strcpy(hashTable[nodeNumber]->string, buffer);
                hashTable[nodeNumber]->next = NULL;
                wordcount++
            }
        }
        else if(hashTable[nodeNumber] != NULL)
        {
            node *newNode = malloc(sizeof(node));
            strcpy(newNode->string, buffer);
            newNode->next = hashTable[nodeNumber];
            hashTable[nodeNumber] = newNode;
            wordCount++;
        }
        else
        {
            fprintf(stderr, "Something went horribly wrong!");
            return false;
        } 
    }

    isLoaded = 1;

    return true;
}

check function:

bool check(const char *word)
{
    if(isLoaded != 1)
    {
        fprintf(stderr, "Dictionary not loaded!");
        return false;
    }

    int hashValue = hash_it(word);

    //node *hashTable[HASHTABLE_SIZE];

    //node *cursor = malloc(sizeof(node));

    node *cursor = hashTable[hashValue];

    if(cursor == NULL)
    {
        return false;
    }
    else
    {
        while(cursor != NULL)
        {
            int i = strcasecmp(word, cursor->string);

            if(i == 0)
            {
               return true; 
            }

            cursor = cursor->next;
        }
    }
    //free cursor;
    return false;
}
1

First: You seem to have different spelling of wordcount/wordCount. Are you sure that's the code that you compiled and had valgrind on?

while (fscanf(fp, "%s", buffer) != EOF)

might better be

while (fscanf(fp, "%s", buffer) == 1)

as fscanf returns the number of successfully matched parameters. I wonder why the code you show does not lead to an infinite loop.

Adding a new node is overly complicated. You don't need to test for hashTable[nodeNumber] to be NULL, as it makes no difference if you create the node in a variable first and use newNode->next = hashTable[nodeNumber].

You don't fclose the dictionary file.

Now check is more complicated. Unless you include the case-ignoring part into your hash function, you'd need to lowercase the word before hashing/comparison.

| improve this answer | |

You must log in to answer this question.

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