160 lines
4.0 KiB
C++
160 lines
4.0 KiB
C++
#include <iostream>
|
|
#include <vector>
|
|
#include <fstream>
|
|
|
|
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_entry*> &phonebook) {
|
|
for (phonebook_entry* entry : phonebook) {
|
|
delete entry;
|
|
}
|
|
phonebook.clear();
|
|
phonebook.shrink_to_fit();
|
|
}
|
|
|
|
std::vector<phonebook_entry*> getPhonebookFromFile(std::string filename) {
|
|
std::ifstream file(filename);
|
|
std::string line;
|
|
int rownumber = 0;
|
|
std::vector<phonebook_entry*> 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_entry*> &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_entry*> 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;
|
|
} |