Hashing/ Linear Probing

My code is supposed to hash data from a file into an array and then implement linear probing with additional data from another file. The first part works, but it freezes up when it enters the linear probing. Linear probing code begins line 48. Thanks in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//Purpose:  This program creates a hashed list
//			(an array of size 13) using a modulo-division 
//			hash function with a linear probe to resolve 
//			collisions. It then searches the list for 
//			numbers listed in an external file.
//---------------------------------------------------

#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;

const int HTSIZE = 13;
int hashFunction(string , int );
int main() {
	
	string hashTable[HTSIZE];
	string line;
	fstream dataIn;
	int sub = 0;
	int index = 0;
	bool found;
	int collision = 0;
	
	dataIn.open("lab6.dat");
	cout << "HASH METHOD: modulo-division" << endl
		 << "COLLISION RESOLUTION: linear probe" << endl
		 << "HASHED LIST" << endl << endl
		 << "SUB" << setw(5) << "KEY" << endl;
		 
	while(dataIn)
	{
		getline(dataIn, line);
		index = hashFunction(line, line.length());
		hashTable[index] = line;
	}
	for(index = 0; index < HTSIZE; index++)
	{
		if(hashTable[index] == "")
		{
			hashTable[index] = "-1";
		}
		cout << left << setw(5) << index << hashTable[index] << endl;
	}
	dataIn.close();
	
	dataIn.open("lab6srch.dat");
	while(dataIn)
	{
		getline(dataIn, line);
		sub = hashFunction(line, line.length());
		found = false;
		
		while(hashTable[sub] != "-1" && !found)
		{
			
			if(hashTable[sub] == line)
			{
				found = true;
			}
			else
				sub = (sub + 1) % HTSIZE;
		}
		if(found)
		{
			cout <<"Yes" << endl;
		}
		else
		{
			hashTable[sub] = line;
		}
	}
		
	dataIn.close();
	for( index = 0; index < HTSIZE; index++)
	{
		cout << setw(5) << hashTable[index] << endl;
	}

	return 0;
}

int hashFunction(string insertKey, int keyLength)
{
	int sum = 0;
	
	for (int index = 0; index < keyLength; index++)
		sum = sum + static_cast<int>(insertKey[index]);
	return (sum % HTSIZE);
}
Topic archived. No new replies allowed.