954,202 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Hash Table display problem

I wrote a hash table that takes a char as a key and for a test I am using a string for my value. It uses a linked list. But I am having a problem displaying the value. I have a function that displays the key correctly but the value is displayed as garbage. I am prety sure it is this one method that is causing it to display incorrectly but it complies and runs fine, i am just not sure why this does not work. Here is the method I think is not working and I am attaching the entire sample program as a txt file if someonecan take a look. I a little stuck on this.

bool Hashtable::get(char *key, string value)
{
    METADATA* temp = find(key);
    if(temp == NULL)
    {
        value = "";
        return false;
    }
    else
    {
        value = temp->value;
        return true;
    }
}

Here is my Display Function and it calls this function so I am assuming that for some reason the data is not bein copied correctly.

void displayAll(Hashtable *hashtable)
{
    char key[SIZE_KEY];
    char value[SIZE_VALUE];
    cout<< "\nCurrent nodes in hashtable: " << endl;
    hashtable->initIterator();
    while(hashtable->hasNext())
    {
        hashtable->getNextKey(key);
        hashtable->get(key, value);
        cout << "key: " << key << "\tvalue : " << value << endl;

    }
}

Thanks

Attachments HASHTBALE_Test.txt (4.97KB)
blazted
Newbie Poster
17 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

value is an array. When you pass it as the second argument to get, it acts as the initializer for the std::string parameter. You've lost your reference, and any changes to the parameter will not be reflected back in displayAll. Change value to a string and pass it by reference to get.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Ahh thank you. I did that and it worked perfectly.

blazted
Newbie Poster
17 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You