Assign hashkey to .txt file HELP?!

How do I assign the hashkey I created to a .txt file that is being read into a linked list(this is how my instructor said it would be easiest to do)? I have included my code with notes as to where I am getting errors. Please help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
  #pragma once
#pragma warning (disable:4996)
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

const short SIZE_KEY = 32;
const short SIZE_VALUE = 256;
const short DEFAULT_TABLESIZE = 31;

typedef struct Metadata
{
	struct Metadata(char* key, char *value)
	{
		strcpy(this->key, key);
		strcpy(this->value, value);
		next = NULL;
	}

	char key[SIZE_KEY];
	char value[SIZE_VALUE];
	struct Metadata* next;
} MDATA;

class Hashtable
{
	int tablesize;
	int size;
	MDATA** table;
	long hashString(char* key);
	MDATA* find(char* key);
	MDATA* current_entry;
	int current_index;
public:
	Hashtable(int tablesize = DEFAULT_TABLESIZE);
	bool put(char* key, char* value);
	bool get(char* key, char* value);
	bool contains(char* key);
	bool hasNext();
	int getSize();
	void Iterator();
	void getNextKey(char* key);
	void text();
};

struct dictionary
{
	string words;
	dictionary* next;
};
dictionary *list;
dictionary *p, *q;

//main.cpp
#include "Hash.h"
Hashtable::Hashtable(int tablesize)
{
	size = 0;
	this->tablesize = tablesize;        //31
	table = new MDATA*[tablesize];   //31

	for (int i = 0; i < tablesize; i++)
		table[i] = NULL;                 //31 arrays
}

MDATA* Hashtable::find(char* key)
{
	int bucket = hashString(key);
	MDATA* temp = table[bucket];

	while (temp != NULL)
	{
		if (strcmp(key, temp->key) == 0)
			return temp;
		temp = temp->next;
	}
	return NULL;
}

//used to return hash number
long Hashtable::hashString(char* key)    
{
	int n = strlen(key);
	long h = 0;

	for (int i = 0; i < n; i++)
	{
		h = (3 * h + key[i]);
		cout << h;      //Gives me weird outputs like 459..,5908...
	}

	return abs(h % tablesize);
}

bool Hashtable::put(char* key, char* value)
{
	if (find(key) != NULL)
		return false;

	MDATA* entry = new MDATA(key, value);
	int bucket = hashString(key);
	entry->next = table[bucket];
	table[bucket] = entry;
	size++;
	return true;
}

int Hashtable::getSize()
{
	return size;
}

bool Hashtable::get(char* key, char* value)
{
	MDATA* temp = find(key);

	if (temp == NULL)
	{
		value[0] = '\0';
		return false;
	}

	else
	{
		strcpy(value, temp->value);
		return true;
	}
}

bool Hashtable::contains(char* key)
{
	if (find(key) == NULL)
		return false;
	else
		return true;
}

void Hashtable::Iterator()
{
	current_entry = NULL;
	current_index = tablesize;

	for (int i = 0; i < tablesize; i++)
	{
		if (table[i] == NULL)
			continue;
		else
		{
			current_entry = table[i];
			current_index = i;
			break;
		}
	}
}

bool Hashtable::hasNext()
{
	if (current_entry == NULL)
		return false;
	else
		return true;
}

void Hashtable::getNextKey(char* key)
{
	if (current_entry == NULL)
	{
		key[0] = '\0';
		return;
	}
	strcpy(key, current_entry->key);

	if (current_entry->next != NULL)
		current_entry = current_entry->next;

	else
	{
		for (int i = current_index + 1; i < tablesize; i++)
		{
			if (table[i] == NULL)
				continue;
			current_entry = table[i];
			current_index = i;
			return;
		}
		current_entry = NULL;
		current_index = tablesize;
	}
}

void displayAll(Hashtable* hashT)
{
	char key[SIZE_KEY];
	char value[SIZE_VALUE];
	cout << "Current nodes in hashtable: " << endl;
	hashT->Iterator();
	while (hashT->hasNext())
	{
		hashT->getNextKey(key);
		hashT->get(key, value);
		cout << "key: " << key << " value: " << value << endl;
	}
}

