0

I've been banging my head for the last few hours as to why my dictionary function only saves one word from the whole dictionary. Even though debug50 and printf shows different hashes for different words, which are stored in different pointers, check50 still shows only 1 word in the dictionary. I would be very grateful if someone could lead me on the right track!

[EDIT - unrelated code removed.]

// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
    // Checks if glob_dict_no greater than 0
    if (glob_dict_no > 0)
    {
        return true;
    }
    return false;
}
2
  • Couldn't replicate the problem. It seems that your problem lies elsewhere in your code. Can you please post the missing parts?
    – Cliff B
    Mar 29 at 22:08
  • Thanks for your time! I added the whole code now. Mar 30 at 4:36

1 Answer 1

1

This is an easy one to sort out. Look at the code for size(). It will either return true or false. (it will NOT return the actual count stored in glob_dict_no.)

BUT, it has to return an integer, so it casts true/false to an integer. False is always cast to 0 and true to 1. That's why it appears that the code is only loading one word in the dictionary. No matter how many words are loaded, the return true; statement will always result in 1 being returned (or 0 if there are no words in the dictionary.)

Instead, why not just return glob_dict_no;??? It's initialized to 0, so if no words are loaded, it's already 0. Otherwise it contains the actual word count! No need for any tests or any other code in the size() function at all. It's that simple! ;-)

There's a hidden lesson here. Look for the simplest ways to do a task. Don't write needlessly complex code when a simple statement does the job! Anytime you write code, ask yourself if there's a simpler way to do the same thing. It's a hard lesson for most programmers to learn. I will often go back and simplify code after I have it working. ;-)

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

1
  • Thanks you very much for your help, I didn't think it would be that simple! :-) Mar 30 at 15:34

You must log in to answer this question.

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