Hello I need help with my programming assignment on hashing. I have all the parts working good separately but it just wont when I try to use it together. I'm reading input from text file line by line but when I try to use my hash insert function it crashes. HELP PLEASE.
Homework assignment is the following:
First, create a structure (named Student) that will describe each 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 "John Smith"). 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: Constructor - Assigns to each element of the array the value NULL for the ID and the name. (Those values are how you determine if a slot is "empty". Destructor - If you're using dynamic memory allocation. Otherwise probably not necessary.
insert - Has 2 parameters, representing ID and name, which are assigned to the member variables of the structure instance Assigns a student's information (ID and name) to the proper element of the array. Important: Uses hashing to determine the proper array element, and resolves collisions.
retrieve - Has 1 parameter, the ID, and returns the student's name, or NULL if no student with that ID exists. Important: Uses hashing to find that student in the array, and deals with collisions.
Fourth, create a driver or test file. This code will:
Create an instance of the hash table class.
Load the hash table from the text file, using the insert member function. Display a menu: Find a student by ID. If chosen, the user is prompted to enter a 4 digit ID (no input validation necessary). Using the retrieve member function, the program either will output the name of the student associated with that ID, or report that no student has that ID.
Exit. If chosen, the program will end.
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
|
//hash.h
/*****************************************************/
#ifndef HASH_H
#define HASH_H
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Student
{
string ID;
string name;
Student* next;
};
class hasht {
public:
hasht();
int Hash(string key);
void insert(string ID, string name);
string retrieve(string id);
int NumItemsIndex(int index);
void PrintTable();
private:
static const int tableSize = 100;
Student* HashTable[tableSize];
};
bool die(const string &msg);
void show(string a[100][2]);
#endif
/**********************************************************/
//hash.cpp
/**********************************************************/
#include "hash.h"
hasht::hasht()
{
for (int i = 0; i < tableSize; i++)
{
HashTable[i] = new Student;
HashTable[i]->name = "empty";
HashTable[i]->name = "empty";
HashTable[i]->next = NULL;
}
}
void hasht::insert(string ID, string name)
{
int index = Hash(ID);
if (HashTable[index]->ID == "empty")
{
HashTable[index]->ID = ID;
HashTable[index]->name = name;
}
else
{
for (int i = 1;; i++)
{
if (HashTable[index + i]->ID == "empty")
{
HashTable[index + i]->ID = ID;
HashTable[index + i]->name = name;
break;
}
}
}//else
}
string hasht::retrieve(string id)
{
int index = Hash(id);
string sname = "";
if (HashTable[index]->ID == id)
{
sname = HashTable[index]->name;
}
else
{
for (int i = 1; i <100 - (index + i); i++)
{
if (HashTable[index + i]->ID == id)
{
sname = HashTable[index + i]->name;
break;
}
}
}
return sname;
}
int hasht::NumItemsIndex(int index)
{
int count = 0;
if (HashTable[index]->ID == "empty")
{
return count;
}
else
{
count++;
Student* Ptr = HashTable[index];
while (Ptr->next != NULL)
{
count++;
Ptr = Ptr->next;
}
}
return count;
}
void hasht::PrintTable()
{
int number;
for (int i = 0; i < tableSize; i++)
{
number = NumItemsIndex(i);
cout << "-----------------------\n";
cout << "index = " << i << endl;
cout << HashTable[i]->ID << endl;
cout << HashTable[i]->name << endl;
cout << "# of items = " << number << endl;
cout << "-----------------------\n";
}
}
int hasht::Hash(string key)
{
string value = key.substr(2, 2);
int index = stoi(value);
return index;
}
/***************************************************************/
main.cpp
/**************************************************************/
#include "hash.h"
int main()
{
hasht hasha;
string id;
string NAME;
string line;
string a[25][2];
int b = 0;
ifstream myfile("C:\\Users\\aleXD\\Desktop\\hw4data.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{
id = line.substr(0, 4);
NAME = line.substr(5);
//cout << id << endl << NAME << endl << endl;
a[b][0] = id;
a[b][1] = NAME;
b++;
//hasha.insert(id, NAME);
}
myfile.close();
}
else cout << "Unable to open file";
//show(a);
//system("pause");
string cont;
string cant;
for (int i = 0; i < 25; ++i)
{
cont = a[i][0];
cant = a[i][1];
cout << cont << endl << cant << endl << endl;
hasha.insert(a[i][0], a[i][1]);
}
/*
//hasha.PrintTable();
/*
int num;
string sid, sname;
cout << "1. Find student by ID." << endl << "2. Exit." << endl;
cin >> num;
if (num == 1)
{
cout << "Enter student ID: ";
cin >> sid;
sname = hasha.retrieve(sid);
if (sname == "")
cout << "There is no student with that ID." << endl;
else
cout << sname << endl;
}
else if (num == 2)
{
exit(1);
}
else
{
die("Invalid Input");
}
*/
return 0;
system("pause");
}
bool die(const string &msg)
{
cout << "Fatal error : " << msg << endl;
exit(EXIT_FAILURE);
}
void show(string a[25][2])
{
for (int i = 0; i < 25; ++i)
{
for (int j = 0; j < 2; ++j)
{
cout << a[i][j] << endl;
}
cout << endl;
}
}
/*********************************************************/
|