how do you make a hashtable store records

Pages: 12
I have made a hashtable that stores numbers perfectly but now I need to change it so that it stores student records ie id number, first name, surname, and GPA. I would really appreciate any help

Somewhere you specified int (or whatever number type you're storing) as what your hash table stores. Instead, specify record, where record is a struct you defined like this:

1
2
3
4
5
6
struct record{
int idNumber;
string firstName;
string surName;
double GPA;
}
Last edited on
ok thanks but i'm still not sure where to put that code.
is it in the header file where i defined all the functions ie
1
2
private:
        int idNum


or in the cpp file
1
2
3
4
5
HashTable::HashTable(void)
	: MAX_ITEMS(10)
	, idNum(0)
{
}

the only other place with idNum is used is when i'm using it for other functions
This HashTable. It stores idNum objects? Replace idNum with record and stick the record definition in a header somewhere.
I just called the numbers it stores idNum. Ya i have a header file will all the other definitions of the functions i just didnt post all the code.
Heres the orginal header file with idNum with will become record:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class HashTable
{
public:
	HashTable(void);
	~HashTable(void);
	void MakeEmpty(void);
	void InsertItem(int item);
	int Hash(void);
	void RetrieveItem(int& item, bool& found,int& itemLocation);
	void DeleteItem(int item);
	bool IsFull(void);
	void Display(void);
private:
	int hashtable[10];
	int MAX_ITEMS;
	const static int Empty = -1;
	const static int deleted = -2;
	int idNum;

	
};

but i tired adding the code you gave me and i just get errors
Last edited on
If you're not actually writing the hash table yourself, why not just use the standard C++ one?


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
#include <map>
#include <string>
#include <iostream>

using std::string;
using std::endl;

struct record{
int idNumber;
string firstName;
string surName;
double GPA;
};

int main()
{

  std::map<int,record> studentRecords;
  int numRecords;
record temp;
  std::cout << "How many student records?" << endl;
  std::cin >>  numRecords;
  for (int eger = 0; eger < numRecords; ++eger)
    {
    
     
      std::cout << "First name?" << endl;
      std::cin >> temp.firstName;
      std::cout << "Surname?" << endl;
      std::cin >> temp.surName;
      std::cout << "GPA?" << endl;
      std::cin >> temp.GPA;
      std::cout << "idNumber?" << endl;
      std::cin >> temp.idNumber;

      studentRecords.insert(std::pair<int,record>(temp.idNumber, temp));
    }

  int errogation = 0;
  do {
    
    std::map<int,record>::iterator it;
    std::cout << "Which record to retrive? (0 to exit)" <<endl;
    std::cin >> errogation;
    if (errogation!=0)
      {
	it = studentRecords.find(errogation);
	std::cout << it->second.firstName << " "
                  << it->second.surName   << " "
                  << it->second.GPA       << " "
                  << it->second.idNumber  << endl;
      }


  }while (errogation !=0);

}

Perhaps you meant unordered_map?
Not particularly, to be honest. Just anything that'll do it.
what do you mean
If you're not actually writing the hash table yourself

I have coded a hash table and don't want to completely change it to use a standard c++ one.
I want to just change the hash table i have to store student records instead of numbers so i know i'll need to make changes to a few of the other functions
Last edited on
If you wrote the hash table yourself, how come you don't know how to change what it stores?
because i'm not sure where to put
1
2
3
4
5
6
struct record{
int idNumber;
string firstName;
string surName;
double GPA;
};

in the code that i have
I tired putting it in the header file but i got errors so i'm guessing it needs to go in the .cpp file but i'm not sure exactly where it goes.
I'm sure i would be able to do it if i just had to add a name instead of a number but since i have four items to add i'm not sure how to do it and then i have to change bits of my code to store the record not a number.
i could post the rest of my code here if that would help
hello?
kw1991, you can put it in it's own header file, and just include the header in the cpp file. You will have to change the way that you access the records, though, since there is another level of abstraction involved. Instead of just directly accessing the idNum from the hash table, you'll have to access the idNumber portion of the record from the hash table.
ok I'll make a new header. So would I just have to replace all the idNum's with idNumber?
No, you'll have to replace them with something like
myHash.myRecord.idNumber
@ciphermagi I put the code in its own header like you suggested but I'm gettin an error with string, its says it is undefined
1
2
3
4
5
6
7
8
9
10
#include "StdAfx.h"

struct record
{
int idNumber;
string firstName;
string surname;
int GPA;
};

I included "StdAfx.h and #include <string> is in that header file.
Any idea how to fix this?
1
2
3
4
5
6
7
8
#include "StdAfx.h"
#include <string>
struct record {
    int idNumber;
    std::string firstName;
    std::string surname;
    int GPA;
};


You could also use the using directive like so:
using std::string
I don't recommend this, however, it's better to avoid any type of 'using' directive when you're in a header file.
it worked thanks but i'm not sure how to change the idNum to the idNumber from records.
I know you suggested something like myHash.myRecord.idNumber but everything i try gives errors.

heres some of the code HashTable.cpp, maybe you could help me if fix it?
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
#include "StdAfx.h"
#include "HashTable.h"
#include "Records.h"


HashTable::HashTable(void)
	: MAX_ITEMS(10)
	, idNum(0)
{
	MakeEmpty();
}


HashTable::~HashTable(void)
{
}

void HashTable::InsertItem(int item) //insert item into Hash table
{
	idNum = item;
	int location = Hash();
	while (hashtable[location] != Empty && hashtable[location] != deleted)
	location = (location + 1) % MAX_ITEMS;
	hashtable[location] = item;
}


int HashTable::Hash(void)
{
	return (idNum % MAX_ITEMS);
}
I'm pretty sure that in order to change a member, you have to directly reference that member. So you can't change idNum, since it's not a member of your struct anymore, you'll have to change idNumber (if that's what you named the variable). Just go through and make sure all of your variable names match up properly.
sorry i dont really understand. You told me that i cant just replace idNum with idNumber so how do i reference it?

I need to get rid of all the idNum's in the code since i'm not inserting just numbers into the code anymore, I need to insert idNumber, firstName, surname and GPA.

Like when i insert a new record into the table it will ask for the idNumber, firstName, surname and GPA and when i search for a record i just enter the idNumber and all the other information comes up. thats what i'm hoping to do anyway

Pages: 12