c++ - Searching a Hash Table Saved in a File -


i've written program has created hash table , saved file. supposed write second program allows user enter key, searches file key, , displays information stored in record matching key entered.

my program has created hash table , has saved correct file, however, having trouble searching file. when enter key know in hash table, "this key not found" message.

here code:

my header file:

//prog8.h #include <iostream> #include <fstream> #include <string> using namespace std;  class hash { private:     static const int hashsize = 8;     struct record {         int key;         string name;         int code;         double cost;         record* next;     };     record* hashtable[hashsize]; public:     hash();     int hash(int key);     void addrecord(int key, string name, int code, double cost);     int numinindex(int index);     void saverecords();     void saverecordsinindex(int index);     void findrecord(int key); }; 

my .cpp file containing definitions methods:

//prog8.cpp #include "prog8.h" #include <iostream> #include <fstream> #include <string> using namespace std;  hash::hash() {     for(int = 0; < hashsize; i++) {         hashtable[i] = new record;         hashtable[i]->key = 8;         hashtable[i]->name = "blank";         hashtable[i]->code = 0;         hashtable[i]->cost = 0.0;         hashtable[i]->next = null;     } } void hash::addrecord(int key, string name, int code, double cost) {     int index = hash(key);     if(hashtable[index]->key == 8) {         hashtable[index]->key = key;         hashtable[index]->name = name;         hashtable[index]->code = code;         hashtable[index]->cost = cost;     }     else {         record* ptr = hashtable[index];         record* newrecord = new record;         newrecord->key = key;         newrecord->name = name;         newrecord->code = code;         newrecord->cost = cost;         newrecord->next = null;         while(ptr->next != null) {             ptr = ptr->next;         }         ptr->next = newrecord;     } } int hash::numinindex(int index) {     int count = 0;     if(hashtable[index]->key == 0) {         return count;     }     else {         count++;         record* ptr = hashtable[index];         while(ptr->next != null) {             count++;             ptr = ptr->next;         }     }     return count; } int hash::hash(int key) {     int index = key % 5;     return index; } void hash::saverecords() {     ofstream recordsfile;     recordsfile.open("records.dat");     int number;     for(int = 0; < hashsize; i++) {         number = numinindex(i);         recordsfile << "-------------\n";         recordsfile << "index = " << << endl;         recordsfile << hashtable[i]->key << endl;         recordsfile << hashtable[i]->name << endl;         recordsfile << hashtable[i]->code << endl;         recordsfile << hashtable[i]->cost << endl;         recordsfile << "number of items in index = " << number << endl;         recordsfile << "-------------\n";     } } void hash::saverecordsinindex(int index) {     ofstream recordsfile;     recordsfile.open("records.dat");         record* ptr = hashtable[index];     if(ptr->key == 0) {         cout << "index " << index << " empty";     }     else {         cout << "index " << index << " contains following records:\n";         while(ptr != null) {             recordsfile << "--------------\n";             recordsfile << ptr->key << endl;             recordsfile << "--------------\n";             ptr = ptr->next;             //index++;         }     } } void hash::findrecord(int key) {     ifstream recordsfile;     recordsfile.open("records.dat");     if(!recordsfile.is_open()) {         cerr << "error opening file" << endl;     }     int index = hash(key);     bool wasfound = false;     record* ptr = hashtable[index];     while(ptr != null) {         if(ptr->key == key) {             wasfound = true;             key = ptr->key;         }         ptr = ptr->next;     }     if(wasfound == true) {         cout << key;     }     else {         cout << "there no record matching key " << key << " found."         << endl;     }     recordsfile.close(); } 

my first main file:

//prog8main.cpp #include "prog8.h" #include <iostream> #include <fstream> #include <string> using namespace std;  int main() {     ifstream file;     file.open("prog8.dat");     if(!file.is_open()) {         cerr << "error opening file" << endl;     }     int index;     int key;     string name;     int code;     double cost;     hash hashobj;     if(key != 8) {         while(file >> key && file >> name && file >> code && file >> cost) {             hashobj.addrecord(key, name, code, cost);             hashobj.saverecords();             //hashobj.saverecordsinindex(index);         }     }     file.close();     return 0; } 

and main file search output file:

//prog8search.cpp #include "prog8.h" #include <iostream> #include <fstream> #include <string> using namespace std;  int main() {     int key;     hash hashobj;     cout << "please enter key ";     cin >> key;     hashobj.hash::findrecord(key);      return 0; } 

here example of output i'm getting, complete records.dat file created:

[cs331129@cs ~]$ g++ -o prog8 prog8.cpp prog8main.cpp [cs331129@cs ~]$ prog8 [cs331129@cs ~]$ cat records.dat ------------- index = 0 12345 item06 45 14.2 number of items in index = 2 ------------- ------------- index = 1 34186 item25 18 17.75 number of items in index = 2 ------------- ------------- index = 2 12382 item09 62 41.37 number of items in index = 3 ------------- ------------- index = 3 8 blank 0 0 number of items in index = 1 ------------- ------------- index = 4 12434 item04 21 17.3 number of items in index = 1 ------------- ------------- index = 5 8 blank 0 0 number of items in index = 1 ------------- ------------- index = 6 8 blank 0 0 number of items in index = 1 ------------- ------------- index = 7 8 blank 0 0 number of items in index = 1 ------------- [cs331129@cs ~]$ g++ -o prog8search prog8.cpp prog8search.cpp [cs331129@cs ~]$ prog8search please enter key 12345 there no record matching key 12345 found. 

any appreciated.

ifstream recordsfile; recordsfile.open("records.dat"); if(!recordsfile.is_open()) {     cerr << "error opening file" << endl; } 

here opened file.

yet read nothing ever.

so when stuff like

record* ptr = hashtable[index]; 

the table definition empty since still have read records.dat file.

remember: programming not magic. may seem @ first, nothing ever happens unless code written (by @ all, not you)


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -