There are a couple of things that need to be fixed.
(1) This is a problem -> p = (Translator*)malloc(sizeof(Translator));
because malloc doesn't call constructors. This means that the underlying
string objects are not initialized properly (perhaps some internal pointers
are not set?). Therefore, the program crashes when you try to use them below.
The solution to this is to simply use new instead of malloc -> p = new Translator;
(2) You also have an out-of-bounds problem in your hash function. How do you
know that your string's size will always be 99? It should look more like this:
int hashing (string s)
{
int total = 0;
for (int j = 0; j < s.size(); j++)
total = total + (int)s[j];
return total % 99;
}
Since you've made it this far by yourself, I'll give you a simple implementation for
searchDef, because there is another important problem that I want to point out.
Here's your code slightly modified and with searchDef added:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
struct Translator
{
string english;
string french;
};
Translator * dictionary[99] = { 0 };
void mainMenu();
int hash(string s);
void addDef();
void searchDef();
Translator * createNode(string a, string b);
void quit ();
int main()
{
while (true) mainMenu();
}
void mainMenu()
{
int choice;
cout
<< "Welcome to English-French translator\n"
<< "What do You want to do ?\n"
<< "1. add Definition to Dictionary\n"
<< "2. Search for Definition\n"
<< "3. Quit\n"
<< "Enter the number you want : ";
cin >> choice;
switch(choice)
{
case 1: addDef(); break;
case 2: searchDef(); break;
case 3: quit(); break;
default: cout << "wrong choice";
}
}
Translator * createNode (string a, string b)
{
Translator * new_node = new Translator;
if (new_node != NULL)
{
new_node->english = a;
new_node->french = b;
}
else
{
cout << "Memory FULL";
}
return new_node;
}
void addDef ()
{
string a;
string b;
int hashVal;
cout << "Enter Word in English : ";
cin >> a;
cout << "Enter the transelation in French : ";
cin >> b;
Translator * new_node = createNode(a,b);
hashVal = hash(a);
dictionary[hashVal] = new_node;
}
void searchDef()
{
string a;
string b;
int hashVal;
cout << "Enter Word in English : ";
cin >> a;
hashVal = hash(a);
if (dictionary[hashVal] && dictionary[hashVal]->english == a)
cout << dictionary[hashVal]->french << endl;
else
cout << "couldn't find word..." << endl;
}
int hash(string s)
{
int total = 0;
for (int j = 0; j < s.size(); j++)
total = total + (int)s[j];
return total % 99;
}
void quit() { exit(0); }
Try entering hi -> salut and blue -> bleue . Then, try to find salut and bleue using hi and blue . It works fine, right?
Now, try entering dog -> chien and god -> dieu (in that order). Then, try to find chien using dog . It doesn't work, right?
(3) The problem here is that you do nothing to take care of hash collisions. dog and god have that
same hash value. As a result, entering god -> dieu
overwrites dog -> chien and vice versa.
A simple solution to this is to implement your hashmap as an array of std::maps. Here's an
example of how you can do that -> http://www.cplusplus.com/forum/beginner/31394/#msg170354
[EDIT] (Note that I forgot to #include <cstdlib> in the above example...) [/EDIT]
P.S. Using the above approach for implementing you hashmap will also take care of your memory leaks :P