Written some cool source code? Upload it to Programmer's Heaven.

Beginner C/C++

Moderators: Lundin
Number of threads: 4916
Number of posts: 15971

This Forum Only
Post New Thread

Report
Hash Table Error Posted by prominababy on 28 Nov 2009 at 8:51 AM
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;
     }
     
}

           
            






 
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A BootstrapLabs project.