Bool method returns wrong value

Roman Lopez

I created a bool contains(string) method for a linked list hash table which checks if a value is in the hash. I use a helper function to recurse, but when the helper function returns false, the bool contains(string) still returns true. I ran it through the debugger and I can clearly see that it returns false, and I'm not sure why.

Here is the current node being searched:

"laccoliths"->"morbiferous"->"oculi"->"unscabbarded"

the value I'm searching for is "typung".

Here's the code:

bool contains_h(string x, node * p) //helper method
{
    if (p == NULL)
        return false;
    else if (x == p->data)
        return true;
    else
        contains_h(x, p->next);
}

bool contains(string word) { return contains_h(word, head); }

Andrew Shepherd

Nice simple one. You forgot to put 'return' on the final statement:

bool contains_h(string x, node * p) //helper method
{
    if (p == NULL)
        return false;
    else if (x == p->data)
        return true;
    else
        return contains_h(x, p->next);
}


And out of curiosity, I rewrote your code to a one-liner to see what it would look like:

bool contains_h(string x, node * p) //helper method
{
    return ((p!=NULL) && (x == p->data || contains_h(x, p->next)));
}

Personally, I would prefer to read your six lines. However, others might disagree, particularly because it would have avoided the missing return statement problem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Java equals() seemingly returns wrong bool value

From Dev

Java equals() seemingly returns wrong bool value

From Dev

MyApplication's method returns wrong value

From Dev

Method returns wrong value in main class

From Dev

If-Method returns wrong value after saving and loading object

From Dev

getrlimit() returns wrong value?

From Dev

getrlimit() returns wrong value?

From Dev

ArrayField returns wrong value

From Dev

Throwing method cannot be a member of an @objc protocol because it returns a value of type 'Bool'

From Dev

Assignment method created using define_singleton_method returns the wrong value

From Dev

dropdown get option value using option text using contains method jquery returns wrong value

From Dev

Method returning the wrong value

From Dev

Method returning wrong value

From Dev

CKContainer accountStatusWithCompletionHandler returns wrong value

From Dev

getMeasuredWidth returns totally wrong value

From Dev

SimpleDateFormat parse returns wrong value

From Dev

GetSystemMetrics and TScreen returns wrong value

From Dev

Android ListPreference returns wrong value

From Dev

OnitemClick returns the wrong string value

From Dev

Structure JNA returns wrong value

From Dev

SimpleDateFormat parse returns wrong value

From Dev

Recursion function returns wrong value

From Dev

"Select count()" returns wrong value

From Dev

Session bean returns the wrong value

From Dev

Math function returns wrong value

From Dev

Android ListPreference returns wrong value

From Dev

Addition returns wrong value in JavaScript

From Dev

BASH - Function returns wrong value

From Dev

Serilaizing in PHP returns wrong value

Related Related

HotTag

Archive