I took a bit of a break from cs50 and having come back to it I realised that there is a problem with my load function in pset5. Although the program tells me correctly which words are misspelled, the WORDS IN DICTIONARY output is always 0. The program also doesn't pass check50, presumably because of that. When I run the program through with GDB it seems to me that it works fine, so I'd be very grateful if someone could look over my load function and give me a hint as to what I'm doing wrong:

* Loads dictionary into memory.  Returns true if successful else false.
  bool load(const char* dictionary)
   // initialise index for hash function --> use later
   int index = 0;
   int word_count = 0;
   // create an array to put the read word into
   char word[LENGTH + 1] = {};

   // open the dictionary file to read from
   FILE* fp = fopen(dictionary, "r");
   if(fp == NULL)
       return false;
       // repeat until end of file
           //build new node
           node*new_node = malloc(sizeof(node));

           // read in word from dictionary

           // put the word inside node
           strcpy((new_node->word), word);

           // use a hash function to map word to index in the hash-table array
           index = hash_function(word);

           // if the index pointer is empty, we need to make the new node the head of the list
           if(hashtable[index] == NULL)
               hashtable[index] = new_node;
               new_node->next = NULL;
           // if a node with that index already exists, go down the linked list
               node* cursor = hashtable[index];

               while(cursor->next != NULL)
                   cursor = cursor->next;

               cursor->next = new_node;
               new_node->next = NULL;



   return true;

1 Answer 1


Simple problem. Your word_count is declared locally in the load() function. As soon as you leave load, it disappears. It does not exist outside of load.

The word count is displayed in the call to size(). I'm guessing that you declared word_count again there. That is a different variable and is probably still 0.

This is a case where a global variable is appropriate. If word_count were global, it would persist from load() to size() and anywhere else it is used.

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

  • Of course, this is fixed now. However, check50 still rejects my code (e.g. it says that the program isn't case insensitive) but when I run it with my own inputs (and GDB) everything seems fine. Do you think you can help with that? I'm really at wit's end here :( Commented Oct 10, 2015 at 20:16
  • It's saying that you're not dealing with mixed case versions of words. Have you tested that "cat", "CAT" and "cAt" would all be counted as correctly spelled words against a dictionary that has only "cat"?
    – Cliff B
    Commented Oct 10, 2015 at 20:25
  • Yup, case insensitivity works. It only seems that the load function saves 45 characters-long strings as two different words? I definitely left enough space in the 'word' array to accommodate the 45 charatcers plus the null terminator and I really don't know why that's happenning. Commented Oct 11, 2015 at 11:57

You must log in to answer this question.

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