Thread: Need some help with my Hash table

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    4

    Question Need some help with my Hash table

    I'm working on an open chaining hash table. I've got the following code set up. My goal is to print the hash table in order (I know it's not ideal, but it's the assignment), but from a data file of 400 names, I'm only getting three names repeatedly. Could anyone let me know what I'm doing wrong?

    Code:
    #include <list>
    #include <iostream>    
    #include <fstream>        
    #include <iomanip>
    #include <string>
    
    
    #include "sortedListADT.h"
    
    
    using namespace std;
    
    
    int BPHash(const string &str);
    
    
    
    
    // This main program asks for the data file name then uses ONE of the 
    // hash functions provided
    int main(){
        string filename = ""; 
        string inputStr = ""; 
        int i;                
        ifstream inFile;     
        unsigned int hashVal; 
        sortedListADT<string> hashTab[211];  
        int numElements = 0;  
    
    
        
        cout << "Please enter the name of the data file to read: ";
        cin >> filename;
    
    
        //pass filename to inFile.open function
        inFile.open(filename);
        
        //if input file is not found, exit
        if(!inFile){
            cerr << "unable to open file. \n";
            exit(1);
        }
        
        //read the input file, count number of strings
        while(getline(inFile, inputStr)){
            //calculate the hash value of the input string, 
            //the result will be the index
            hashVal = BPHash(inputStr);
            //add input string to the hash table at the index
            hashTab[hashVal % 211].insertElement(inputStr);
            numElements++;
        }
    
    
        for(i = 0; i < numElements; i++){
            hashTab->printInOrder();
        }
    
    
        return 0;
    }
    
    
    // implementation of the BPHash function
    int BPHash(const string &str){
        unsigned int hash = 0;
        for(std::size_t i = 0; i < str.length(); i++){
            hash = hash << 7 ^ str[i];
        }
    
    
        return hash;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,433
    > hashTab->printInOrder();
    This is like
    hashTab[0].printInOrder();

    Maybe you want your loop subscript in there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,047
    If you plan to use hash tables, why not to use set<> or multiset<>?

  4. #4
    Registered User
    Join Date
    Oct 2022
    Posts
    4
    Quote Originally Posted by flp1969 View Post
    If you plan to use hash tables, why not to use set<> or multiset<>?
    I would like to, but my assignment specifies to use list, queue, deque.

  5. #5
    Registered User
    Join Date
    Oct 2022
    Posts
    4
    Quote Originally Posted by Salem View Post
    > hashTab->printInOrder();
    This is like
    hashTab[0].printInOrder();

    Maybe you want your loop subscript in there.
    Ah yeah, it might help if my debugging skills were up to par. From what my understanding is, the first line of my data file is hashed, and input into the table, and the loop repeats. If a collision occurs, the name is added to the linked list. This is why, when I print each element of the list, other names appear inside that element. Is this correct?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,433
    You have 211 hash tables, so yeah, you need to print each one.

    Your loop should be
    Code:
        for(i = 0; i < 211; i++){
            hashTab->printInOrder();
        }
    Consider making the table size a constant.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2022
    Posts
    4
    Quote Originally Posted by Salem View Post
    Consider making the table size a constant.
    You're absolutely right and I figured what I'm doing. I'm pretty dense, and sometimes things need to be bashed right into the center of my forehead. I think I'm good.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hash table
    By mexx in forum C++ Programming
    Replies: 0
    Last Post: 06-30-2009, 12:23 PM
  2. hash table in C
    By -EquinoX- in forum C Programming
    Replies: 7
    Last Post: 03-25-2008, 09:16 PM
  3. Hash Table
    By Cpro in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2008, 02:14 PM
  4. hash table!
    By spank in forum C Programming
    Replies: 4
    Last Post: 04-10-2006, 11:09 AM
  5. What is a Hash Table?
    By rip1968 in forum C++ Programming
    Replies: 3
    Last Post: 06-18-2002, 08:57 PM

Tags for this Thread