Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "phonebook.h"
- typedef void (*fPointerType)(phonebook& phonebook);
- string name = "";
- const int NUM_FUNCTIONS = 5;
- int DoPrompt();
- void AddEntry(phonebook& phonebook);
- void EditContact(phonebook& phonebook);
- void DeleteItem(phonebook& phonebook);
- void SearchPhonebook(phonebook& phonebook);
- void Display(phonebook& phonebook);
- void exitContact();
- string getValue(string prompt);
- int main()
- {
- phonebook phBook;
- fPointerType fPointer[NUM_FUNCTIONS] = {
- AddEntry,
- EditContact,
- DeleteItem,
- SearchPhonebook,
- Display };
- while (true)
- {
- int choice = DoPrompt();
- if (choice >= 0 && choice < 6)
- {
- (*fPointer[choice])(phBook);
- }
- else
- {
- cout << "Number is not valid";
- }
- }
- }
- void AddEntry(phonebook& phonebook)
- {
- int n = 0;
- system("cls"); // clear the console screen1
- cout << "Add Numbers" << endl
- << endl
- << endl;
- cout << "Enter Your Entries:-";
- cin >> n;
- cin.ignore(); // ignore the newline character left int the buffer
- if (n < 100)
- {
- for (int i = 0; i < n; i++)
- {
- string name = getValue("Enter Your Name");
- string number = getValue("Enter Your Number");
- phonebook.addEntry(name, number);
- // convert the number string to an long long
- }
- }
- else
- {
- cout << "Out Of Memory...." << endl
- << "Your Can Enter 99 Entries";
- cin.get();
- }
- }
- void EditContact(phonebook& phonebook)
- {
- cout << "Not Implemented Yet";
- }
- void DeleteItem(phonebook& phonebook)
- {
- system("cls");
- cin.ignore();
- name = "";
- cout << "Enter Name You Want To Delete:-";
- string name = getValue("Enter Name You Want to Delete");
- cout << "Enter to exit...";
- phonebook.removeEntry(name);
- cin.get();
- }
- void SearchPhonebook(phonebook& phonebook)
- {
- system("cls");
- cout << "Find" << endl;
- string name = getValue("Enter Search Name");
- phonebook.searchEntry(name);
- cout << "Press Enter To Exit...";
- cin.get();
- }
- void exitContact()
- {
- cout << "Bye.....";
- exit(0);
- }
- void Display(phonebook& phonebook)
- {
- system("cls");
- phonebook.display();
- cout << "Press Enter To Exit...";
- cin.ignore();
- cin.get();
- }
- int DoPrompt()
- {
- int choice;
- system("cls");
- cout << "Welcome To Phone Book ver 1.0";
- cout << endl
- << endl
- << endl;
- cout << "0.Add A New Contact" << endl;
- cout << "1.Edit A Contact" << endl;
- cout << "2.Delete Contact" << endl;
- cout << "3.Search Contact" << endl;
- cout << "4.View All Contact" << endl;
- cout << "5.Exit" << endl
- << endl;
- while (true)//
- {
- try
- {
- cout << "Enter Your Choice:-" << endl;
- cin >> choice;
- if (cin.fail())
- {
- throw runtime_error("Input must be a number.");
- }
- if (choice < 0 || choice > 5)
- {
- throw runtime_error("Number is not valid.");
- }
- break;
- }
- catch (const exception& e)
- {
- cerr << "Error: " << e.what() << endl;
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- }
- }
- return choice;
- }
- string getValue(string prompt)//
- {
- string value;
- while (true)
- {
- try
- {
- cout << prompt << ": ";
- getline(cin, value);
- if (value.empty())
- {
- throw runtime_error("Value cannot be empty.");
- }
- break;
- }
- catch (const exception& e)
- {
- cerr << "Error: " << e.what() << endl;
- }
- }
- return value;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement