I'm currently writing a program that uses hash tables searching through ship records. It's based off a text file I named "shipRecords.txt" containing the following:
1009 1 "Royal Queen" 2015 160
1010 2 "Carnival" 2016 1600
1019 1 "Ocean King" 2013 110
1029 2 "Royal Prince" 2012 2000
1039 2 "Royal Princess" 2010 2100
1014 2 "Royal Caribbean" 2016 1600
The thing I'm having trouble with is my addInfo() function that's supposed to do three things:
Although my code compiles fine, there's a error somewhere that's preventing to display the expected output. If anyone can help me out on how to fix this, I appreciate it!
Current Output
PARSING STRING: 1009 1 "Royal Queen" 2015 160
PARSING STRING: 1010 2 "Carnival" 2016 1600
PARSING STRING: 1019 1 "Ocean King" 2013 110
Process returned -1073741819 (0xC0000005) execution time : 1.083 s
Current Program Code
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
struct ShipRecord
{
int serialNum;
int shipType;
string name;
int year;
int cap;
ShipRecord* link;
};
const int SIZE = 10;
class HashMgr
{
ShipRecord* hashTable[SIZE] = {nullptr};
public:
HashMgr()
{
string line;
ifstream inputFile;
inputFile.open("shipRecords.txt");
if(inputFile.is_open())
{
while (!inputFile.eof())
{
getline(inputFile, line);
addInfo(line);
}
inputFile.close();
}
}
~HashMgr()
{
/// To DO: Add your code here to ensure no memory leak occurs
for (int i = 0; i < SIZE; ++i)
{
while (hashTable[i] != nullptr)
{
ShipRecord* tempRecord = hashTable[i];
hashTable[i] = tempRecord->link;
delete tempRecord;
}
}
}
HashFunc(int serialNum)
{
return serialNum % SIZE;
}
// Parsing string into tokens.
vector<string> parseString(string s)
{
vector<string> fields;
size_t pos = 0;
string temp = s;
cout << '\n' << "PARSING STRING: " << s << '\n';
while(temp.length() > 1)
{
if (temp[0] == ' ') // extra white space between tokens
{
temp = temp.substr(1);
}
else if (temp[0] == '"') // case of double quoted enclosed token
{
pos = temp.find_first_of('\"', 1);
fields.push_back(temp.substr(1, pos - 1));
temp = temp.substr(pos + 1);
}
else // case of space separated token
{
pos = temp.find_first_of(' ');
if (pos != string::npos)
{
fields.push_back(temp.substr(0, pos));
}
else
{
fields.push_back(temp.substr(0, temp.length() - 1));
break;
}
temp = temp.substr(pos);
}
}
return fields;
}
void addInfo(string line)
{
vector<string> fields = parseString(line);
// Adding tokens to an object.
ShipRecord* sr = new ShipRecord;
sr->serialNum = stoi(fields[0]);
sr->shipType = stoi(fields[1]);
sr->name = fields[2];
sr->year = stoi(fields[3]);
sr->cap = stoi(fields[4]);
// Linking objects to the Hash Table.
int bucket = sr->serialNum % SIZE;
if(hashTable[bucket] == nullptr)
{
hashTable[bucket] = sr;
}
else
{
ShipRecord* ptr = hashTable[bucket];
while(ptr->link != nullptr)
{
ptr = ptr->link;
ptr->link = sr;
}
}
}
void displayOne(int serialNum)
{
int bucket = HashFunc(serialNum);
ShipRecord* tempRecord = hashTable[bucket];
while(tempRecord != nullptr && tempRecord->serialNum != serialNum)
{
tempRecord = tempRecord->link;
}
if(tempRecord == nullptr)
{
cout << serialNum << " <- This ship record does not exist." << endl;
}
else if(tempRecord->serialNum == serialNum)
{
cout << tempRecord->serialNum << setw(10)
<< tempRecord->shipType << setw(10)
<< tempRecord->name << setw(10)
<< tempRecord->year << setw(10)
<< tempRecord->cap << setw(10) << endl;
}
}
void displayAll()
{
for(int i = 0; i < SIZE; i++)
{
ShipRecord* tempRecord = hashTable[i];
while(tempRecord != nullptr)
{
cout << tempRecord->serialNum << setw(10)
<< tempRecord->shipType << setw(10)
<< tempRecord->name << setw(10)
<< tempRecord->year << setw(10)
<< tempRecord->cap << setw(10) << endl;
tempRecord = tempRecord->link;
}
}
}
void deleteOne(int serialNum)
{
cout << "Ship record " << serialNum << " deleted!" << endl;
int bucket = HashFunc(serialNum);
ShipRecord* tempRecord = hashTable[bucket];
ShipRecord* link;
while(tempRecord != nullptr && tempRecord->serialNum != serialNum)
{
link = tempRecord->link;
free(tempRecord);
tempRecord = link;
}
}
};
int main()
{
HashMgr hm;
cout << "displayAll()" << endl << endl;
hm.displayAll();
cout << "displayOne()" << endl << endl;
hm.displayOne(1009);
hm.displayOne(1010);
hm.displayOne(1019);
hm.displayOne(1029);
hm.displayOne(1039);
hm.displayOne(1014);
hm.displayOne(1008); /// Prompt a message to that the record does not exist
hm.deleteOne(1009);
hm.deleteOne(1039);
cout << "displayAll()" << endl << endl;
hm.displayAll();
return 0;
}
Expected Output
PARSING STRING:" 1009 1 "Royal Queen" 2015 160"
---------------
1009 1
Royal Queen
2015 160
---------------
PARSING STRING:" 1010 2 "Carnival" 2016 1600"
---------------
1010 2
Carnival
2016 1600
---------------
PARSING STRING:" 1019 1 "Ocean King" 2013 110"
---------------
1019 1
Ocean King
2013 110
---------------
PARSING STRING:" 1029 2 "Royal Prince" 2012 2000"
---------------
1029 2
Royal Prince
2012 2000
---------------
PARSING STRING:" 1039 2 "Royal Princess" 2010 2100"
---------------
1039 2
Royal Princess
2010 2100
---------------
PARSING STRING:" 1014 2 "Royal Caribbean" 2016 1600"
---------------
1014 2
Royal Caribbean
2016 1600
---------------
displayAll()
Bucket #0
1010 2 "Carnival" 2016 1600
Bucket #4
1014 2 "Royal Caribbean" 2016 1600
Bucket #9
1009 1 "Royal Queen" 2015 160
1019 1 "Ocean King" 2013 110
1029 2 "Royal Prince" 2012 2000
1039 2 "Royal Princess" 2010 2100
displayOne()
Bucket #0
1010
Bucket #4
1014
Bucket #8
1008 <- This record does not exist!
Bucket #9
1009
1019
1029
1039
displayAll()
Bucket #0
1010 2 "Carnival" 2016 1600
Bucket #4
1014 2 "Royal Caribbean" 2016 1600
Bucket #9
1019 1 "Ocean King" 2013 110
1029 2 "Royal Prince" 2012 2000
Deleted ship record (1009)!
Deleted ship record (1039)!
User contributions licensed under CC BY-SA 3.0