943,985 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2141
  • C++ RSS
Jun 1st, 2007
0

Hash Table display problem

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. bool Hashtable::get(char *key, string value)
  2. {
  3. METADATA* temp = find(key);
  4. if(temp == NULL)
  5. {
  6. value = "";
  7. return false;
  8. }
  9. else
  10. {
  11. value = temp->value;
  12. return true;
  13. }
  14. }

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.

C++ Syntax (Toggle Plain Text)
  1. void displayAll(Hashtable *hashtable)
  2. {
  3. char key[SIZE_KEY];
  4. char value[SIZE_VALUE];
  5. cout<< "\nCurrent nodes in hashtable: " << endl;
  6. hashtable->initIterator();
  7. while(hashtable->hasNext())
  8. {
  9. hashtable->getNextKey(key);
  10. hashtable->get(key, value);
  11. cout << "key: " << key << "\tvalue : " << value << endl;
  12.  
  13. }
  14. }
Thanks
Attached Files
File Type: txt HASHTBALE_Test.txt (5.0 KB, 27 views)
Last edited by blazted; Jun 1st, 2007 at 4:15 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blazted is offline Offline
17 posts
since Oct 2006
Jun 1st, 2007
0

Re: Hash Table display problem

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 1st, 2007
0

Re: Hash Table display problem

Ahh thank you. I did that and it worked perfectly.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
blazted is offline Offline
17 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help if possible (asap)
Next Thread in C++ Forum Timeline: gnu/gcc





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC