0

where the error

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "dictionary.h"
#include <ctype.h>
#include<cs50.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 = 101;
long dicSize = 0;
bool loadRet = false;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
    int num = hash(word);
        for (node *c = table[num]; c != NULL; c = c->next)
        {
            if(strcasecmp(word, c->word) == 0)
            {
                return true;
            }
        }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    unsigned hashval;
    char w;
    for (hashval = 0; *word != '\0'; word++)
    {
        if (isupper(*word))
        {
            w = tolower(*word);
            hashval = w + 31 * hashval;
        }
        else
        {
            hashval = *word + 31 * hashval;
        }
    }
     return hashval % N;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    node *n;
    char word[LENGTH + 1];
    int number;
    FILE *f = fopen(dictionary, "r");
    if (f == NULL)
    {
        return 1;
    }
    while (fscanf(f, "%s", word) != EOF)
    {
        n = malloc(sizeof(node));
        if (n == NULL)
        {
            return 1;
        }
        strcpy(n-> word, word);
        n->next = NULL;
        number = hash(word);
        if (table[number] == NULL)
        {
            table[number] = n;
        }
        else
        {
        n->next = table[number];
        table[number] = n;
        }
        dicSize++;
    }
    dicSize = true;
    fclose(f);
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    if (dicSize == true)
    {
        return dicSize;
    }
    else
    {
        return 0;
    }
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    node *temp = NULL;
    for (int n = 0; n < 101; n++)
    {
        temp = table[n];
        for (node *current = table[n]; current != NULL; current = temp)
        {
            temp = temp->next;
            free(current);
        }
    }
    return true;
}

enter image description here

enter image description here

0
0

Same problem as before. Dictionary word count is always 1. The code is actually setting the count to 1, even after counting words.

dicSize = true;

true is interpreted as 1.

It looks like you're trying to use the word count var dicSize as both a counter and a bool. You can't do that. You need two separate vars to do that. However, you could simply test whether dicSize == 0 or !=0, depending on which you need.

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

You must log in to answer this question.

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