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 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.
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include "IndDicT.h"
usingnamespace 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);
}
elseif(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);
}
elseif(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)
{
returnfalse;
}
}
returntrue;
}
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