Hello guys :)

I hope all are alright ;)

I'm using HashTable to make a program that reads a string from a file..
when I run it, it gives me the message of error (Don't Send) << I guess all of you know this message..

this is my code

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

class Node
{
public:
	int data;
	int count;
	Node *link;

	Node()
	{
		data = count = 0;
		link = 0;

	}

	Node( int x )
	{
		data = x;
		count = 0;
		link = 0;

	}

}; // end class Node

class HashTable
{
private:
	Node *HT, *tmp;
	int tSize;

public:

	HashTable( int x )
	{
		HT = new Node[x];
		tSize = x;
		tmp = 0;

	} // end constructor for HashTable

	void insert( int x )
	{
		int loc;

		loc = x % tSize;
		
		if( HT[loc].count == 0 )
		{
			HT[loc].data = x;
			HT[loc].count++;

		}

		else
			if( x > HT[loc].data )
			{
				if(HT[loc].count == 1)
				{
					HT[loc].link = new Node(x);
					HT[loc].count++;
				}
				else if(HT[loc].link->data > x)
				{
					tmp = new Node(x);
					tmp->link = HT[loc].link;
					HT[loc].link = tmp;
					HT[loc].count++;
				}
				else
				{
					Node * tmp2;
					for(tmp = HT[loc].link, tmp2=tmp->link; tmp2; tmp2 = tmp, tmp = tmp->link)
						if(tmp2->data > x)
							break;
					Node *tmp3 = new Node(x);
					tmp3->link = tmp2;
					tmp->link = tmp3;
					HT[loc].count++;
				}
			}

				else
					if( x < HT[loc].data )
					{
						if(HT[loc].count == 1)
						{
							HT[loc].link = new Node(HT[loc].data);
							HT[loc].data = x;
							HT[loc].count++;
						}
						else
						{
							tmp = new Node(HT[loc].data);
							HT[loc].data = x;
							tmp->link = HT[loc].link;
							HT[loc].link = tmp;
							HT[loc].count++;
						}

					}

	} // end function insert

	bool search( int x, int &y )
	{
		y = x %tSize;
		if( HT[y]. data == x )
		{
			cout << "\nNumber found at location " << y << endl;
			return true;
		}
		else{
			for( tmp = HT[y].link; tmp; tmp = tmp ->link)
			{
				if(tmp->data == x)
				{
					cout << "\nNumber found at location " << y << endl;
					return true;
				}
			} // end for loop for searching an element entered by the user
		}
		cout << "\nNumber was not found\n";
		return false;

	} // end function search


	void print()
	{
		cout << "\nLocation			Entry(s)\n";
		cout << "=========			========\n";

		for( int i = 0; i < tSize; i++ )
		{
			cout << "HT [" << i << "]		" << HT[i].data;
			for( tmp = HT[i].link; tmp; tmp = tmp ->link)
			{
				cout << " -> " << tmp -> data;

			}

			cout << endl;

		} // end for loop

		int z = 0;
		int u;
		for( int j = 0; j < tSize; j++ )
		{
			if( HT[j].count > 0 )
			{
				z++;
			}

		}
		u = (z/tSize) * 100;
		cout << "\nThe utilization is " << u;
		cout << endl;
		cout << "The number of elements in the HashTable is ";

		int w = 0;

		for( int k = 0; k < tSize; k++ )
		{
			w+=HT[k].count;

		}

		cout << w;
		cout << endl;

	} // end print

};
	int main()
	{
		HashTable hi(4);
		string n;
		int sum = 0;

		// constructor opens the file
		ifstream fin( "dictionary.txt" );

		// if statement to check if the file opens
		if( !fin )
		{
			cerr << "File cannot be opened..\n";
			exit(1);

		} // end if statement

		while( fin >> n )
		{
			for( int i = 0; i < int( n.length() ); i++ )
			{
				sum += n[ i ];
				hi.insert( sum );

			}

		}
			

		cout << endl;

		return 0;
	}

What I'm doing is using (While loop) to read the string from a file, then convert it to integer then call the function (insert) that'll insert the string in the hashtable

but I don't know where's the problem of getting that message of error!!

I hope you'll help me finding it.. :)

thanks in advance :)

>>I guess all of you know this message..
No I don't. Post the exact error message instead of making us guess.

what compiler and os are you using? Learn to use your compiler's debegger so that you can step through the program one line at a time to see its execution.

The only problem I could spot is in main() a data overflow will occur when the value of sum reaches the limit that an integer can hold -- the results of overflow is undetermined.

You need to supply dictionary.txt. This program can't be run without it.

OMG! my friend you can use any txt file as a sample..

ok ok these are the contents of dictionary.txt

