May 2, 2017 at 10:13pm May 2, 2017 at 10:13pm UTC
Hello Cplusplus people, I have currently have a problem with my program where I am trying to create a hash table that has a structure of name, last name and ID number where the ID can be find by the user input but when I run my program only the name of the first list pop up and it shows
"Exception thrown: read access violation.
**std::_String_alloc<std::_String_base_types<char,std::allocator<char> > >::_Mysize**(...) returned 0xCCCCCCE0. occurred"
with xstring file popping up anything helps
hash.h
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
#include <iostream>
#include <string>
using namespace std;
#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];
};
hash.cpp
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
#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
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
#include <iostream>
#include <string>
using namespace std;
#include "hash.h"
bool die(const string &msg);
void show(string a[25][2]);
int main()
{
hasht hasha;
string id;
string NAME;
string line;
string a[25][2];
int b = 0;
ifstream myfile("id.txt" );
if (myfile.is_open())
{
while (getline(myfile, line))
{
id = line.substr(0, 4);
NAME = line.substr(5);
cout << id << endl << NAME << endl << endl;
//cout << id << endl << NAME << endl << endl;
a[b][0] = id;
a[b][1] = NAME;
b++;
hasha.insert(id, NAME);
//hasha.insert(id, NAME);
}
myfile.close();
}
else cout << "Unable to open file" ;
show(a);
system("pause" );
//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();
//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;
}
}
Last edited on May 2, 2017 at 10:33pm May 2, 2017 at 10:33pm UTC