0

I have one more questions about SPELLER. I've completed all my codes for load, check, size and unload. I'm now trying to debug all of them. It appears I have an error with my check function, which one is suppose to return true or false. I think this error is linked to the numerous false statements. Probably, confusion is there.

Actually I thought all cases were covered and if any of the returns were reached in the loop, the last false statement would be return. Should I rewrite my code in a way only one choice is made at the end of the loop between returning true or returning false?

Please, someone can explain me why this fault occur. I want to continue debugging my programs but now I'm just turning in circle.

There, you will find my code for CHECK and a print screen of my prompt command trying to run speller in gdb :

bool check(const char* word)
{
// initialize variable
int index = 0;

// Processing throughout word
for (int i = 0, n = strlen(word); i <= n; i++)
{
    // pointing current to root
    current = root;

    if (isalpha(word[i]))
    {
        if (i == 0)
        {
            index = tolower(word[i]) - 'a';
            current = root->children[index];

            if (root->children[index] == NULL)
            {
                return false;
        }

        else
        {
            index = tolower(word[i]) - 'a';
            current = current->children[index];

            if (current->children[index] == NULL)
            {
                return false;
            }
        }
    }

    else if (word[i] == '\'' && i > 0)
    {
        current = current->children[26];

        if (current->children[26] == NULL)
        {
            return false;
        }
    }

    else if (word[i] == '\0' && i == n)
    {
        // if is a completed word
        if (current->is_word == true)
        {
            return true;
        }

        else return false;
    }
}

return false;
}
}

and there, print screen of my prompt :

enter image description here

3
  • P.S. I have solved my problem running speller in gdb, if anyone have the same issue, just ask, it will be my pleasure to explain. – CharlesD89 Nov 12 '15 at 2:41
  • How about posting it as an answer instead? In about 2 days, the system will let you accept your own answer thus removing it from the unanswered question pool. thanks. – Cliff B Nov 12 '15 at 3:50
  • I didn't found the answer to my question, I'm just saying that I solved my issue for the one appearing in my print screen, the one related with the execution of speller in gdb. My question is concerning my compiling error "control may reach end of nun-void function". Can you answer to that? @CliffB – CharlesD89 Nov 12 '15 at 13:30
1

At the bottom of your code, there is a place where your function will not return anything. That's probably around line 93 in your program, the line that clang complained about.

enter image description here

Now you need to follow the logic, either on paper or with gdb, to see what conditions(s) will get you to that spot. One pair of braces in your code stands out because of their alignment. I leave it to you to determine if that is contributing to your problem. And to be clear, adding a return true; or return false; at that point in the program will "fix" the compile error. However, you really need to evaluate all your if/else/else if conditions to make sure the function works as you intend and returns the correct result.

2
  • Thanks @DinoCoderSaurus, I'll work on it! – CharlesD89 Nov 12 '15 at 15:34
  • I found! I think I had this error because in the case which my for loop reaches his breaking condition, any return statement would be found. So I have to add one after the loop. But in theory, this statement is not supposed to be reach. Actually it's just to satisfy the compiler. Great thanks @DinoCoderSaurus – CharlesD89 Nov 13 '15 at 0:25

You must log in to answer this question.

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