Problem with hash class

closed account (Lvk4GNh0)
Hi everyone, I'm trying to finish this program that will read in 15 names and then hash them using a hash class. My problem is that I can't figure out what is wrong with my insert function. During execution it will get to the use of the insert function and get a segmentation fault. If the use of the insert function is commented out, it will work just fine.

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
here's the driver

  #include<iostream>
#include <string>
using namespace std;
#include "hash.h"


int main (void)
{
 hash h;
 int p;
 string str,
        obj;
 bool success;
 char response;

 for(p=0;p<=14;p++)
  {
   cout<<"Enter a word \n";
   cin>>str;
   cout<<str<<endl;
   h.insert(str);

  }
 h.display();
 do
  {
   cout<<"Enter a word to be searched for \n";
   cin>>obj;
   success=h.search(obj);
   if(success)
    {
     cout<<"The word  was found \n";
    }
   else
    {
     cout<<"The word wasn't found \n";
    }
   cout<<"Search for more words (Y or N)? \n";
   cin>>response;
  }
  while (response == 'y' || response == 'Y');

 return 0;
}

here's the implementation file 

#include <iostream>
#include<string>
#include "hash.h"

using namespace std;

hash::hash()
{

 int i;
 cout<<"Entering constructor! \n";
  for(i=0;i<23;i++)
  {
   a[i]="?";
  }
}
void hash::insert(string word)
{
 int num,
     len,
     pos;
 cout<<"Entering insert";
 bool in;
 len=word.length()-1;
 num=int(word[0])+ int(word[len]);
 pos=num%23;
 if((a[pos]) != "?")
  {
   a[pos]=word;
  }
 else
  {
   pos=0;
   in=false;
   while(!in)
    {
     if((a[pos]) != "?")
      {
       a[pos]=word;
       in=true;
      }
     else
      {
       pos++;
      }
    }
    
   }
}
bool hash:: search (string  target)
{
 int j;
 bool found;
 j=0;
 found=false;
 while( (j<23) && (!found))
  {
   if(target == a[j])
    {
     found=true;
    }
   else
    {
     j++;
    }
  }
 return found;
}

void hash::display()
{
 int k;
 for(k=0;k<23;k++)
  {
   cout<<a[k]<<endl;
   cout<<"\n";
  }
}


and the header file 
#include <iostream>
#include <string>

using namespace std;

class hash
{
 public:

 hash();

 void insert(string word);

 bool search(string target);

 void display();

 private:
 string a[23];


 };




 
One problem I see is in the while loop at lines 84-95. At line 93 you increment pos with no check as to whether you've reached the end of the array. Consider the iteration through the loop when pos = 22. Line 93 increments pos to 23 and returns to the top of the loop. Line 86 then proceeds to reference a[23] which is not a valid element of the array. Valid elements are 0-22.
closed account (Lvk4GNh0)
Well looking at it now, I realize that my logic is backwards. If the element in a[pos] is equal to a question mark, then the word should be stored there.The while loop should be enacted if the position isn't a question mark. I agree to that my while loop may need some revision.

I don't understand why the the program won't even reach the insert function though.

thanks,
Sean
When I ran it, it reached the insert function, but crashed at line 88 trying to write into a[23], which is out of bounds.
Topic archived. No new replies allowed.