0

When I try to create my code I get the following errors:

./dictionary.h:26:29: error: expected ';' after top level declarator

./dictionary.h:30:6: error: expected identifier or '('

./dictionary.h:33:1: error: expected identifier or '('

./dictionary.h:137:1: error: expected identifier or '('

I really don't understand what I should do about this. I know my code isn't finished but I want to clean this up before I proceed.

/**
 * Declares a dictionary's functionality.
 */

#ifndef DICTIONARY_H
#define DICTIONARY_H
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

// maximum length for a word
// (e.g., pneumonoultramicroscopicsilicovolcanoconiosis)
#define LENGTH 45

// Count
int wordcount = 0;

// Define trie
typedef struct trie
{
    bool correct;
    struct trie* paths[27];
}
trie;

void freetrie(trie *current)

// Start the root of the trie
struct trie root;
trie &root = *malloc(sizeof(trie));

// Check if there is enough memory
if (root == NULL)
{
    fprintf(stderr, "Ran out of memory\n");
    return false;
}
trie root = {false,{NULL}};

/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char *word);}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char *dictionary)
{
    // Open dicitionary
    FILE *dict = fopen(dictionary, "r");

    // Check dictionary
    if (dict == NULL)
    {
        fprintf(stderr, "Could not open %s\n", dictionary);
        return false;
    }

    // Init word & letter
    char word[LENGTH+1];
    int letter = 0;

    // spell-check each word in text
    for (int c = fgetc(dict); c != EOF; c = fgetc(dict))
    {
        // Load word from dict into word
        for (int i = 0; i<strlen(word); i++ )
        {
            word[letter] = c;
            letter ++;
        }
        wordcount ++;

        // Make sure the word ends
        word[letter] = '\0';

        //Start current
        struct trie* current = &root;

        for (int i=0, j = strlen(word); i<j; i++)
        {
            int c = atoi(&word[i]);
            if (c == 39 || c == 34)
            {
                c = 26;
            }
            else
            {
                c = c - 97;
            }

            if (current -> paths[c] == NULL)
            {
                struct trie* new_trie = malloc(sizeof(trie));
                if (new_trie == NULL)
                {
                    fprintf(stderr, "Ran out of memory\n");
                    return false;
                }
                current->paths[c] = new_trie;
                current = new_trie;
                current -> correct = false;
            }
            else 
            {
                current = current -> paths[c];
            }
        }
        current -> correct = true;

        // Prepare for next word
        letter = 0;
    }
}


/**
 * Returns number of words in dictionary if loaded else 0 if not yet loaded.
 */
unsigned int size(void)
{
    if (wordcount)
    {
        return wordcount;
    }
    else
    {
        return 0;
    }
}

/**
 * Unloads dictionary from memory.  Returns true if successful else false.
 */
bool unload(void);
{
    for (int i = 0; i<27; i++)
    {
        if (root.paths[i] != NULL)
        {
            freetrie(root.paths[i]);
        }
    }
}

void freetrie(trie *current)
{
    for (int i=0;i<27;i++)
    {
        if (current->paths[i] != NULL) 
        {
            freetrie(current->paths[i]); 
        }
    }
    freetrie(current);
 }

#endif // DICTIONARY_H

1 Answer 1

0

Since this function declaration void freetrie(trie *current) has no body, compiler is expecting a prototype, which would end with ;. Some of the errors cascade from there. This line bool check(const char *word);} is a problem too. Remember, from the spec, the functions should be implemented in dictionary.c, not in dictionary.h.

You must log in to answer this question.

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