Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5248
Number of posts: 16673

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

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;
     }
     
}

           
            






 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 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.
Operated by CommunityHeaven, a BootstrapLabs company.