C++ Hash Table

I am new to new hash tables and I have this assignment I need to do. I started on the assignment and everything compiles and the program runs fine, but it doesn't do what its suppose to do. Search for a student using his ID. Here is the assignment...

First, create a structure (named Student) the students. The structure contains the following member variables:

An ID consisting of 4 digits. The data type should be a string or c-string instead of an int.
A name, which may have embedded spaces (like "Genghis Khent"). The data type is a string or c-string.
Second, create a text file which lists students (say 25) in the format ID number, space, name. For example:

6666 Mini Me
2333 Help Me
7722 Yogi Beria
Note: There should be no duplicate IDs. However, so there will be some collisions, for about 12 of the 25 students, have 2 students have the same last 2 digits of the ID (e.g., no ID will duplicate, but the last 2 digits of the ID will).

Third, create a hash table class (using .h and .cpp files preferably) whose one member variable points to an array of 100 student structures. The placement of a student structure within the array is based on the last 2 digits of a student's ID. Thus, a student with an ID ending in 45 will go in the 45th subscript ([45]) of the array, unless there's a collision. The hash table class also would include the following member functions:

Here is the file

2300 Robb Arredondo
5401 Chris Campos
6305 Yogi Bear
9108 Yoshi Man
0310 John Du
1812 Maria Yu
4318 Power Ranger
7122 Bob Chan
8225 Will Boo
5324 Ghost Lee
0134 Mary Su
2150 Jane Mary
1100 Gary Campos
2305 Alan Kong
3420 Bill Nye
5608 Alex Garcia
9112 Goku Nani
6750 Paul Avalos
1200 Jason Noni
9005 Oscar Roger
6550 Geo Qwerty
1112 Mini Me
2315 Garfield Beria
4201 Just Saying
2399 Help Me



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
#pragma once
#include <string>

struct Student
{
	std::string m_idNum;
	std::string m_Name;
	Student *next;
};

#pragma once
#include "Student.h"
#include <iostream>

class HashTable
{

private:
	int Hash(const std::string&);
	static const int tableSize = 100;
	Student* listofStudents[tableSize];

public:
	HashTable();
	~HashTable();
	void Insert(std::string ID, std::string name);
	void Retrieve(std::string ID);
};

#include "HashTable.h"


HashTable::HashTable()
{
	for (int i = 0; i < tableSize; i++)
	{
		listofStudents[i] = new Student;
		listofStudents[i]->m_Name = " ";
		listofStudents[i]->m_idNum = " ";
		listofStudents[i]->next = NULL;
	}
}

HashTable::~HashTable()
{
	for (int i = 0; i < tableSize; i++)
	{
		delete listofStudents[i];
	}
}

void HashTable::Insert(std::string ID, std::string name)
{
	int location = Hash(ID);

	if (listofStudents[location]->m_idNum == "")
	{
		listofStudents[location]->m_idNum = ID;
		listofStudents[location]->m_Name = name;
	}
	else
	{
		Student* ptr = listofStudents[location];
		Student* newStudent = new Student;
		newStudent->m_Name = name;
		newStudent->m_idNum = ID;
		newStudent->next = NULL;

		while (ptr->next != NULL)
		{
			ptr = ptr->next;
		}
		ptr->next = newStudent;
	}

}

void HashTable::Retrieve(std::string ID)
{
	int location = Hash(ID);
	bool foundStudent;
	std::string name;

	Student* ptr = listofStudents[location];
	while (ptr != NULL)
	{
		if (ptr->m_idNum == ID);
		{
			foundStudent = true;
			name = ptr->m_Name;
		}
		ptr = ptr->next;
	}

	if (foundStudent == true)
	{
		std::cout << "------------------------------\n";
		std::cout << " Name of Student: " << name << std::endl;
		std::cout << "------------------------------\n";
	}
	else
	{
		foundStudent = false;
		std::cout << "---------------------------------\n";
		std::cout << " No Student exist with that ID" << std::endl;
		std::cout << "----------------------------------\n";
	}

}

int HashTable::Hash(const std::string& key)
{
	int hashVal = 0;
	for (int i = 0; i < key.length(); i++)
	{
		hashVal = int( 37 * hashVal + key[i]);
	}
	hashVal %= tableSize;

	if (hashVal < 0)
		hashVal += tableSize;

	return hashVal;
}

#include <fstream>
#include "HashTable.h"

int Menu();
void FindStudent(HashTable&);

int main()
{
	HashTable hashtable;
	std::ifstream file("students.txt");
	int option;
	std::string studentID;
	std::string studentName;

	if (!file.is_open())
	{
		std::cout << "Error in opening file\n";
	}
	else
	{
		while (file >> studentID >> studentName)
		{
			hashtable.Insert(studentID, studentName);
		}

		file.close();
	}

	do
	{
		option = Menu();

		switch (option)
		{
			case 1: FindStudent(hashtable);
			break;

			case 2:
			break;
		}

	} while (option != 2);

	return 0;
}

int Menu()
{
	int choice;

	std::cout << " 1.)Find Student by ID\n";
	std::cout << " 2.)Exit\n";
	std::cout << " Enter option:";
	std::cin >> choice;

	return choice;
}

void FindStudent(HashTable& hash)
{
	std::string fourdigit;

	std::cout << " Enter Students four digit ID: ";
	std::cin >> fourdigit;

	hash.Retrieve(fourdigit);
}


Last edited on
Topic archived. No new replies allowed.