Don't really know how to search for this.

Hi all, apologies if this is posted somewhere else, not really sure what is wrong here. Code compiles fine, 0 errors and the program runs but crashes when it reaches this statement:

if(dictionary[i].meaningList->isEmpty())

int i is set, and the struct looks like this:

1
2
3
4
5
6
7
    
private:
        struct
        {
            string word;
            List* meaningList;
        }dictionary[MAX_TABLESZ];


Not really sure whats going on here, any help would be appreciated.
I think what im trying to ask here is how do I call the list member function isEmpty() if its in the dictionary array?

dictionary[i].meaningList.isEmpty()

doesnt seem to work.
If you haven't initialised meaningList, it won't pont to a List and dereferencing it in calls like meaningList->isEmpty() should cause a runtime error/crash.
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
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include "IndDicT.h"

using namespace std;

IndDicT::IndDicT()
{
    for(int a=0; a >= MAX_TABLESZ; a++)
	{
		dictionary[a].word = "";
		dictionary[a].meaningList = new List;
	}
}

IndDicT::~IndDicT()
{
    for(int a=0; a >= MAX_TABLESZ; a++)
	{
		dictionary[a].word = "";	// set word equal to null
		delete dictionary[a].meaningList;	// call default destructor for list objects
	}
}

//return the hash value of a word. Using Division Remainder method.
//slightly modified so that i dont have to use a rehash algorithm
int IndDicT::hashKey(string word, int prime)
{
        int result=0;

        for(int i=0;i<word.length();i++)
        {
            //obatain the ASCII interger value of character i in the sting word.
            result = result + static_cast<int>(word[i]);
        }
        //make sure that the result is a positive numeric value first
        if(result < 0)
        {
            result += 1;
        }

        return(result % prime); //prime being the used prime number table value.
}

//using the word, searchs to make sure word is not in the table
//and table location is empty if it is then insert at location.
//If location is not empty rehash and step threw to locate new position.
//If word exists then the meaning is added to the end of existing meanings.
void IndDicT::insert(string keyWord, string keyMeaning)
{
    //primary hash of the key
    int i = hashKey(keyWord, MAX_TABLESZ);

    //check to see if first hashkey location is empty
    if(dictionary[i].word != "")
    {
        //check to see if key words are the same
        if(dictionary[i].word == keyWord)
        {
            //if they are insert meaning at list rear of key word
            cout << "New Meaning added to " << keyWord << " at position " << i << " : " << keyMeaning;
            dictionary[i].meaningList.insertAtRear(keyMeaning);
        }
        else
        {
            //collision has occured and the key must be rehased
            //home check for infinit loop.
            int home = i;
            int c = hashKey(keyWord, REH_TABLESZ);

            do
            {
                i = i+c;
                if(i >= REH_TABLESZ)
                {
                    //hash table wraparound
                    i = i - REH_TABLESZ;
                }
            }while((dictionary[i].word != "") && (home != i) && (keyWord != dictionary[i].word));

            if(dictionary[i].word == keyWord)
            {
                //if they are insert meaning at list rear of key word
                cout << "New Meaning added to " << keyWord << " at position " << i << " : " << keyMeaning;
                dictionary[i].meaningList.insertAtRear(keyMeaning);
            }
            else if(dictionary[i].word == "")
            {
                cout << "New Word added to dictionary :" << keyWord << " at position " << i << " with meaning : " << keyMeaning;
                dictionary[i].word = keyWord;
                dictionary[i].meaningList.insertAtRear(keyWord);
            }
            else if(home == i)
            {
                cout << "Key : " << keyWord << " and meaning could not be added to the dictionary, This may be because the dictionary is full";
                cout << "or all index locations may not have been traveresed. My sincere apologies." << endl << endl;
            }
            else
            {
                cout << "An Uknown error has occured and nothing was added to the dictionary." << keyWord << " : ";
                cout << keyMeaning << " : " << i << " ."<< endl << endl;
            }
        }
    }
    else
    {
        //check if there is a meaning asigned to the empty string
        if(dictionary[i].meaningList.isEmpty())
        {
            cout << endl << "An error has occured : position " << i << " in the table has key : " << dictionary[i].word;
            cout << " and meaning " ;
            dictionary[i].meaningList.printList();
        }
        else
        {
            cout << "New Word added to dictionary :" << keyWord << " at position " << i << " with meaning : " << keyMeaning;
            dictionary[i].word = keyWord;
            dictionary[i].meaningList.insertAtRear(keyMeaning);
        }
    }

}

//using the hashkey value to lookup words in the hash table.
List* IndDicT::lookUp(string word)
{
    return dictionary[1].meaningList;
}

//Print out the non-empty dictionary entries in alphabetical order to a file
void IndDicT::traverse()
{

}

//output dictionary entries to the screen.
void IndDicT::printTable()
{
        for(int i=0;i < MAX_TABLESZ;i++)
        {
            if(dictionary[i].word == "")
            {
                cout << i << " : " << " is empty. " << endl;
            }
            else
            {
                cout << i << " : " << dictionary[i].word << " : ";
                dictionary[i].meaningList->printList();
            }
        }
}

/*Alphanumeric function tests the word for non alphanumeric characters and
returns a bool is true or false*/
bool Alphanumeric(string word)
{
	for(int i=0; i < word.length(); i++)
	{
		if(word[i] <= 65 || word[i] >= 122)
		{
			return false;
		}
	}
	return true;
}


Please note this is not a finished piece of work :). I'm just trying to get the insert function to work. As I understand it

dictionary[a].meaningList = new List;

should intialize the list right?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
IndDicT::IndDicT()
{
    for(int a=0; a >= MAX_TABLESZ; a++)
	{
		dictionary[a].word = "";
		dictionary[a].meaningList = new List;
	}
}

IndDicT::~IndDicT()
{
    for(int a=0; a >= MAX_TABLESZ; a++)
	{
		dictionary[a].word = "";	// set word equal to null
		delete dictionary[a].meaningList;	// call default destructor for list objects
	}
}


all that comes to mind is thw word D'OH!

for(int a=0; a >= MAX_TABLESZ; a++)

should be

for(int a=0; a < MAX_TABLESZ; a++)
Last edited on
Topic archived. No new replies allowed.