0

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;

}

1
  • Your code does not compile,could you post your full code?
    – MARS
    Commented Jul 5, 2020 at 19:39

1 Answer 1

0

If the code doesn't compile for check50, none of the other tests will be executed. Did you change anything in speller.c, speller.h or dictionary.h? This will cause a compile fail because those files are not uploaded with dictionary.c. You should keep all the file mods to dictionary.c. Also, the function signatures in dictionary.c can't be changed either.

if this doesn't solve your problem, then please edit the question and post the full code.

1
  • no, I didn't change anything in the other files Commented Jul 6, 2020 at 11:27

You must log in to answer this question.

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