1

Can you please help with the following issue with speller from pset5 in cs50:

When I run the cs50 check, I get that it does not handle basic words properly, I am guessing, my check function is not working properly:

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

#include "dictionary.h"

#define ALPHABET_SIZE 26
#define ASCII_OFFSET 97
#define MAX_WORD_SIZE 45

bool load(const char* dictionary)
{

    FILE* dictionaryFile = fopen(dictionary, "r");
    if (dictionaryFile == NULL)
    {
        printf("Could not open file.\n");
        return false;
    }

    first = createNode();

    unsigned int c = 0;
    node* nextNode = first;

    while (c != EOF)
    {
        c = fgetc(dictionaryFile);

        if (c != '\n' && c != EOF)
        {
            if (c == '\'')
                c = ALPHABET_SIZE + ASCII_OFFSET;

            if(nextNode->alphabet[c - ASCII_OFFSET] == NULL)
            {
                nextNode->alphabet[c - ASCII_OFFSET] = createNode();
            }
            nextNode = nextNode->alphabet[c - ASCII_OFFSET];
        }
        else if (c == '\n')
        {
            nextNode->exists = true;
            dictionarySize++;
            nextNode = first;
        }
    }
    fclose(dictionaryFile);
    return true;
}
10
  • Can't tell yet. Can you please edit the question and add your load function?
    – Cliff B
    Nov 13 '16 at 23:37
  • Hi Cliff! Is the code of load() properly displayed now? Please, advise how do I need to edit the question, thank you!
    – SnezhinaT
    Nov 14 '16 at 6:43
  • No, the load function has not been added yet. Clickc on "edit" above and paste in the missing code. Once you've pasted it in, highlight the added code (if it isn't already) and click on the curly braces { } above to identify it and format it as code.
    – Cliff B
    Nov 14 '16 at 6:46
  • Done! thanks for the tip!
    – SnezhinaT
    Nov 14 '16 at 6:50
  • still no load code.
    – Cliff B
    Nov 14 '16 at 6:51
0

THIS IS NOT AN ANSWER TO THE QUESTION. BUT THERE'S NO WAY TO EASILY FORMAT BLOCKS OF CODE AND PARAGRAPHS ON COMMENTS.

I debugged your load() function and I don't see anything that could be wrong in this particular function. Assuming your createNode() function is something like this:

node* createNode(){
    return malloc(sizeof(node));
}

You might have more code to force the boolean of the node to false and each pointer of the array of pointers to NULL. There is no need to do that though. That won't change the functionality of the program but it will take some unnecessary time from your efficiency.

Assuming also that your node declaration looks something like this:

typedef struct node{

    struct node* alphabet[ALPHABET_SIZE];
    bool exists;

}node;

And that you have these lines somewhere on top:

int dictionarySize = 0;
node* first;

You must log in to answer this question.

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