This is a simple dictionary program, where we put words and it's definition in a single struct and assign that struct with a hash value. But i'm having trouble with the program. it keeps crashing. and i dont know why. can you help me ? please. thx.
#include <iostream>
using namespace std;
struct Dictionary {
string english;
string indonesia;
};
Dictionary *p;
Dictionary *q[99] = {NULL};
void mainMenu();
int hashing (string s);
void addDef();
void searchDef();
void deleteDef();
void createNode(string a, string b);
int quit ();
int main ()
{
mainMenu ();
}
void mainMenu ()
{
int choice;
cout<<"Welcome to Dictionary of English-Indonesia 2012(KIAMAT !)"<<endl;
cout<<"What do You want to do ?"<<endl;
cout<<"1. add Definition to Dictionary"<<endl;
cout<<"2. Search for Definition"<<endl;
cout<<"3. delete Definition"<<endl;
cout<<"4. Quit"<<endl;
cout<<"Enter the number you want : ";
cin>>choice;
if( choice == 1)
{
int no;
do
{
addDef();
cout <<"Do you want to continue ? (1. yes, 2. no)";
cin >> no;
} while ( no != 2);
}
else if (choice == 2)
{
searchDef();
}
else if (choice == 3)
{
deleteDef();
}
else if ( choice == 4 )
{
quit ();
}
else
{
cout<<"Input Invalid ! Enter between 1-4 !"<<endl<<endl;
return;
}
}
void createNode (string a, string b)
{
p = (Dictionary *)malloc(sizeof(Dictionary));
if (p!=NULL)
{
p->english = a;
p->indonesia = b;
}
else
{
cout<<"Memory FULL";
}
}
void addDef ()
{
string a;
string b;
int hashVal;
cout<<"Enter Word in English :";
cin>>a;
cout<<"Enter the transelation in Indonesia :";
cin>>b;
createNode(a,b);
hashVal = hashing (a);
q[hashVal] = p;
}
int hashing (string s)
{
int total = 0;
int z = 0;
char cname [99];
string temp = s;
for ( int i = 0; i <= temp.length(); i++)
{
cname[i] = temp.at(i);
}
for (int j = 0; j <=temp.length(); j++)
{
total = total + (int)cname[j];
}
z = total % 99;
return z;
}
int quit ()
{
return 0;
}
void searchDef()
{
string a;
int search;
cout<<"Enter the word you want to search in english : "<<endl;
cin>>a;
search = hashing(a);
if (q[search] != NULL)
{
cout<<"The Definition of : "<<q[search]->english<<endl;
cout<<"in Indonesian is : "<<q[search]->indonesia<<endl;
}
else
{
cout<<"Definition not Found";
return;
}
}
void deleteDef()
{
string a;
int search;
cout<<"Enter the word you want to delete : "<<endl;
cin>>a;
search = hashing (a);
if (q[search] !=NULL)
{
free(q[search]);
cout<<"Node is Deleted !"<<endl;
mainMenu();
}
else
{
cout<<"Word not found !";
return;
}
}