0

I'm stuck on the speller assignment but it feels like the smallest thing to be stuck on....in my hash function I keep getting "use of undeclared identifier" for n and i. Usually if this happens my curly braces are wrong, but I've been looking at this for a few hours now and I genuinely don't know what's wrong. I've also seen 3 other people use this same hash function and my code looks the same as their hash function, so I don't think it's that?

Here's just the hash function itself:

// Hashes word to a number
unsigned int hash(const char *word)
{
    unsigned int hash_value = 0;
    for (int i = 0; n = strlen(word); i < n; i++)
    
    //citation: hash function from Shrey Tripathi on Stackexchange.com
    {
        hash_value = (hash_value << 2) ^ word[i];
    }
    return hash_value % HASHTABLE_SIZE;
}

Here is my entire code - I recognize that not all of it is 100% correct, I haven't had a chance to check all of it because it won't compile due to my hash function errors.

[EDIT: Code unrelated to question was removed.]

0

The issue lies here:

for (int i = 0; n = strlen(word); i < n; i++)

There are actually two problems here, the undeclared var and the structure of the for statement. Both can be fixed with one change.

The problem is the first semicolon. It should be a comma.

When declaring multiple vars of the same type, whether in a for statement or as a separate line of code, the vars need to be separated by a comma. Since a semicolon was used, it broke the relationship between int and n, so n was not actually declared.

Also, because there's an extra semicolon inside the for statement, it's going to give a syntax error.

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

You must log in to answer this question.

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