I'm trying to make this simple dictionary using hash table and struct. basically i restore the information in the struct and put the struct into an array of pointer. but my program seems buggy. this program is not complete yet, so there are still some function missing. but with the current function that are available, my program keeps crashing after the 2 string input. can somebody point out my mistake please ? Thx !

#include <iostream>
using namespace std;

struct Dictionary {
       string english;
       string indonesia;
       };
       Dictionary *p;
       Dictionary *q[99];

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;
        
        while ( no != 2)
        {
		addDef();
		cout <<"Do you want to continue ? (1. yes, 2. no)";
		cin >> no;
        } 
	}
	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;
		mainMenu();
	}
}

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 :"<<endl;
            cin>>a;
            cout<<"Enter the transelation in Indonesia :"<<endl;
            cin>>b;
            createNode(a,b);
            hashVal = hashing (p->english);
            q[hashVal] = p;
}

int hashing (string s)
{
    int total = 0;
    int z = 0;
    char cname [99];
    string temp = s;
    
    for ( int i = 0; i <= 99; i++)
    {
    cname[i] =  temp.at(i);
    }
    
    for (int j = 0; j <= 99; j++)
    {
        total = total + (int)cname[j];
    }
    
    z = total % 99;
    return z;
}

int quit ()
{
    return 0;
}

void searchDef(){}
void deleteDef(){}

One bug is can see inside int hashing (string s): you are declaring cname an array of 99 characters. This means you can use subscripts 0-98. But in your two loops, your condition is i<= 99 and j <=99 which accesses subscript 99 which is out of range from your declaration.

By the way, please initialize ALL your variables. For example in line 39, your variable "no" is not initialize, yet in the next statement, you are using it and comparing it with some other value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.