I have this simple hash table but I am getting an access error, the code is
// Include Directives
// ------------------
#include <cmath>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int MAX_KEYS=1000;
const string EMPTY_NODE="***Available***";
string HashTable[MAX_KEYS];
unsigned int calcHashValue( string S){
const unsigned int TOP5_MASK = 0xF8000000;
const int LEFT_SHIFT = 5;
const int RIGHT_SHIFT = 11;
unsigned int ReturnVal = 0;
unsigned int Carry = 0x0;
int StringLength = S.length();
for(int i=0; i < StringLength;i++){
Carry = (ReturnVal & TOP5_MASK);
Carry = Carry >> RIGHT_SHIFT;
ReturnVal <<= LEFT_SHIFT;
ReturnVal ^= Carry;
ReturnVal ^= S.c_str();
}
return(ReturnVal % MAX_KEYS );
}
int main(void){
ifstream InputFile;
string TempString = "";
unsigned int HashValue = 0;
char UserResponse;
string Input = "sample.txt";
InputFile.open(Input.c_str());
for(int j;j<MAX_KEYS;j++){
HashTable[j] = EMPTY_NODE;
}
while( InputFile ){
InputFile >> TempString;
HashValue = calcHashValue( TempString );
if( HashTable[HashValue] == EMPTY_NODE ){
HashTable[HashValue] = TempString;
}
else if( TempString != HashTable[HashValue] ){
cout << endl << TempString
<< " collides with "
<< HashTable[HashValue]
<< " at HashTable[" << HashValue << "]";
cin >> UserResponse;
}
}
cout << "help" << endl;
InputFile.close();
return(0);
}
Will appreciate any help
// Include Directives
// ------------------
#include <cmath>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int MAX_KEYS=1000;
const string EMPTY_NODE="***Available***";
string HashTable[MAX_KEYS];
unsigned int calcHashValue( string S){
const unsigned int TOP5_MASK = 0xF8000000;
const int LEFT_SHIFT = 5;
const int RIGHT_SHIFT = 11;
unsigned int ReturnVal = 0;
unsigned int Carry = 0x0;
int StringLength = S.length();
for(int i=0; i < StringLength;i++){
Carry = (ReturnVal & TOP5_MASK);
Carry = Carry >> RIGHT_SHIFT;
ReturnVal <<= LEFT_SHIFT;
ReturnVal ^= Carry;
ReturnVal ^= S.c_str();
}
return(ReturnVal % MAX_KEYS );
}
int main(void){
ifstream InputFile;
string TempString = "";
unsigned int HashValue = 0;
char UserResponse;
string Input = "sample.txt";
InputFile.open(Input.c_str());
for(int j;j<MAX_KEYS;j++){
HashTable[j] = EMPTY_NODE;
}
while( InputFile ){
InputFile >> TempString;
HashValue = calcHashValue( TempString );
if( HashTable[HashValue] == EMPTY_NODE ){
HashTable[HashValue] = TempString;
}
else if( TempString != HashTable[HashValue] ){
cout << endl << TempString
<< " collides with "
<< HashTable[HashValue]
<< " at HashTable[" << HashValue << "]";
cin >> UserResponse;
}
}
cout << "help" << endl;
InputFile.close();
return(0);
}
Will appreciate any help