0

I've been struggling terribly w/speller. Most recently the check function. I'm troubled by how convoluted my code looks because I see it as indicative of convoluted logic.

I most recently generated a 'control may reach end of non-void function' error, but thanks to the fantastic diagram in How to solve “control may reach end of non void function” error, can see my code suffers from the issues it mentions.

I resolved the error by adding a return false; statement at the end, but agree it feels sloppy. Here's the code:

bool check(const char *word)
{
    trav = root;

    // variable to store alphabetic index of 'word[i]'
    int ind_abc = 0;

    // for each letter in input word
    for (int i = 0; word[i]!= '\0'; i++)
    {
        // convert word[i] to int
        int ind_abc = atoi(&word[i]);

        // get alphabetical index for *word[i]
        alpha(&ind_abc);

        // if NULL, word is misspelled
        if (trav->branch[ind_abc] == NULL)
        {
            return false;
        }

        // move to next letter in trie
        else
        {
            trav = trav->branch[ind_abc];
        }
    }

    // end of 'word' reached...
    if (word[ind_abc] == '\0')
    {
        if (trav->is_word == true)
        {
            return true;
        }
    }

    return false;
}

It made me begin to wonder whether I could cut out the if (word[ind_abc] == '\0') loop and replace it with return trav->is_word because return trav->is_word is a bool. I argued myself out of it on the basis it was necessary to check whether word[ind_abc] = '\0' and thereafter if (trav->is_word == true), but is that really the case?

The code does compile when check returns trav->is word, but I want to follow what's considered good practices and am uncertain whether this practice is ok or there's some fallout I'm too inexperienced to recognize.

FYI alpha() is a custom function I wrote to calculate the alphabetic index of a value and I'm shaky on whether I implemented atoi() in this context, so feel free to critique any issue you find. Thank you for any help re: this.

0

It's not nearly as "sloppy" as you think. Consider what you have to do in check. The code has to walk through the trie to the end of the word. If it doesn't get to the end, return false. Once it gets there, return true or false based on is_word. That's exactly what you're doing.

Is returning trav->is_word a best practice? Absolutely! The function returns a book, is_word is a bool, and it's the value that you're trying to check!

As for the return false; at the end, that too is a best practice. It's the catchall in case something unforeseen gets by and it returns a failed check, so no false successes.

The one thing that could be cleaned up is the conversion from char to int using atoi(). While it does work, it isn't necessary. A char can be treated as a single byte integer as-is, and you can do math on it. So, there's no reason you couldn't have a char as the input to your alpha function, and return an int. Also, if you really felt compelled to use the atoi function, you could still pass a char to the function and do the atoi conversion inside the function. Think about it, you are doing the conversion EVERY time you call the function. WHy not move it inside the function and write the code once. ;-)

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

1
  • Thanks so much for the guidance. Made me see the problem is w/my alpha function. Have made progress, but there's a whole new series of errors to resolve.
    – Lindsey
    Oct 7 '17 at 8:36

You must log in to answer this question.

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