Home > Back-end >  My program of speller.c is not compiling pset5
My program of speller.c is not compiling pset5

Time:12-26

When I run my program i get these 2 errors and not sure why

The error

dictionary.c:87:9: error: use of undeclared identifier 'words_counter'
        words_counter  ;
        ^
dictionary.c:97:12: error: use of undeclared identifier 'words_counter'
    return words_counter;
           ^
2 errors generated.
make: *** [Makefile:3: speller] Error 1

The code

// Implements a dictionary's functionality

#include <stdbool.h>

#include "dictionary.h"
#include <stdlib.h>
#include <strings.h>
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>


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

// Number of buckets in hash table
const unsigned int N = 1500;

// Hash table
node *table[N];

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    // TODO
    int index = hash(word);
    node *cursor = table[index];
    while (cursor != NULL)
    {
        if (strcasecmp(word, cursor -> word) ==0 )
        {
            return true;
        }
        //Move cursor to next node
        cursor = cursor -> next;
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO
    // Ref of hash function: https://cs50.stackexchange.com/questions/38753/cs50-good-hash-function-for-pset5-speller/38755
    int hash = 600;
    while (*word != '\0')
    {
        hash = ((hash << 4 )   (int) (*word)) % N;
        word  ;
    }
    return hash % N;
}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    // TODO
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        return false;
    }
    // Read strings from file
    // define word
    char word [LENGTH   1];
    int index;
    while(fscanf(file, "%s", word) != EOF)
    {
        // Creating new node
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            return false;
        }
        // Copies word to the node
        strcpy (n->word, word);
        // Obtaining index to hash word
        index = hash(word);
        n -> next = table[index];
        table[index] = n;
        words_counter  ;
    }
    fclose(file);
    return true;
}

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

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    // TODO
    //Free nodes in each linked list
    for (int i = 0; i<N; i  )
    {
        node *cursor = table[i];
        node *tmp = cursor;
        while (cursor != NULL)
        {
            cursor = cursor-> next;
            free (tmp);
            tmp = cursor;
        }
    }
    return true;
}

CodePudding user response:

Your words_counter is used as the global variable in your code, so define it in the same place as other global objects:

const unsigned int N = 1500;
unsigned words_counter;
  • Related