Program is keep crashing

Okay I got the right result, but my program is keep crashing. I think it has something to do with the way I declare "val", and the program will crash as soon as it is done printing. Any help will be great.
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
  class ClosedHash
{
 private:
   struct DCT{
   int k;
   string val;
  }a[65];

 public:
 int hash(int);
 void insert(int,int,string);
 void display();

};

void ClosedHash::init()
{
 for(int i=0;i<65;i++)
 {
 a[i].k= -1;
 a[i].val="non";
 }
}

void ClosedHash::insert(int index,int key,string state)
{
    int flag,i,count=0;
    flag=0;
 if(a[index].k==-1)/*if the location indicated by hash key is empty*/
 {
 a[index].k=key;
 a[index].val=state;
 }
 else
 {
 i=0;
 while(i<65)
 {
 if(a[i].k!= -1)
  count++;
 i++;
 }

 for(i=index+1;i<65;i++)
  if(a[i].k== -1)    
  {
  a[i].k=key;
  a[i].val=state;
    /*placing the number at empty location*/
  flag=1;
  break;
}
 for(i=0;i<index&&flag==0;i++)/*array from 0th to keyth location will be scanned*/
  if(a[i].k== -1)
  {
  a[i].k=key;
  a[i].val=state;
  flag=1;
  break;
  }
  } 
}

void ClosedHash::display()
{
    int i;
    cout<<"\n--------------------------------";

    for(i=1;i<66;i+=4)
    {
        cout<<"\n  "<<a[i].val << " "<< a[i+1].val<<" "<< a[i+2].val <<" "<<a[i+3].val;
    }
        cout<<"\n--------------------------------";
}

int main()
{
    int key,Hkey,search_key;
    int a=1,b=4,c=3,d=2;
    char inString[15]="ALABAMA";
    string state="ALABAMA";

    char ans;
    ClosedHash obj;

    cout<<"\nDictionary Functions using Hashing";
    obj.init();



        key=(a*inString[0]+b*inString[1]+c*inString[2]+d*inString[3])%65;
        Hkey=obj.hash(key);/*returns hash key*/
        obj.insert(Hkey,key,state);/*collision handled by linear probing*/

    obj.display();/*displays hash table*/


}
Change your 'for' loop on line 69 to for (i=1; i<65; i+=4) to stop going out of bounds on your arrays. Also, as it is, you need some way to output the first value in the array (or so I would assume). Maybe change the loop to for (i=0; i<64; i+=4) and then after the end of the loop simply add cout << "\n " << a[64].val;
Last edited on
thank you thank you you are a life savior.
Topic archived. No new replies allowed.