0

Struggling with the load function in speller for second week now. What I found using debug50 and placing "printf" between different lines of code is that the fscanf reads every second word in the dictionary. The counter that is supposed to sum the amount of words prints 4 instead of 7 that is actually in dictionary. Any thoughts what might be the problem here?

// Implements a dictionary's functionality
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <strings.h>
#include "dictionary.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 = 25;

// Hash table
node *table[N];

//declare global counter to keep track of words loaded
int counter = 0;

// Returns true if word is in dictionary, else false
bool check(const char *word)
{

    //hash word to see it's hash value
    //access linked list at that index
    //traverse linked list using strcasecmp
    return 0;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    unsigned int i;
    if (islower(word[0]))
    i = word[0] - 97;
    else
    i = word[0] - 65;
    // TODO
    return i;
}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    // TODO
    //open dictionary file
    FILE *input = fopen(dictionary, "r");
    if (input == NULL)
    {
        return false;
    }
    //read stringsfrom file one at a time
    char buffer[LENGTH + 1];
    while (fscanf(input, "%s", buffer) != EOF)
    {
    fscanf (input, "%s", buffer);
    //create new node for each word
    node *n = malloc(sizeof(node));
    strcpy(n->word, buffer);
    n->next = NULL;
    //hash word to obtain a hash value
    int a = hash(n->word);
    //insert node into hash table at that location
    if (table[a] == NULL)
    {
        table[a] = n;
        counter++;
    }
        else
        {
            n->next = table[a];
            table[a] = n;
            counter++;
        }

    }
    fclose(input);
    return true;
}

// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
    // TODO
    printf("%i\n", counter);
    return counter;
}

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    // TODO
    return false;
}
3

This while (fscanf(input, "%s", buffer) != EOF) executes an fscanf, followed immediately by this fscanf (input, "%s", buffer);, therefore it is processing every other word.

1
1

Remove the fscanf (input, "%s", buffer); inside of while (fscanf(input, "%s", buffer) != EOF)

3
  • Your answer would be better if it explained why this is the issue, as DinoCoderSaurus's does. This will not only provide reasoning to follow your answer, but help them learn/understand why the error was happening, even if it seems obvious to you. :) – Robert S. Pratt Mar 7 at 14:59
  • Thank you so much bro! – Alexander Alexandrov Mar 7 at 15:31
  • Sorry. I will take note of this when answering future questions – Nobody Mar 9 at 15:39

You must log in to answer this question.

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