I can't understand where and what my error is I think it's because of "load", but I can't fix it
it returns an error when checking
:) dictionary.c, dictionary.h, and Makefile exist
:) speller compiles
:| handles most basic words properly can't check until a frown turns upside down
:| handles min length (1-char) words can't check until a frown turns upside down
:| handles max length (45-char) words can't check until a frown turns upside down
:| handles words with apostrophes properly can't check until a frown turns upside down
:| spell-checking is case-insensitive can't check until a frown turns upside down
:| handles substrings properly can't check until a frown turns upside down
:| program is free of memory errors can't check until a frown turns upside down
// Implements a dictionary's functionality
#include <stdbool.h>
#include "dictionary.h"
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <cs50.h> #include <strings.h> #include <string.h>
FILE *file; int word_count;
// Represents a node in a hash table typedef struct node { char word[LENGTH + 1]; struct node *next; } node;
// Number of buckets in hash table const unsigned int N = 1;
// Hash table node *table[N];
// Returns true if word is in dictionary else false bool check(const char *word) { int chech_world = hash(word);
node *cursor = table[chech_world];
while (cursor != NULL)
{
if (strcasecmp(cursor -> word, word) == 0)
{
return true;
}
cursor = cursor->next;
}
return false;
}
// Hashes word to a number unsigned int hash(const char *word) { unsigned long hash = 5381; int c;
while ((c = *word++))
{
hash = ((hash << 5) + hash) + c;
}
return hash;
}
// Loads dictionary into memory, returning true if successful else false bool load(const char *dictionary) {
char word[LENGTH + 1];
file = fopen(dictionary, "r");
if (file == NULL)
{
// printf("Could not open %s.\n", dictionary);
return false;
}
while (fscanf(file, "%s", word) != EOF)
{
if (feof(file))
{
break;
// unload();
}
node *new_node = malloc(sizeof(node));
if (new_node == NULL)
{
return false;
}
strcpy(new_node -> word, word);
new_node -> next = NULL;
unsigned int index_word = hash(word);
new_node -> next = table[index_word];
table[index_word] = new_node;
word_count += 1;
}
fclose(file);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded unsigned int size(void) { return word_count; }
// Unloads dictionary from memory, returning true if successful else false bool unload(void) { for (int i = 0; i < N; i++) { node *cursor = table[i];
while (cursor != NULL)
{
node *temp = cursor;
cursor = cursor -> next;
temp = cursor;
free(temp);
}
}
return true;
}