int main()
{
	Hashtable* hashT = new Hashtable();
	char key[SIZE_KEY];
	char value[SIZE_VALUE];

	ifstream inputFile;
	list = NULL;
	p = new dictionary;

	inputFile.open("Dictionary.txt");

	while (!inputFile.eof())
	{
		p = new dictionary;
		getline(inputFile, p->words);
		cout << p->words << endl;

		strcpy(key, " ");
		strcpy(value, p->words);   //IS GIVING ME AN ERROR BECAUSE OF MISMATCHED TYPES

		if (!hashT->contains(key))
		{
			cout << "key: " << key << " value: " << value << endl;
			hashT->put(key, value);
		}
	}
	if (list == NULL)
		list = p;
	else
	{
		q = list;
		while (q->next != NULL)
			q = q->next;
		q->next = p;
	}
	inputFile.close();
	/**
	FOR TESTING PURPOSES
	string line_;
	ifstream file_("Dictionary.txt");
	if (file_.is_open())
	{
	while (getline(file_, line_))
	{
	cout << line_ << '\n';
	}
	file_.close();
	}
	
	strcpy(key, "111");
	strcpy(value, "Bob");

	if (!hashT->contains(key))
	{
		cout << "adding node - key: " << key << " value: " << value << endl;
		hashT->put(key, value);
	}

	strcpy(key, "415");
	strcpy(value, "Henry");

	if (!hashT->contains(key))
	{
		cout << "adding node - key: " << key << " value: " << value << endl;
		hashT->put(key, value);
	}
	*/

	displayAll(hashT);

	system("pause");
	return 0;
}

//Dictionary.txt file (full list contains 39 lines of words
/*Alpha Cnetauri Base
Antimatter containment vessel
Barnards Star
Battleship
Beverly Crusher
Data

What sample output should look like:
Battleship - 236
Data - 111*/
Last edited on
The prototype of strcpy() is:
char* strcpy( char* dest, const char* src );

Could you please try to modify your 226th line from:

strcpy(value, p->words);

to

strcpy(value, p->words.c_str());

and tell me if it solved at least one of your issue?
I changed the code a bit and finally got it to do what I wanted it to do. One of the notes from my instructor was that I shouldn't have opened the .txt file in main() but rather use a constructor or member function in my class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#pragma once
#include <iostream>
using namespace std;
#include <fstream>
#include <string>


const int TABLESIZE = 31;

class HashTable
{
	struct Node
	{
		int		collision;
		string	word;
		Node*	next;
	};
	Node* head[TABLESIZE];
public:
	int HashString(string hashkey);
	void put(string dictionary);
	void display();
};

//main file

#include "HashT.h"

int HashTable::HashString(string hashkey)
{
	long hash = 0;

	for (int i = 0; i < hashkey.length(); i++)
		hash = ((hash * i) + (hashkey[i])) % TABLESIZE;

	return abs(hash);
}


void HashTable::put(string dictionary)
{
	int	key = 0;

	Node *newNode, *nodePtr;
	string		temp;
	ifstream	text;


	for (int i = 0; i < TABLESIZE; i++)
		head[i] = NULL;

	text.open(dictionary);
	while (!text.eof())
	{
		newNode = new Node;
		getline(text, temp);

		if (!temp.empty())
		{
			key = HashString(temp);

			newNode->word = temp;
			newNode->next = NULL;
		}

		if (!head[key])
		{
			head[key] = newNode;
			newNode->collision = 0;
		}
		else
		{
			newNode->collision = 1;
			nodePtr = head[key];
			while (nodePtr->next)
			{
				nodePtr = nodePtr->next;
				newNode->collision++;
			}
			nodePtr->next = newNode;
		}
	}
	text.close();
}

void HashTable::display()
{
	Node *displayHash;
	displayHash = new Node;

	for (int i = 0; i < TABLESIZE; i++)
	{
		if (i < TABLESIZE)
		{
			displayHash = head[i];
		}

		while (displayHash)
		{
			if (!displayHash->word.empty())
				cout << "HashKey " << i << '\t' << displayHash->word << "\n" << "Collision:" << '\t' << displayHash->collision << endl;

			displayHash = displayHash->next;
		}
	}
}

int main()
{
	string	file = "dictionary.txt";
	HashTable obj1;

	obj1.put(file);
	obj1.display();

	system("pause");
	return 0;
}
Topic archived. No new replies allowed.