Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Okay so I am doing a Hash table code to try and implement 4 different function i

ID: 3602702 • Letter: O

Question

Okay so I am doing a Hash table code to try and implement 4 different function in a menu. I am reading and writing to a binary file which I have named. The topic is flight numbers. I want to use the flight numbers as the key for my hash table. I have gotten it to search correctly and display, but for some reason the hashfile breaks when trying to perform the menu function after one another. That's problem one. The next problem I am having is that I want to add and remove from the Hash table as well. I am completely lost on how to implement a remove function. I think I am close with the adding function, but it's not quite working right. Please help! I've bolded certain parts of the code that I am concerned about. I also have the header files and other source files. I will put those below the main for easy reference.

#include   // Used for input and output.
#include   // Used for file input and output.
#include
#include   // Used for getch().

#include "Header.h"

using namespace std;

// Hash table and hash file functions.
int initializeHashTable();
void writeBinary(int position, Flight theFlight);
void readBinary(int position, Flight& theFlight);
void displayFlights();
void createFlights();
void addFlights(Flight theFlight);
void search();
int hashFunction(int key);
bool addable(int position);
int quadraticProbe(int position);
void remove();

int key;

void pressAnyKey();

void clrscr()
{
system("cls");
}


// Hash table data.
const static int FILE_LENGTH = 41; // Several collision/probing techniques work best if the length
         // of possible records is a prime number.
const static int REMOVED = -2; // If this is to be placed in the key value, then it must be outside of valid range.
const static int EMPTY = -1;
const Flight placeHolderFlight("------", "-----", 0, EMPTY);
const Flight removedFlight("XXXX", "XXXXX", 0, REMOVED);
fstream hashFile;

int main()
{
cout << "Initialize the Hashtable with our 8 predetermined flights: " << endl;
initializeHashTable();   
pressAnyKey();
createFlights();     
pressAnyKey();
clrscr();

cout << "Show the hastable of size 41. (-1 represents the Flight place is empty. -2 represents the flight has been removed.) " << endl;
pressAnyKey();
displayFlights();
pressAnyKey();
clrscr();

hashFile.close();

// Write objects to the binary file.
fstream file("test-binary.dat", ios::binary | ios::in | ios::out | ios::trunc);
if (!file.is_open())
{
  cout << "error while opening the file";
}

int ch;
while (1)
{

  cout << " " << endl;
  cout << "======================================================" << endl;
  cout << " This is a hash table representing Flights departing " << endl;
  cout << " The list reads: Airline, Destination, Time, Flight Number " << endl;
  cout << "-------------------------------------------------------" << endl;
  cout << " Hash Table Options " << endl;
  cout << " ----------------------------- " << endl;
  cout << " 1. Add a Flight " << endl;
  cout << " 2. Search for a Flight " << endl;
  cout << " 3. Remove a Flight " << endl;
  cout << " 4. View all Flights " << endl;
  cout << " 5. Exit " << endl;

  cout << " Enter your choice : ";
  cin >> ch;
  switch (ch)
  {
  case 1:
  {
   cout << " Add a new Flight to the Hash table: " << endl;
   addFlights(Flight(" "," ",0,0));
   pressAnyKey();
   clrscr();
   break;
  case 2:
   cout << "The current list of Flights with their Hash #'s are below: " << endl;
   initializeHashTable();
   createFlights();
   cout << " Enter the Hash value for the flight you want to search for " << endl;
   cin >> key;
   clrscr();
   search();
   pressAnyKey();
   clrscr();
   break;
  case 3:
   cout << " The Current list of Flights with their Hash #'s are below: " << endl;
   initializeHashTable();
   createFlights();
   cout << " Enter the Hash value for the flight you want to remove from the list " << endl;
   cin >> key;

   pressAnyKey();
   clrscr();
   break;
  case 4:
   initializeHashTable();
   createFlights();
   displayFlights();
   pressAnyKey();
   clrscr();
   break;
  case 5: return 0;
  }
}
}
}

int initializeHashTable()
{
// Open hash file.
hashFile.open("FlightTable.dat", ios::in | ios::out | ios::binary | ios::trunc);
if (!hashFile)
{
  cout << "Hash file error - file did not open successfully "
   << "Program will end. ";
  pressAnyKey();
  return 1;
}
// Initialize hash file (hash table) with empty values.
for (int position = 0; position < FILE_LENGTH; position++)
{
  writeBinary(position, placeHolderFlight);
}
return 0; // Successful completion of hash file intialization.
}

void writeBinary(int position, Flight theFlight)
{
hashFile.seekp(position * sizeof(Flight));
hashFile.write((char *)&theFlight, sizeof(Flight));
hashFile.flush();

hashFile.seekp(0 * sizeof(Flight));
}

void readBinary(int position, Flight& theFlight)
{
hashFile.seekg(position * sizeof(Flight));
hashFile.read((char *)&theFlight, sizeof(Flight));
}

void displayFlights()
{
Flight tempFlight; // Used to read object data in binary format from file.  
cout << "Displaying hash table contents: ";
for (int position = 0; position < FILE_LENGTH; position++)
{
  tempFlight = Flight("", "", 0, 0);
  readBinary(position, tempFlight);
  cout << "Display: " << position << ": " << tempFlight << endl;
}
cout << "End of hash table. ";
}

