0

I've been working a few days in speller and I've managed to make it work exactly like the cs50 staff's (or at least it looks exactly) But check50 says everything is wrong except compiling:

:) dictionary.c, dictionary.h, and Makefile exist

:) speller compiles

:( handles most basic words properly expected "MISSPELLED WOR...", not "MISSPELLED WOR..."

:( handles min length (1-char) words expected "MISSPELLED WOR...", not "MISSPELLED WOR..."

:( handles max length (45-char) words expected "MISSPELLED WOR...", not "MISSPELLED WOR..."

:( handles words with apostrophes properly expected "MISSPELLED WOR...", not "MISSPELLED WOR..."

:( spell-checking is case-insensitive expected "MISSPELLED WOR...", not "MISSPELLED WOR..."

:( handles substrings properly expected "MISSPELLED WOR...", not "MISSPELLED WOR..."

:| program is free of memory errors can't check until a frown turns upside down

this is the code I'm using:

check:

bool check(const char *word) 

 int h = hash(word);

        node *tmp = table[h];

       while (true)
        {

            if (strcasecmp (word, tmp->word) == 0)
            {
                return true;
            }

            else if (tmp->next != NULL)
            {
                tmp = tmp->next;

            }
            else if (tmp->next == NULL)
            {
                return false;
            }

        } 

return false; }

hash:

unsigned int hash(const char *word) 

{ //Hash function "djb2" by Dan Bernstein

unsigned long hash = 5381;
int l = strlen(word);

char lowerword[l+1];

for (int i = 0; i<l; i++)
{
    lowerword[i] = tolower(word[i]);
}

for (int i = 0; i<l; word++, i++)
{
    hash = ((hash<<5) + hash) + *lowerword;
}

if (hash >= N)
{
    return hash % N;
}
else
{
    return hash;
}

}

load:

bool load(const char *dictionary)

{

char word[LENGTH+1];

int h = 0;

FILE *file = fopen(dictionary, "r");

if (file == NULL)
{
    printf ("Could not load\n");

    return 1;
}

while (fscanf (file, "%s", word) != EOF)
{
    node *n = malloc(sizeof(node));

    if (n == NULL)
    {
        printf ("Could not load\n");

        return 1;
    }

    strcpy(n->word, word);

    sizetable++;

    n->next = NULL;

    h = hash(word);

    node *tmp = table[h];

    n->next = table[h];

    table[h] = n;
}

fclose (file); return true; }

unload:

node *clear = NULL;

node *tmp = NULL;

for (int i = 0;i

clear = table[i];

while(clear->next != NULL)
{
        tmp = clear;

        clear = clear->next;

        free(tmp);

}
tmp = clear;

free(tmp);

}

return true;

sorry for that for loop, I can't put it right, here is the rest:

for (int i = 0;i lower than N;i++)

that´s in detail what check50 says:

https://submit.cs50.io/check50/cccb469293f9de280e1a76400c9d0d645982859d

I would truly appreciate any help, I'm so close now but I can't find the last mistake. Thanks!

1
  • Hi Alenjandro... Could you please edit your post to cleanly paste your dictionary.c? It's hard to work with this. Thanks.
    – user3671
    Jun 10 '20 at 11:38
3

There is a clue in the check50 results. Suspect your testing is on the large dictionary ("it work exactly like the cs50 staff's").

Try the program with the small dictionary. Chances are it will give a seg fault. A good test scenario is ./speller dictionaries/small texts/cat.txt).

That is the usual cause when check50 reports program output as "MISSPELLED WORDS" and no other report data. That also indicates it is giving seg fault in check (because speller outputs MISSPELLED WORDS before it calls check).

Hint: The problem happens when table[h] is NULL. debug50 with the "small" test scenario should help you narrow down the problem.

2
  • DinoCoderSaurus thank you sooooo much, Not only I've solved a problem that was difficuly for me but you've made find and understand the error. Thanks God there is people like you on internet for the rookies. You're awesome men! Jun 11 '20 at 9:30
  • Thank you both SO MUCH for (1.) adding this thread, I had exactly the same issue, and (2.) giving the alternative test scenario, it helped me a lot. Thanks!!!
    – Antoine S.
    Jul 10 at 11:33

You must log in to answer this question.

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