0

My code seems to work fine with all text files. When I look at the errors it doesn't even show actual output, why could that be? https://submit.cs50.io/check50/e963f4484de14f0b73f8fca9a5e9431556cb649a

Here is the code, I would be very grateful for any suggestions as to what is causing the errors in check50.


// Implements a dictionary's functionality
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>

#include "dictionary.h"

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// TODO: Choose number of buckets in hash table
const unsigned int N = 26;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    int k = hash(word);
    for (node *ptr = table[k]; ptr != NULL; ptr = ptr->next){
        char *sa = ptr->word;
        int len = strlen(sa);
        if(len>0 && sa[len-1]=='\n'){
            sa[len-1] = '\0';
        }
        if(strcasecmp(sa, word)==0){
            return true;
        }
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO: Improve this hash function
    return toupper(word[0]) - 'A';
}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    // TODO
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        printf("Could not open dictionary\n");
        return 1;
    }
    int index = 0;
    char c, word[LENGTH + 1];
    int h;
    while (fread(&c, sizeof(char), 1, file)){
        word[index] = c;
        index++;
        if(c == '\n'){
            word[index] = '\0';
            index = 0;
            node *p = malloc(sizeof(node));
            strcpy(p->word, word);
            p->next = NULL;
            h = hash(word);
            if(table[h]==NULL){
                table[h] = p;
            }else{
                for (node *ptr = table[h]; ptr != NULL; ptr = ptr->next)
                {
                    // If at end of list
                    if (ptr->next == NULL)
                    {
                        // Append node
                        ptr->next = p;
                        break;
                    }
                }
            }
        }
    }
    word[index] = '\0';
    node *p = malloc(sizeof(node));
    strcpy(p->word, word);
    p->next = NULL;
    h = hash(word);
    if(table[h]==NULL){
        table[h] = p;
    }else{
        for (node *ptr = table[h]; ptr != NULL; ptr = ptr->next){
            // If at end of list
            if (ptr->next == NULL){
                // Append node
                ptr->next = p;
                break;
            }
        }
    }
    fclose(file);
    return true;
}

// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
    int g = 0;
    for (int i = 0; i < N; i++){
        for (node *ptr = table[i]; ptr != NULL; ptr = ptr->next){
            g++;
        }
    }
    return g;
}

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

1 Answer 1

1

It may have compiled, but it isn't working. Simply put, it's broken. When I ran the code, it generated a seg fault at line 100. (Is it possible that you posted the wrong source code?)

When I tried to go through the code to see what it was doing, it didn't make a lot of sense. First, the code reads in a single character to the char var "c". It doesn't read in word. In fact, it never reads in an entire word or stores anything in the string var "word".

Later, the code tries to process the word that should be stored in the string var "word", but no word was ever read in. Since word doesn't contain anything, using it later just leads to seg faults.

I suggest going back and reworking the code, maybe even redo the load function completely. It should read in an entire line from the input file to the string var "word". Each line contains a single word in the dictionary. Then, get a hash value for the word, copy the word to a node and add the node to the correct linked list in the dictionary, based on the hash value.

Don't try to make the code more complicated than needed. Just keep it simple. (There's far too more code here than needed.)

Give it another shot and if you have any issues, post a new question.

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

2
  • Mmhh I don't know, for me it doesn't just compile. If I put my own text in: Helo I m doing very good how are u? it tells me exactly what I would expect, misspelled words helo m u words misspelled 3, in dictionary 143091, and in text 9 The part of saving each char in c and then putting it in the word, is pretty much the same as the part that already comes in speller.c Commented May 23, 2023 at 21:10
  • Ok, nevermind, I found the solution. The problem actually IS that I dont read in words, but chars. For me it works both ways (obviously words is better), but check50 seems to have wanted specifically that. Thank you for your response! Commented May 23, 2023 at 21:36

You must log in to answer this question.

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