From 0e1424ea3fe4c26ea8357e13684929f09eda6138 Mon Sep 17 00:00:00 2001 From: Iurii Tatishchev Date: Thu, 20 Jun 2024 12:45:56 -0700 Subject: [PATCH] handle absence of default input file, fix delete cpuId input --- HashTable.h | 2 +- fio.cpp | 18 +++++++++++++----- main.cpp | 22 +++++++++++++--------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/HashTable.h b/HashTable.h index 34b0f33..4263857 100644 --- a/HashTable.h +++ b/HashTable.h @@ -27,7 +27,7 @@ private: public: HashTable() { count = 0; - hashSize = 3; + hashSize = 7; hashAry = new HashNode[hashSize]; } diff --git a/fio.cpp b/fio.cpp index 1477925..b88bd9f 100644 --- a/fio.cpp +++ b/fio.cpp @@ -10,8 +10,8 @@ using namespace std; int findHashSize(const string &filename) { ifstream inputFile(filename); - if (!inputFile) { - cout << "Error opening the input file: \"" << filename << "\"" << endl; + if (!inputFile.good()) { + // cout << "Error opening the input file: \"" << filename << "\"" << endl; return -1; } // cout << "Reading data from \"" << filename << "\"" << endl; @@ -32,9 +32,9 @@ void insertFile(const string &filename, BinarySearchTree &bst, HashTable ifstream inputFile(filename); // cout << "Reading data from \"" << filename << "\"" << endl; - if (!inputFile) { - cout << "Error opening the input file: \"" << filename << "\"" << endl; - exit(EXIT_FAILURE); + if (!inputFile.good()) { + cout << "Error opening the input file: \"" << filename << "\". Skipping...\n"; + return; } string line; @@ -47,6 +47,13 @@ void insertFile(const string &filename, BinarySearchTree &bst, HashTable getline(temp, name, ';'); + // check if the CPU is already in the hash table + CPU key(name, -1, -1, "", -1); + if (hash.search(key, key, key_to_index) != -1) { + cout << "Duplicate CPU \"" << name << "\" found in file. Skipping...\n"; + continue; + } + temp.ignore(); getline(temp, strToNum, ';'); releaseYear = stoi(strToNum); @@ -71,6 +78,7 @@ void insertFile(const string &filename, BinarySearchTree &bst, HashTable } inputFile.close(); + cout << "Data from file \"" << filename << "\" added.\n"; } diff --git a/main.cpp b/main.cpp index b0c9cda..f15d6e6 100644 --- a/main.cpp +++ b/main.cpp @@ -49,11 +49,15 @@ int main() { printHelp(); int hashSize = findHashSize(DEFAULT_FILE); - HashTable cpuTable = HashTable(hashSize); + HashTable cpuTable = HashTable(hashSize == -1 ? 7 : hashSize); BinarySearchTree cpuTree; - // Read initial data from output file - insertFile(DEFAULT_FILE, cpuTree, cpuTable); + // Read initial data from output file if it exists + if (hashSize != -1) { + insertFile(DEFAULT_FILE, cpuTree, cpuTable); + } else { + cout << "Could not open default file \"" << DEFAULT_FILE << "\". Starting with an empty database.\n"; + } DisplayManager displayManager(&cpuTable, &cpuTree); SearchManager searchManager(&cpuTable); @@ -148,19 +152,19 @@ void handleFileInput(HashTable &hashTable, BinarySearchTree &tree) cin >> filename; insertFile(filename, tree, hashTable); - cout << "Data from file \"" << filename << "\" added.\n"; } void deleteCPU(HashTable &hashTable, BinarySearchTree &tree, UndoManager &undoManager) { string cpuId; cout << "Enter CPU ID to delete: "; - cin >> cpuId; + cin.ignore(); + getline(cin, cpuId); + CPU cpu(cpuId, 0, 0, "", 0.0); CPU cpuFound; - while (hashTable.search(cpuFound, cpu, key_to_index) == -1) { - cout << "CPU ID not found. Enter a valid CPU ID: "; - cin >> cpuId; - cpu = CPU(cpuId, 0, 0, "", 0.0); + if (hashTable.search(cpuFound, cpu, key_to_index) == -1) { + cout << "CPU ID \"" << cpuId << "\" not found.\n"; + return; } hashTable.remove(cpuFound, cpu, key_to_index); undoManager.addToUndoStack(cpuFound);