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

Recommended Answers

All 2 Replies

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.