Hash table with multiple inputs

Apr 6, 2020 at 2:44am
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

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
//insert the values into the hash function
void Hash::insert(string name, string tel)
{
    int index = createHash(name);
    
    if(hashTable[index]->name == "")
    {
        hashTable[index]->name = name;
        hashTable[index]->tel = tel;
    }
    
    else
    {
        database* ptr = hashTable[index];
        database* n = new database;
        collisions++;
        n->name = name;
        n->tel = tel;
        n->next = NULL;
        
        while(ptr->next != NULL)
        {
            ptr = ptr->next;
        }
        
        ptr->next = n;
    }
}

void Hash::printTable()  //function to print the table 
{
    int number;
    
    for(int i=0; i<tableSize; i++)
    {
        number = numItemsIndex(i);
        cout<<hashTable[i]->name<<" "<<hashTable[i]->tel<<endl;
    }
}
//in main 
Hash database;
  
 while(infile)
  {
      infile>>name>>tel;  
      database.insert(name, tel);
  }
  
Last edited on Apr 6, 2020 at 2:56am
Apr 6, 2020 at 3:04am
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.
Last edited on Apr 6, 2020 at 3:05am
Apr 6, 2020 at 3:24am
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.
Last edited on Apr 6, 2020 at 3:25am
Apr 6, 2020 at 12:57pm
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.
Apr 6, 2020 at 1:27pm
To parse the input, read it one word at a time. If the word starts with a digit, it's the number. Otherwise append it to the name.
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
#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;
	}
    }
}


Input:
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

Apr 6, 2020 at 1:32pm
Thank you guys so much and thank you for being nice.
Apr 6, 2020 at 1:33pm
Also, your insert method is buggy.

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;
}


Topic archived. No new replies allowed.