From 36502ffbafc38c3d818d6bcc9138400e8ca4635b Mon Sep 17 00:00:00 2001 From: Sebastian Brosch Date: Fri, 1 Nov 2024 13:51:56 +0100 Subject: [PATCH] added solution for phonebook with file save and load --- VL03-03-01/main.cc | 160 +++++++++++++++++++++++++++++++++++++++ VL03-03-01/phonebook.txt | 6 ++ 2 files changed, 166 insertions(+) create mode 100644 VL03-03-01/main.cc create mode 100644 VL03-03-01/phonebook.txt diff --git a/VL03-03-01/main.cc b/VL03-03-01/main.cc new file mode 100644 index 0000000..74a9b3d --- /dev/null +++ b/VL03-03-01/main.cc @@ -0,0 +1,160 @@ +#include +#include +#include + +typedef struct phonebook_entry { + std::string firstname; + std::string lastname; + std::string phone; +} phonebook_entry; + +int showMenuSelection() { + std::cout << std::endl << std::endl; + std::cout << "-- Phonebook --" << std::endl; + std::cout << "\t 1 - New Entry" << std::endl; + std::cout << "\t 2 - Search" << std::endl; + std::cout << "\t 3 - Clear" << std::endl; + std::cout << "\t 4 - List" << std::endl; + std::cout << "\t 9 - Exit" << std::endl; + std::cout << std::endl << "Selection: "; + int selection; + std::cin >> selection; + return selection; +} + +void clear(std::vector &phonebook) { + for (phonebook_entry* entry : phonebook) { + delete entry; + } + phonebook.clear(); + phonebook.shrink_to_fit(); +} + +std::vector getPhonebookFromFile(std::string filename) { + std::ifstream file(filename); + std::string line; + int rownumber = 0; + std::vector phonebook; + phonebook_entry* entry; + + if (file.is_open()) { + while (file >> line) { + switch (rownumber % 3) { + case 0: + entry = new phonebook_entry; + entry->firstname = line; + break; + + case 1: + entry->lastname = line; + break; + + case 2: + entry->phone = line; + phonebook.push_back(entry); + break; + } + rownumber++; + } + + file.close(); + } else { + std::cout << "Error opening file!" << std::endl; + } + + return phonebook; +} + +void listPhonebook(std::vector &phonebook) { + for (phonebook_entry* entry : phonebook) { + std::cout << "Firstname: " << entry->firstname << std::endl; + std::cout << "Lastname: " << entry->lastname << std::endl; + std::cout << "Phone: " << entry->phone << std::endl; + std::cout << std::endl; + } +} + +int main() { + std::vector phonebook; + phonebook = getPhonebookFromFile("phonebook.txt"); + + while (int selection = showMenuSelection()) { + phonebook_entry* entry = new phonebook_entry; + + switch (selection) { + case 1: { + std::cout << "-- New Entry (Phonebook) --" << std::endl; + std::cout << "Firstname: "; + std::cin >> entry->firstname; + std::cout << "Lastname: "; + std::cin >> entry->lastname; + std::cout << "Phone: "; + std::cin >> entry->phone; + phonebook.push_back(entry); + } break; + + case 2: { + std::cout << std::endl << std::endl; + std::cout << "-- Search (Phonebook) --" << std::endl; + std::cout << "Lastname: "; + std::string lastname; + std::cin >> lastname; + + bool found = false; + + std::cout << std::endl << std::endl; + std::cout << "-- Search Result --" << std::endl; + + for (phonebook_entry* entry : phonebook) { + if (entry->lastname == lastname) { + std::cout << "Lastname: " << entry->firstname << std::endl; + std::cout << "Firstname: " << entry->lastname << std::endl; + std::cout << "Phone: " << entry->phone << std::endl; + found = true; + } + } + + if (found == false) { + std::cout << "No entry found!" << std::endl; + } + } break; + + case 3: { + clear(phonebook); + } break; + + case 4: { + std::cout << std::endl << std::endl; + std::cout << "-- List --" << std::endl; + listPhonebook(phonebook); + } break; + + case 9: { + std::ofstream file("phonebook.txt"); + + if (file.is_open()) { + if (phonebook.empty() == false) { + for (phonebook_entry* entry : phonebook) { + file << entry->firstname << std::endl; + file << entry->lastname << std::endl; + file << entry->phone << std::endl; + } + } else { + file.clear(); + } + + file.close(); + } + + clear(phonebook); + return 0; + } break; + + default: + std::cout << std::endl << std::endl; + std::cout << "Unknown selection. Please try again!"; + } + } + + return 0; +} \ No newline at end of file diff --git a/VL03-03-01/phonebook.txt b/VL03-03-01/phonebook.txt new file mode 100644 index 0000000..1851605 --- /dev/null +++ b/VL03-03-01/phonebook.txt @@ -0,0 +1,6 @@ +Mary +Johnson +714-895-2324 +Christopher +Smith +205-448-1563