0

I've been working on speller (implementing a hashtable) for quite a while now and I'm stuck...my code compiles but I'm assuming there's a run-time error. I'm not sure what to make of my error message, and I'm not sure my logic is even correct; could someone take a look at my code and explain what I need to improve upon, as well as decipher the error? Help is very much appreciated.

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cs50.h>

#include "dictionary.h"

#define N 26

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

// Represents a hash table
node *hashtable[N];

// Hashes word to a number between 0 and 25, inclusive, based on its first 
letter
unsigned int hash(const char *word)
{
    return tolower(word[0]) - 'a';
}

// Keeps track of number of words
int word_count = 0;

int loaded = 1;

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // Initialize hash table
    for (int i = 0; i < N; i++)
    {
        hashtable[i] = NULL;
    }

    // Open dictionary
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        unload();
        return false;
    }

    // Buffer for a word
    char word[LENGTH + 1];

    for (int i = 0; i < N; i++)
    {
        while ((fscanf(file, "%s", word) != EOF) &&
        (hash(word) == i))
        {
            // Creates a new node
            node *new_node = malloc(sizeof(node));
            if (new_node == NULL)
            {
                unload();
                return false;
            }

        // Copies the word into the word portion of the node, increments 
           number of words
            strcpy(new_node->word, word);
            word_count++;
        }
    }

    fclose(file);
    loaded = 0;
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    return word_count;
}

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    unsigned int i = hash(word);

    node *cursor = hashtable[i];
    while (cursor != NULL)
    {
        if (strcasecmp(word, cursor->word) == 0)
            return true;
        else
            cursor = cursor->next;
    }

    // If the word doesn't show up, then it is incorrect.
    return false;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    for (int i = 0; i < N; i++)
    {
        node *cursor = hashtable[i];
        while (cursor != NULL)
        {
            node *temp = cursor;
            cursor = cursor->next;
            free(temp);
        }
    }

    return true;
 }

There are no syntactical errors, I don't think, but when I execute "make dictionary" this happens, after a bunch of repetitive lines are printed:

(.text+0x20): undefined reference to `main' clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [dictionary] Error 1

Any help is very much appreciated. Thank you!

enter image description here

1 Answer 1

1

Are you trying to make dictionary or make speller?

You can't make dictionary because it's not a complete program.

Yet, you say that it will compile. It makes me wonder if it compiles locally, but won't compile when submitted to check50. In that case, I would ask this: Did you change anything other than dictionary.c? IF you changed speller.c, that's a big fail. Can't alter that file.

If something else is failing, please edit the question and post an actual screen capture of what's happening.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

7
  • Looks like another novice mistake on my part...what do I do instead?
    – Sean Cheng
    Apr 10, 2019 at 1:40
  • I've added a picture of what's happening. I never touched speller.c, and I haven't tried submitting it to check50...dictionary.c was the only thing I've touched.
    – Sean Cheng
    Apr 10, 2019 at 1:44
  • Like I said, you absolutely can't make dictionary. It's not a complete program. Review the pset instructions, especially the part that says make speller to compile. You also said that it would compile. This was not an accurate statement.
    – Cliff B
    Apr 10, 2019 at 1:59
  • I just read it and I understand now...thanks! Could you give me some pointers on why my program isn't loading?
    – Sean Cheng
    Apr 10, 2019 at 2:00
  • It's simple. I've said it twice now! You can't make dictionary! It's not a complete program. dictionary.c is part of a larger program, speller!
    – Cliff B
    Apr 10, 2019 at 2:01

You must log in to answer this question.

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