// Create Flights
void createFlights()
{
addFlights(Flight("Delta", "Mobile to New York", 4, 756));
addFlights(Flight("United", "Africa to Japan", 2, 135));
addFlights(Flight("Spirit", "Canada to Europe", 3, 843));
addFlights(Flight("Southwest", "New Orleans to Atlanta", 6, 543));
addFlights(Flight("Delta", "Honduras to Mexico", 1, 431));
addFlights(Flight("United", "United States to North Korea", 7, 432));
addFlights(Flight("Spirit", "Australia to Russia", 8, 613));
addFlights(Flight("American", "Alaska to Hawaii", 5, 134));
}

void addFlights(Flight theFlight)
{
// Get hash value. It's best if the key is unique and if the keys' hash values are spread
// evenly by the hash function.  
int position = hashFunction(theFlight.getFlightNumbers());
if (!addable(position))
{
  // Probe quadratically as needed to get the correct final position.
  position = quadraticProbe(position);
}
// Add the Flight
writeBinary(position, theFlight);   // Insert into hash table.
cout << "Flight: " << theFlight << endl;
}

int hashFunction(int key)
{
int position = key % FILE_LENGTH;

// cout for demo purposes only.
cout << "Hash value: " << position << endl;
return position;
}

bool addable(int position)
{
Flight flight;
readBinary(position, flight);
return (flight.getFlightNumbers() == EMPTY);
}

int quadraticProbe(int position)
{
do // Increment position until an addable position is reached.
{
  if (position < FILE_LENGTH - 1)
  {
   position = (position + 1) ^ 2;
  }
  else // If the end of the file is reached
  {  // restart in the first position.
   position = 0;
  }
} while (!addable(position));
return position;  // Return the appropriate position to add the book.
}

// Press any key to continue.
void pressAnyKey()
{
cout << "Press any key to continue" << endl << endl;
_getch();     // Waits and gets next character entered.  
}


void search()
{
Flight tempFlight; // Used to read object data in binary format from file.  
cout << "Searching for Flight by Hash value: ";
if (key)
{
  tempFlight = Flight("", "", 0, 0);
  readBinary(key, tempFlight);
  cout << "Display: " << key << ": " << tempFlight << endl;
}
else
{
  cout << " Key not found" << endl;
}
cout << "End of hash table. ";
}

-

-

-

-

-

-

-

-

-

-

Header File:

using namespace std;

// Preprocessing directives that prevents multiple definitions.
#ifndef Flight_H
#define Flight_H

// Good resource for class definition with strings to be used for
// binary file: https://www.youtube.com/watch?v=P7XGOBoVzW4

class Flight
{
// Data attributes.
char Airline[80];
char Destination[80];
int Time;
int FlightNumbers;

// Declaring friends.
friend ostream &operator << (ostream& out, const Flight& theFlight);
public:
Flight();
Flight(char *theAirline, char *theDestination, int theTime, int theFlightNumbers);
void print();

// Get and set methods or member functions.
string getAirline() const;
string getDestination() const;
int getTime() const;
int getFlightNumbers() const;
void setAirline(char *theAirline);
void setDestination(char *theDestination);
void setTime(int theTime);
void setFlightNumbers(int theFlightNumbers);
};

#endif

-

-

-

-

-

-

-

-

-

-

Second Source File

#include   // Used for input and output.
#include   // Used for file input and output.

#include "Header.h"  // Notice "" marks to identify our own header files.

using namespace std;

///////////////////////
// Constructors.
Flight::Flight()
{
setAirline("");
setDestination("");
setTime(0);
setFlightNumbers(0);
}

Flight::Flight(char *theAirline, char *theDestination, int theTime, int theFlightNumbers)
{
setAirline(theAirline);
setDestination(theDestination);
setTime(theTime);
setFlightNumbers(theFlightNumbers);
}

///////////////////////
// Get and set methods.

string Flight::getAirline() const
{
return Airline;
}

string Flight::getDestination() const
{
return Destination;
}

int Flight::getTime() const
{
return Time;
}

int Flight::getFlightNumbers() const
{
return FlightNumbers;
}

void Flight::setAirline(char *theAirline)
{
strcpy_s(Airline, theAirline);  // strcpy has been deprecated. _s functions are more secure variants.
}

void Flight::setDestination(char *theDestination)
{
strcpy_s(Destination, theDestination);
}

void Flight::setTime(int theTime)
{
if (theTime < 0)
{
  Time = 0;
}
else
{
  Time = theTime;
}
}

void Flight::setFlightNumbers(int theFlightNumbers)
{
// ISBN's must be 9 digit positive numbers or negative numbered flag conditions.
if ((FlightNumbers < 1000) && (theFlightNumbers > 0))
{
  FlightNumbers=theFlightNumbers;
}
else
{
  FlightNumbers = theFlightNumbers;
}
}

///////////////////////
// Utility functions.

void Flight::print()
{
cout << Airline << " Leaves at " << Time << " from " << Destination << " with flight #: " << FlightNumbers << " " << endl;
}

ostream &operator << (ostream& out, const Flight& theFlight)
{
out << theFlight.Airline << " Airlines in route from " << theFlight.Destination << " at " << theFlight.Time << " o clock " <<" with flight # "<< theFlight.FlightNumbers<<" "<< endl;
return out;
}

Explanation / Answer

You are opening the file in InitiateHashFile but after that file is left like that , its not closed.

You have implemented close only in Main function before Switch statement, but file is being opened in switch statement again.

If you dont close it your file object will be alive which consumes the space. That might be the cause for sudden breakage

Now coming to remove function

As your data is completely in an external file

For Adding in HashMap

I am giving out a standard example , before writing , always a read operation has to be performed

Try this out, this should solve your problem , if not do similarly how we handled delete case

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote