Hashtable duplicate

When ever I run displayHash() function on a second file, the data from the previous file is still showing up. Can someone help me?








#include<iostream>
#include <string>
#include "Project7.h"

using namespace std;

void menu();

int main()
{
SpellCheck spellChk;
string str1, str2;
Project7 hshstring;
HashTable MyHash;
int h, sumASCII;
char ch;


// User will choose from the menu what type of application to run

do
{

menu();

cout << endl << "Enter your choice: ";
cin >> h;

switch (h)
{
case 1:
hshstring.CreateFile(); // Create a new file
break;

case 2:
MyHash.display(); // Display the contents of the file
break;

case 3: // The user will have the option to add a word into the file
cout << "Adding a word from an existing file? (y/n)";
cin >> ch;
if ((ch == 'y') || (ch == 'Y'))
hshstring.ReadFile();
else if ((ch == 'n') || (ch == 'N'))
hshstring.CreateFile();
break;

case 4: // Searching for a word inside a file
cout << "\nPlease Enter the File Name: ";
cin >> str1;
cout << "\nWhat word are you searching? ";
cin >> str2;
sumASCII = spellChk.spellCheck(str2);
MyHash.searchHash(sumASCII, str2, str1);
break;

case 5: // Exit the program
cout << "\n**********Thank you for using this program**********" << endl;
break;

default: cout << "Invalid choice!" << endl;
}

cout << endl << endl << endl << endl << endl;

} while (h != 5);


cout << endl;
system("pause");

return 0;
}


// Menu function

void menu()
{

cout << endl << endl;
cout << "\nPlease enter your choice from the menu below: " << endl;
cout << "\n1 - Create a File" << endl;
cout << "2 - Open and display a File content" << endl;
cout << "3 - Add a word" << endl;
cout << "4 - Search for a word" << endl;
cout << "5 - Exit" << endl;
}







#include "Project7.h"
#include <iostream>
#include <string>
#include <fstream>


using namespace std;

/********************************************************************
* The constructor of the program. In here any new pointer array *
* and regular array are initialized to zero or NULL *
*********************************************************************/

Project7::Project7()
{
//tail = new Node *[5];
for (int i = 0; i < 5; i++)
{
Words[i] = "";
}
}

HashTable::HashTable()
{
tail = new Node *[5];
for (int i = 0; i < 5; i++)
{
SetArr[i] = NULL;
tail[i] = NULL;
}
}

/********************************************************************
* The destructor of the program for the Project7 Class. *
* *
*********************************************************************/

Project7::~Project7()
{

}

HashTable::~HashTable()
{
Node* temp;

for (int i = 0; i < 5; i++)
{
while (SetArr[i] != NULL)
{
temp = SetArr[i];
SetArr[i] = SetArr[i]->next;
delete temp;
}
}

delete[]tail;
}


/********************************************************************
* The add function in the implementation file performs the *
* construction of the hash table. I have elected to use the *
* chaining method to create the hash table. *
*********************************************************************/

void HashTable::add(string x)
{

Node *p = new Node;

int indx = 0;
int TtlAscii = 0;
for (int i = 0; i < x.length(); i++)
indx = indx + (int)x[i]; // Converting the string into int by calculating the sum of the ASCII code

indx = indx % 5; // The sum of the ASCII code is then divided by 5, and the remainder will be the index of the array
p->info = x;
p->next = NULL;



if (SetArr[indx] == NULL) // Inserting the first word in the index
{
SetArr[indx] = p;
tail[indx] = p;
}
else // Inserting the next word in the index using single linked list
{
tail[indx]->next = p;
tail[indx] = p;
}
}



/********************************************************************
* The display function in the implementation file displays the *
* the hash table after the user provide the file name *
*********************************************************************/

void HashTable::display()
{

string FName, Wrd;
char ch;
int j = 5;
cout << "\nEnter file name to be open: ";
cin >> FName;
ifstream FileOpen;
FileOpen.open(FName);
if (FileOpen.fail()) // if the file does not exist, returns to the main menu
cout << "\nInput file opening failed.No such file exist!!!!";
else
{
HashTable();
cout << "\nThe file contains the word: " << endl;

while (getline(FileOpen, Wrd)) // To get you all the lines
{
add(Wrd);
cout << Wrd << endl;
}


// The below coding will give the user the choice to see what the hash table looks like

cout << "\nDo you wish to see the hash table of these words? (y/n)";
cin >> ch;
cout << endl;

if ((ch == 'y') || (ch == 'Y'))
displayHash();
FileOpen.close();
}


cout << endl << endl;
system("pause");
}


void HashTable::displayHash()
{
Node *Temp = new Node;
for (int i = 0; i < 5; i++)
{
Temp = SetArr[i];
cout << i << ". ";
while (Temp != NULL)
{
cout << Temp->info << " ";

Temp = Temp->next;
}
cout << "\n";
}
}





/********************************************************************
* The CreateFile function in the implementation file will create *
* and save the word in the file. It will not be in the form of a *
* hash table *
*********************************************************************/


void Project7::CreateFile()
{
string FileName, Wrd;
char ch = 'y';

ofstream MyFile;
cout << "\nFile name to create: ";
cin >> FileName;

MyFile.open(FileName);

while ((ch == 'y') || (ch == 'Y'))
{
cout << "\nEnter a word: ";
cin >> Wrd;
MyFile << Wrd << endl;
cout << "\nThe word " << Wrd << " added succesfully to the file" << endl;
cout << "\nAdd more word? (y/n)"; // asking user if there is any more words to be added
cin >> ch;
}

MyFile.close();
cout << "\nFile saved!!" << endl;

cout << endl;
system("pause");
}


/********************************************************************
* The ReadFile function in the implementation file will open an *
* file, and added any word to the end of the file. In order to *
* perform this function, the ifstream and ofstream are opened *
*********************************************************************/



void Project7::ReadFile()
{
string Wrd, FName;
char ch = 'y';

cout << "\nEnter file name to be open: ";
cin >> FName;

ofstream fout; // input and output file streams are opened
ifstream fin; // input and output file streams are opened
fin.open(FName);
if (fin.fail())
cout << "Input file opening failed.\n";
else
{
fout.open(FName, ofstream::app);

while ((ch == 'y') || (ch == 'Y'))
{
cout << "\nEnter a word: ";
cin >> Wrd;
fout << Wrd << endl;
cout << "\nThe word " << Wrd << " added succesfully to the file";
cout << "\nAdd more word? (y/n)";
cin >> ch;
}
fin.close();
fout.close();
cout << "\nFile saved!!" << endl;

}
cout << endl;
system("pause");
}



int SpellCheck::spellCheck(string x)
{

string Wrd = x;

int indx = 0;
for (int i = 0; i < Wrd.length(); i++)
indx = indx + (int)x[i];
int TtlAscii = indx;
indx = indx % 5;

return indx;
}




void HashTable::searchHash(int x, string a, string b)
{

string Wrd;
string FName = b;
bool found = 0;
ifstream fin; // input file streams are opened
fin.open(FName);
if (fin.fail())
cout << "Input file opening failed.\n";
while (getline(fin, Wrd)) // To get you all the lines
add(Wrd);

Wrd = a;
Node *p = SetArr[x];
while (p != NULL)
{
if (p->info == Wrd)
{
found = 1;
break;
}
p = p->next;
}
if (found == 1)
cout << "The word exist";
else
cout << "The word does not exist";

fin.close();

cout << endl;
system("pause");
}





Last edited on
Yeah, you really need to use the [code][/code] tags if you're dumping 100's of lines of code on us with basically no explanation or comments.
Topic archived. No new replies allowed.