<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'Hash Table Error' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'Hash Table Error' posted on the 'Beginner C/C++' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2012 Programmers Heaven</copyright>
    <pubDate>Thu, 09 Feb 2012 09:28:06 -0800</pubDate>
    <lastBuildDate>Thu, 09 Feb 2012 09:28:06 -0800</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>Hash Table Error</title>
      <link>http://www.programmersheaven.com/mb/beginnercpp/409979/409979/hash-table-error/</link>
      <description>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.&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
#include &amp;lt;iostream&amp;gt;
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&amp;lt;&amp;lt;"Welcome to Dictionary of English-Indonesia 2012(KIAMAT !)"&amp;lt;&amp;lt;endl;
	cout&amp;lt;&amp;lt;"What do You want to do ?"&amp;lt;&amp;lt;endl;
	cout&amp;lt;&amp;lt;"1. add Definition to Dictionary"&amp;lt;&amp;lt;endl;
	cout&amp;lt;&amp;lt;"2. Search for Definition"&amp;lt;&amp;lt;endl;
	cout&amp;lt;&amp;lt;"3. delete Definition"&amp;lt;&amp;lt;endl;
	cout&amp;lt;&amp;lt;"4. Quit"&amp;lt;&amp;lt;endl;
	cout&amp;lt;&amp;lt;"Enter the number you want :  ";
	cin&amp;gt;&amp;gt;choice;
	
	if( choice == 1)
	{   
        int no;
        
        do
        {
		addDef();
		cout &amp;lt;&amp;lt;"Do you want to continue ? (1. yes, 2. no)";
		cin &amp;gt;&amp;gt; no;
        } while ( no != 2);
	}
	else if (choice == 2)
	{
		searchDef();
	}
	else if (choice == 3)
	{
		deleteDef();
	}
	else if ( choice == 4 )
	{
		quit ();
	}
	else
	{
		cout&amp;lt;&amp;lt;"Input Invalid ! Enter between 1-4 !"&amp;lt;&amp;lt;endl&amp;lt;&amp;lt;endl;
		return;
	}
}

void createNode (string a, string b)
{
     p = (Dictionary *)malloc(sizeof(Dictionary));
     if (p!=NULL)
     {
        p-&amp;gt;english = a;
        p-&amp;gt;indonesia = b;
     }
     else
     {
         cout&amp;lt;&amp;lt;"Memory FULL";
     }
     
}
     
void addDef ()
{
     string a;
     string b;
     int hashVal;
     
            cout&amp;lt;&amp;lt;"Enter Word in English :";
            cin&amp;gt;&amp;gt;a;
            cout&amp;lt;&amp;lt;"Enter the transelation in Indonesia :";
            cin&amp;gt;&amp;gt;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 &amp;lt;= temp.length(); i++)
    {
    cname[i] =  temp.at(i);
    }
    
    for (int j = 0; j &amp;lt;=temp.length(); j++)
    {
        total = total + (int)cname[j];
    }
    
    z = total % 99;
    return z;
}

int quit ()
{
    return 0;
}

void searchDef()
{
     string a;
     int search;
     cout&amp;lt;&amp;lt;"Enter the word you want to search in english :  "&amp;lt;&amp;lt;endl;
     cin&amp;gt;&amp;gt;a;
     search = hashing(a);
     if (q[search] != NULL)
     {
        cout&amp;lt;&amp;lt;"The Definition of : "&amp;lt;&amp;lt;q[search]-&amp;gt;english&amp;lt;&amp;lt;endl;
        cout&amp;lt;&amp;lt;"in Indonesian is : "&amp;lt;&amp;lt;q[search]-&amp;gt;indonesia&amp;lt;&amp;lt;endl;
     }
     else
     {
         cout&amp;lt;&amp;lt;"Definition not Found";
         return;
     }
}
void deleteDef()
{
     string a;
     int search;
     cout&amp;lt;&amp;lt;"Enter the word you want to delete : "&amp;lt;&amp;lt;endl;
     cin&amp;gt;&amp;gt;a;
     search = hashing (a);
     
     if (q[search] !=NULL)
     {
        free(q[search]);
        cout&amp;lt;&amp;lt;"Node is Deleted !"&amp;lt;&amp;lt;endl;
        mainMenu();
     }
     else
     {
         cout&amp;lt;&amp;lt;"Word not found !";
         return;
     }
     
}

           
            

&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/beginnercpp/409979/409979/hash-table-error/</guid>
      <pubDate>Sat, 28 Nov 2009 08:51:49 -0800</pubDate>
      <category>Beginner C/C++</category>
    </item>
  </channel>
</rss>
