0

I am having troubles with the check function in dictionary.c in pset5.

Here is my code:

/**
 * dictionary.c
 *
 * Computer Science 50
 * Problem Set 5
 *
 * Implements a dictionary's functionality.
 */

#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>

#include "dictionary.h"


/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word)
{
    node* cursor = root;
    bool isLastLetter = false;
    for (int i = 0; i < strlen(word); i++) {
        if (isLastLetter) {
            if (strcmp((word + i), "\'") == 0) {
                if (cursor->children[0] == NULL) {
                    return false;
                }
                else {
                    if (cursor->children[0]->isWord == true)
                        return true;
                }
            }
            else if (isupper((char)*(word + i))) {
            else if (cursor->children[(int)*(word + i) - 64] == NULL) {
                return false;
            }
            else if (cursor->children[(int)*(word + i) - 64]->isWord == true) {
                return true;
            }
            else {
                return false;
            }
            }
            else {
        else if (cursor->children[(int)*(word + i) - 96] == NULL) {
            return false;
        }
        else if (cursor->children[(int)*(word + i) - 96]->isWord == true) {
            return true;
        }
        else {
            return false;
        }
            }

        }
        else {
            if (strcmp((word + i), "\'") == 0) {
                if ((cursor->children[0]) == NULL) {
                    return false;
                }
                else {
                    cursor = cursor->children[0];
                }
            }
            else if (isupper((char)*(word + i))) {

            else if (cursor->children[(int)*(word + i) - 64] == NULL) {
                return false;
            }
            else if (cursor->children[(int)*(word + i) - 64] != NULL) {
                cursor = cursor->children[(int)*(word + i) - 96];
            }
            }
            else {
                            else if (cursor->children[(int)*(word + i) - 96] == NULL) {
                                return false;
                            }
                            else if (cursor->children[(int)*(word + i) - 96] != NULL) {
                                cursor = cursor->children[(int)*(word + i) - 96];
                            }
            }
        }
        if (i == strlen(word) - 2) {
            isLastLetter = true;
        }
    }

    return false;
}
void setLetter(node* cursor, int letter, bool is_w) {
    if (cursor->children[letter] == NULL) {
        cursor->children[letter] = malloc(sizeof(node));
        cursor->children[letter]->isWord = is_w;
        return;
    }
    cursor->children[letter]->isWord = is_w;
}

Here are my errors:

clang -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror   -c -o dictionary.o dictionary.c
 dictionary.c:38:17: error: expected expression
                else if (cursor -> children[(int)*(word + i) - 64] == NULL) {
                ^
 dictionary.c:49:17: error: expected expression
                else if (cursor -> children[(int)*(word + i) - 96] == NULL) {
                ^
 dictionary.c:72:17: error: expected expression
                else if (cursor -> children[(int)*(word + i) - 64] == NULL) {
                ^
 dictionary.c:80:17: error: expected expression
                else if (cursor -> children[(int)*(word + i) - 96] == NULL) {
                ^
 4 errors generated.
 make: *** [dictionary.o] Error 1

Please help me. Thank-you very much!

Jon

1
  • As a rule, at least at the begging of your coding journey, always add the curly braces for your if statements and for/while/do-while loops. It's going to save you a lot of trouble with bugs, and it's going to make your code much more readable, especially to you.
    – ChrisG
    Aug 5 '16 at 6:10
3

The problem is bad structuring of the chain of ifs and else ifs. Look at the following:

 if(...)
 {
     ...
 }
     else if (isupper((char)*(word + i))) {
         else if (cursor -> children[(int)*(word + i) - 64] == NULL) {

The first else if follows from the first if statement. There is an if statement, a block of code to execute when it is true, and then an else clause that follows when that first if statement is false.

Now comes the problem. The second if statement (which is the first "else if") opens a block of code to execute with the opening curly brace at the end of the line. The very first line of code inside that block is an "else" statement. An else statement must follow a block of code to execute, even if that block of code is nothing more than a semicolon. In this case, the last else statement is not associated with an if statement, so the compiler throws it out.

This happens again in each of the errors that follow. else statements cannot be blindly added to code. They must pair with an if statement.

This is probably a confusing description, but then again, the code is confusing the compiler too, so that says something in itself.

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

1
  • Thank-you very much!!
    – Jon
    Aug 6 '16 at 3:06

You must log in to answer this question.

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