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;
}