Help with hash program using vectors

Hey I have a "segmentation fault(core dumped)" error for the following code and not sure what to look into next. Please help thanks.

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <vector>

using namespace std;

vector<string> input(vector<string> hash);
void display(vector<string> hash);
void check(vector<string> hash);

int main(void)
{
	vector<string> hash (23);
	int i;
	for(i = 0; i < 23; i++)
	{
		hash[i] = "??";
	}
	input(hash);
	display(hash);
	check(hash);
}

vector<string> input(vector<string> hash)
{
	string word;
	int inthash;
	int i;
	int length;
	bool test;
	
	for(i = 0; i < 15; i++)
	{
		test = true;
		cout<<"Input the word to hash into the array "<< (i+1) <<": "<<endl;
		cin>>word;
		length = word.size() - 1;
		inthash = ( word[0] + word[length] ) % 23;
		while(test == true)
		{
			if (inthash < 23)
			{
				if (hash[inthash] == "??")
				{
					hash[inthash] = word;
					test = false;
				}
				else
					inthash++;
			}
			else
				inthash = 0;
		}
	}

	return hash;
}

void display(vector<string> hash)
{
	cout<<"Your array reads: "<<endl;
	int i;
	
	for(i = 0; i < 23; i++)
	{
		cout<<i<<" : "<<hash[i]<<endl;
	}
}

void check(vector<string> hash)
{
	string check;
	int j = 0;
	int i;
	int intcheck;
	bool factor = true;
	char again;
	int length;
	
	while(factor == true)
	{
		cout<<"Which word would you like to check in the database"<<endl;
		cin>>check;
		length = check.size() - 1;
		intcheck = ( check[0] + check[length] ) % 23;
		for(i = intcheck, j = 0; i < 23 && j < 23; i++, j++)
		{
			if(check == hash[i])
			{
				cout<<"Word is found"<<endl;
				break;
			}
			cout<<check<<" != "<<hash[i]<<endl;
			if(i == 22)
				i = 0;
		}
		if(j == 23)
			cout<<"Word was not found"<<endl;
		
		cout<<"Want to try again: Y or N "<<endl;
		cin>>again;
		if(again == 'Y')
			factor = true;
		else
			factor = false;
	}
	
}			


Last edited on
1. Use code tags. See http://cplusplus.com/articles/z13hAqkS/ .
2. After you have edited your post to properly show the code with code tags, be sure to let us know the line number where the problem appears.

You see, I don't even read code posted without code tags and proper indentation, and probably many others as well. So help us help you. :D
Hey is this the right format? There is no line number because it has no errors but when executed it goes straight to segmentation fault(core dumped).
This is better, yes. Find out the line number. Debug your code. Run the program one line at a time and witness the moment the segfault appears. Then you'll know exactly the line giving you a hard time.
Your vector has no size.
One more bug in your code

you are accessing the element which is not present in vector range using []
valid range is 0 to size()-1

length = check.size();

//index starts from 0 to length-1, but u r trying to use check[length]

intcheck = ( check[0] + check[length] ) % 23;//problem may be with this line of code



Alright, I fixed those two errors, but it won't even print the first cout statement, it just goes to segmentation fault(core dumped). It wont even print a line before I initialize the hash vector, so am I using the vectors incorrectly.
Last edited on
All I had to do to get it to compile and run(wether it runs correctly I dont know) was change vector declaration to vector<string> hash(23);

and change #include <string.h>
to
#include <string>

Post your current code.

Edit: And as NVTKrishna said your reading past array subscripts so change length.
Last edited on
Might be me, but I don't think your code should even be compiling properly... You're using vector<string>, but you haven't included the C++ string library, just the C-string one. Your need the string header, not string.h or cstring.

[edit]

for(i = 0; i < 23; i++) --> You only have 22 elements, at [0,21]. 22 is going to throw an out of bounds error. Same goes for lines 44, 95 & 96, which should be %22, not %23, and a loop to <22.
Last edited on
do hash.emty(); before the while loop in void check(vector<string> hash)
Alright, I edited my code with your help and it runs good. Thanks guys. But, it won't input the words into the string vector during the input function, so looking into that now.
Your also passing the vectors by value when you should pass a reference. The seg fault is due to the vector size being 22, make Gaminic's changes or change vector size to 23.

@bluecoder Why should he empty the vector?

EDIT: inthash = ( word[0] + word[length-1] ) % 23; You will probably gett alot of collisions with this hash. Most i've seen use XOR and a prime number.
Last edited on
Yep that did it, thanks guys
Topic archived. No new replies allowed.