abalone
abandon
abase
abash
abate
abater
abbas
abbe
abbey
abbot
Abbott
abbreviate
abc
abdicate
abdomen
abdominal
abduct
Abe
abed
Abel
Abelian
Abelson
Aberdeen
Abernathy
aberrant
aberrate
abet
abetted
abetting
abeyance
abeyant
abhorred
abhorrent
abide
Abidjan
Abigail
abject
ablate
ablaze
able
ablution
Abner
abnormal
Abo
aboard
abode
abolish
abolition
abominable
abominate
aboriginal
aborigine
aborning
abort
abound
about
above
aboveboard
aboveground
abovementioned
abrade
Abraham
Abram
Abramson
abrasion
abrasive
abreact
abreast
abridge
abridgment
abroad
abrogate
abrupt
abscess
abscissa
abscissae
absence
absent
absentee
absenteeism
absentia
absentminded
absinthe
absolute
absolution
absolve
absorb
absorbent
absorption
absorptive
abstain
abstention
abstinent
abstract
abstracter
abstractor
abstruse
absurd

I'm sorry, for being not so detailed in my question.. I have some network conection errors :\

Well, I hope that's enough informations :)

thanks in advance :)

OMG! my friend you can use any txt file as a sample..

ok ok these are the contents of dictionary.txt

abalone
abandon
abase
abash
abate
abater
abbas
abbe
abbey
abbot
Abbott
abbreviate
abc
abdicate
abdomen
abdominal
abduct
Abe
abed
Abel
Abelian
Abelson
Aberdeen
Abernathy
aberrant
aberrate
abet
abetted
abetting
abeyance
abeyant
abhorred
abhorrent
abide
Abidjan
Abigail
abject
ablate
ablaze
able
ablution
Abner
abnormal
Abo
aboard
abode
abolish
abolition
abominable
abominate
aboriginal
aborigine
aborning
abort
abound
about
above
aboveboard
aboveground
abovementioned
abrade
Abraham
Abram
Abramson
abrasion
abrasive
abreact
abreast
abridge
abridgment
abroad
abrogate
abrupt
abscess
abscissa
abscissae
absence
absent
absentee
absenteeism
absentia
absentminded
absinthe
absolute
absolution
absolve
absorb
absorbent
absorption
absorptive
abstain
abstention
abstinent
abstract
abstracter
abstractor
abstruse
absurd

I'm sorry, for being not so detailed in my question.. I have some network conection errors :\

Well, I hope that's enough informations :)

thanks in advance :)

Well, I was kind of guessing I could, but I didn't know whether there were any restrictions on the text file (i.e. certain format, must be alphabetized, etc.).

I think your problem may be here:

for(tmp = HT[loc].link, tmp2=tmp->link; tmp2; tmp2 = tmp, tmp = tmp->link)
    if(tmp2->data > x)
        break;

tmp is allowed to become null before exiting the for-loop, then you try to access tmp->link when tmp is null.

I think your problem may be here:

for(tmp = HT[loc].link, tmp2=tmp->link; tmp2; tmp2 = tmp, tmp = tmp->link)
    if(tmp2->data > x)
        break;

tmp is allowed to become null before exiting the for-loop, then you try to access tmp->link when tmp is null.

I don't think so my friend, I did tried this program for integers and it does work efficiently

when I change the input to string it doesn't work!! :'(

I don't think so my friend, I did tried this program for integers and it does work efficiently

when I change the input to string it doesn't work!! :'(

Change this for-loop:

for(tmp = HT[loc].link, tmp2=tmp->link; tmp2; tmp2 = tmp, tmp = tmp->link)
    if(tmp2->data > x)
        break;

to a while loop:

tmp = HT[loc].link;
tmp2=tmp->link;
while (tmp2)
{
    if(tmp2->data > x)
    {
        break;
    }

    tmp2 = tmp;
    if (tmp == NULL)
    {
        cout << "tmp is null" << endl;
    }
    tmp = tmp->link;
}

and run it with the input file you posted here. See if the message in line 13 displays. It did for me. This may not be where the problem is, but it looks to me that this is where it is crashing.

Comments
Thanks for trying to help :)

Umm so what do you think I shall do, any suggestions? :\

thanks anyway for the help..

>>any suggestions ?
New insert function. Note that this version does not use HT[n], but starts at HT[n].link -- simplifies the code.

void insert( int x )
    {
        int loc;

        loc = x % tSize;
		
        Node * tmp2 = 0;
        tmp = HT[loc].link;
        while( tmp )
        {
            if(tmp->data > x)
                break;
            tmp2 = tmp;
            tmp = tmp->link;
        }
        Node *tmp3 = new Node(x);
        if(tmp2 == 0) // insert at head of list
            HT[loc].link = tmp3;
        else if(tmp == 0) // insert at tail
            tmp2->link = tmp3;
        else
        {  // insert somewhere in the middle
            tmp3->link = tmp2->link;
            tmp2->link = tmp3;
        }
        HT[loc].count++;
    } // end function insert
Comments
I don't know how to thank you! :)

Ancient Dragon, thanks a lot for the help

if you have time to explain why does this function (insert) worked with integers? and when I used string it doesn't work!!

otherwise, thanks again really appreciated

<< Adding approved reputation for all who replied and tried to help :)

This question has already been answered. Start a new discussion instead.