0

I have written the load code for pset6 which compiles but when I execute speller.c it returns "Could not load dictionary". I have tried using a homemade dictionary on my machine and the dictionary at home/cs50/pset6/dictionaries/large.

I cannot workout where the error is and am reaching the tearing hair out phase! I have tried running it with gdb, but gdb does not seem to show anything not directly in the speller.c program (no doubt due to user error?!). My code is below. Any help would be massively appreciated.

Many thanks!

//define hashtable
node* hashtable[table_size];

//open dictionary file
FILE* d = fopen(dictionary,"r");
if(d == NULL);
{
    printf("Could not load %s\n",dictionary);
    return false;
}
//initialise hash table
for(int i = 0; i < table_size; i++)
{
    hashtable[i] = NULL;
}

//create new node
node* new_node = malloc(sizeof(new_node));
if(new_node == NULL)
{
    return false;
}

//scan in word from file
while(fscanf(d,"%s", new_node->word) == 1)
{
    int index = hash_gen(new_node->word);

    //curr and previous pointers
    node* curr = hashtable[index];
    node* prev = NULL;

    if(hashtable[index] == NULL)
    {
        new_node->next = NULL;
        hashtable[index] = new_node;
    }
    else
    {
        while(curr != NULL);
        {  
            //if appears before next word on the list
            if(strcmp(curr->word, new_node->word) > 0)
            {
                //inset before
                new_node->next = curr;

                if(prev == NULL)
                {
                    hashtable[index] = new_node;
                }
                else
                {
                    prev->next = new_node;
                }
                return true;
            }
            //if word appears after this node
            if(strcmp(curr->word, new_node->word) < 0)
            {   //go to next node
                prev = curr;
                curr = curr->next;
            }   //update pointers to previous and next

            //if word already in the dictionary, do nothing
            if(strcmp(curr->word, new_node->word) == 0)
            {
                free(new_node);
                return false;
            }
        }
    }
    //at the end of the list
    new_node->next = NULL;
    prev->next = new_node;

    free(new_node);
    return true;
}

}

2

Try removing the semicolon after your if statement.

if(d == NULL)
{
    printf("Could not load %s\n",dictionary);
    return false;
}
| improve this answer | |
  • What a great spot - thank you so much! I can't believe I missed it (and spent 3 hours tearing my hair out over it!) – AlastairHoward Dec 16 '14 at 22:49
  • It's always the little things that cause the most trouble, isn't it! – lethaljd Dec 16 '14 at 23:10
0

there is also an extra semicolon after while statement.

 else
{
    while(curr != NULL);
    {  
        //if appears before next word on the list
        if(strcmp(curr->word, new_node->word) > 0)
        {
| improve this answer | |

You must log in to answer this question.

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