Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help on my hash table

Status
Not open for further replies.

ToddT

Programmer
Feb 7, 2003
14
US
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=&quot;***Available***&quot;;
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 = &quot;&quot;;
unsigned int HashValue = 0;
char UserResponse;
string Input = &quot;sample.txt&quot;;
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
<< &quot; collides with &quot;
<< HashTable[HashValue]
<< &quot; at HashTable[&quot; << HashValue << &quot;]&quot;;
cin >> UserResponse;

}
}
cout << &quot;help&quot; << endl;
InputFile.close();
return(0);
}
Will appreciate any help
 
Thanks to any would be helpers, but I figured it out. I had an array domain error from failing to initialize a counter.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top