I have an assignment to make a hash table. It is a phone book with names and phone numbers. The issue is that its not just the first name, last name and phone number. Some names have a middle name included. I don't know the best way to go about reading this in from a file. The hash table is completely done and I have written a basic read in from a file, but had to modify the input file to make this work. The instructions say it must be a name(w/ multiple parts) and a phone number.
We have not learned vectors or stringstream, I have tried using getline and can't figure out if I need to read as characters first or what. I tried .get() as well, I have tried doing a C-string at first and ifdigit(input[0]}} store that into the tel value, I just can't get something to perfectly work.
I have also been trying to search online for something like while(infile != '\n) read the line but it keeps giving me an error.
Please be kind, I have written the entire hash program except for this part.
ex of input data
Taylor marie 939-1512
james wilis thomas 263-9921
Stone Rock 544-2372
how do you plan to extract the data from your table (that is, user puts in what, to get what out)? This is critical to how you deal with it. You can read a whole line with getline, but that may or may not be right thing to do here, depending on your answer.
We have to read it in as name, telephone because I have a function to search the name index and retrieve the phone number. If they all had a first, middle, and last I would do input>>first>>middle>>last>>telephone and then name = first + " " + middle + " " + last; but the issue is that some lines dont have a middle name.
Everything is already written to a file and we just have to process the file's contents.
read it as a line, and break it by spaces, would that work?
find() the first space, substring (remember to remove the spaces as you do it), repeat.
you can count the spaces up front to see if you have a middle.
#include <string>
#include <iostream>
using std::cin;
using std::cout;
using std::string;
int main()
{
string name, number, s;
while (cin >> s) {
if (isdigit(s[0])) {
// It's the phone number
number = s;
cout << "Name=" << name << '\t' << "Number=" << number << '\n';
name.clear();
number.clear();
} else {
// It's part of the name. If we're at the beginning of the
// line (name is empty) then copy s to name. Otherwise
// append a space and then s.
if (!name.empty()) {
name += ' ';
}
name += s;
}
}
}
Taylor marie 939-1512
james wilis thomas 263-9921
Stone Rock 544-2372
John Jangle Jingleheimer Schmidt IV 872-933-1234
Output:
Name=Taylor marie Number=939-1512
Name=james wilis thomas Number=263-9921
Name=Stone Rock Number=544-2372
Name=John Jangle Jingleheimer Schmidt IV Number=872-933-1234
If you're inserting into an empty bucket, then hashTable[index] will be nullptr. You have to check for that case before you check for name.
In fact, if asked to insert a record, shouldn't you ALWAYS insert it? And why insert it at the back of the list when you can insert at the front? Something like:
1 2 3 4 5 6 7 8 9 10 11 12 13
void Hash::insert(string name, string tel)
{
int index = createHash(name);
if(hashTable[index] != nullptr) {
collisions++;
}
database *ptr = new database; // "database" is a bad name for a node in a list.
ptr->name = name;
ptr->tel = tel;
ptr->next = hashTable[index];
hashTable[index] = ptr;
}