Hash Table display problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2006
Posts: 17
Reputation: blazted is an unknown quantity at this point 
Solved Threads: 0
blazted blazted is offline Offline
Newbie Poster

Hash Table display problem

 
0
  #1
Jun 1st, 2007
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.

  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.

  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
Last edited by blazted; Jun 1st, 2007 at 4:15 pm.
Attached Files
File Type: txt HASHTBALE_Test.txt (5.0 KB, 7 views)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,612
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Hash Table display problem

 
0
  #2
Jun 1st, 2007
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 17
Reputation: blazted is an unknown quantity at this point 
Solved Threads: 0
blazted blazted is offline Offline
Newbie Poster

Re: Hash Table display problem

 
0
  #3
Jun 1st, 2007
Ahh thank you. I did that and it worked perfectly.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC