0

Can someone give me feedback on my code please? I can't compile this, and it says too many errors. A lot of the errors don't seem to make sense. For example, it says "error: use of undeclared identifier 'LENGTH'" even though #LENGTH is defined in the dictionary.h file.

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

#include "dictionary.h"

//create node struct
typedef struct node
{
    //declare value word
    char word[46];
    //declare next pointer
    struct node *next;
}
node;

//create hashtable
node* hashtable [100];

//declare counter for words in dictionary
int count = 0;

//create a hashing function, credit to delipity(staff) on cs50 reddit page
int hash(char* needs_hashing)
{
    unsigned int index = 0;
    for (int i=0, n=strlen(needs_hashing); i<n; i++)
        int num = (hash << 2) ^ needs_hashing[i];
    return index % sizeof(hashtable);
};

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // TODO
    //get the length of the word
    int length = strlen(word);
    //declare text string
    char text[length + 1];
    //set last char of text to \0
    text[length] = '\0';

    //loop through word's letters
    for(int i = 0; i < length; i++)
    {
        //change word's letter to lowercase and assign those to letters of text
        text[i] = tolower(word[i]);
    }

    //get an index for text from hash fx
    int index = hash(text);

    // assign cursor node to the first node of the linked link
    node *cursor = hashtable[index];

    //as long as cursor has a value
    while(cursor != NULL)
    {
        //compare text and word
        if (strcmp(text, word)) = 0)
        {
        return true;
        //move cursor to next word
        cursor = cursor->next;
        }
    }
    return false;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // loop through hash table and make all elements NULL
    for(int h = 0; hash = sizeof(hashtable); h < hash; h++)
    {
        hashtable[h] = NULL;
    }

    //create and open dictionary file
    *FILE dict = fopen(dictionary, "r");
    if(dict == NULL)
    {
        fprint("The dictionary could not be opened.");
        return 1;
    }

    //create word array
    char word[LENGTH + 1];
    //read dictionary 1 word at a time
    while(fscanf(dict, "%s", word, !=EOF));
    {
        //create node for each word
        node* newnode = malloc(sizeof(node));
        //check for memory
        if(newnode == NULL)
        //copy word into the new node
        {
            unload();
            return false;
        }

      ` //copy a word into newnode
        strcpy(newnode->word, word);

        //first point newnode to the head or start of array
        newnode->next = hashtable[index];
        //now place newnode as the new head at beginning of array
        hashtable[index] = newnode
        //count the word
        count++;
    }

    //close dictionary
    fclose(dict);
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    // TODO
    if(load)
    {
        return count;
    }
    return 0;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    // TODO
    //loop through length of hashtable
    for(int h = 0, h < sizeof(hashtable), h++)
    {
        //set cursor pointer to 1st index of hashtable
        node* cursor = hashtable[h];
        //traverse the index's linked list
        while (cursor != NULL)
        {
            //assign cursor to temp node
            node* temp = cursor;
            //reset curor to the next node
            cursor = cursor->next;
            //free the temporary node
            free(temp);
        }
    }

    return true;
    //reset the count
    count = 0;
}
  • You were right. I got through it. :) Now I'm working on make with all the files. – Marie Urbina Jun 21 '18 at 0:13
  • Great! You also learned a lot along the way, right? Kinda reinforces your command of syntax? My usual reaction when this occurs? "OK, that happened." Now, go work on the real problems! ;-) – Cliff B Jun 21 '18 at 0:27
  • Yes, I'll work on patience. I got thrown off by 1 or more of the errors that were real problems,like the one I mentioned above, which I figured out later, and so I thought there must be a bigger problem. But yes it was mostly small syntax bugs. – Marie Urbina Jun 22 '18 at 19:53
0

The compiler is right, there are too many errors. Once the compiler hits 20 errors, it stops, even though there may be more issues.

In this case, most of the errors are syntax errors - too many semicolons in for loop setups, declarations of vars inside for loops that should have been done before the loop, etc. etc. Some of these errors are causing subsequent errors.

There are simply too many errors to address in this forum in one question. If you have a specific issue, then please post a specific question.

My advice is to start with the first error and work through them sequentially. Remember, some errors cause later errors. It'll be good practice in developing your debugging skills.

This looks a lot like my first pass after writing a significant block of code, when I usually find all the syntax errors that I made. Here's where I fix all the typos, the misuse of functions (missing or extra parameters, etc.) and all the stuff I did wrong because I was in too big a hurry or too tired. It's not something that should be intimidating or cause for panic, just something to work through and part of the process. ;-)

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

| improve this answer | |

You must log in to answer this question.

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