Advertisement
lalani001

Untitled

Mar 18th, 2023
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <iostream>
  2. #include "phonebook.h"
  3. typedef void (*fPointerType)(phonebook& phonebook);
  4. string name = "";
  5. const int NUM_FUNCTIONS = 5;
  6. int DoPrompt();
  7. void AddEntry(phonebook& phonebook);
  8. void EditContact(phonebook& phonebook);
  9. void DeleteItem(phonebook& phonebook);
  10. void SearchPhonebook(phonebook& phonebook);
  11. void Display(phonebook& phonebook);
  12. void exitContact();
  13. string getValue(string prompt);
  14.  
  15. int main()
  16. {
  17.     phonebook phBook;
  18.  
  19.     fPointerType fPointer[NUM_FUNCTIONS] = {
  20.         AddEntry,
  21.         EditContact,
  22.         DeleteItem,
  23.         SearchPhonebook,
  24.         Display };
  25.  
  26.     while (true)
  27.     {
  28.         int choice = DoPrompt();
  29.         if (choice >= 0 && choice < 6)
  30.         {
  31.             (*fPointer[choice])(phBook);
  32.         }
  33.         else
  34.         {
  35.             cout << "Number is not valid";
  36.         }
  37.     }
  38. }
  39. void AddEntry(phonebook& phonebook)
  40. {
  41.  
  42.     int n = 0;
  43.     system("cls"); // clear the console screen1
  44.     cout << "Add Numbers" << endl
  45.         << endl
  46.         << endl;
  47.     cout << "Enter Your Entries:-";
  48.     cin >> n;
  49.     cin.ignore(); // ignore the newline character left int the buffer
  50.     if (n < 100)
  51.     {
  52.         for (int i = 0; i < n; i++)
  53.         {
  54.             string name = getValue("Enter Your Name");
  55.             string number = getValue("Enter Your Number");
  56.  
  57.             phonebook.addEntry(name, number);
  58.  
  59.             // convert the number string to an  long long
  60.         }
  61.     }
  62.     else
  63.     {
  64.         cout << "Out Of Memory...." << endl
  65.             << "Your Can Enter 99 Entries";
  66.         cin.get();
  67.     }
  68. }
  69. void EditContact(phonebook& phonebook)
  70. {
  71.     cout << "Not Implemented Yet";
  72. }
  73. void DeleteItem(phonebook& phonebook)
  74. {
  75.     system("cls");
  76.  
  77.     cin.ignore();
  78.     name = "";
  79.     cout << "Enter Name You Want To Delete:-";
  80.     string name = getValue("Enter Name You Want to Delete");
  81.  
  82.     cout << "Enter to exit...";
  83.     phonebook.removeEntry(name);
  84.  
  85.     cin.get();
  86. }
  87. void SearchPhonebook(phonebook& phonebook)
  88. {
  89.     system("cls");
  90.     cout << "Find" << endl;
  91.     string name = getValue("Enter Search Name");
  92.     phonebook.searchEntry(name);
  93.     cout << "Press Enter To Exit...";
  94.     cin.get();
  95. }
  96. void exitContact()
  97. {
  98.     cout << "Bye.....";
  99.     exit(0);
  100. }
  101. void Display(phonebook& phonebook)
  102. {
  103.     system("cls");
  104.     phonebook.display();
  105.     cout << "Press Enter To Exit...";
  106.     cin.ignore();
  107.     cin.get();
  108. }
  109. int DoPrompt()
  110. {
  111.     int choice;
  112.     system("cls");
  113.     cout << "Welcome To Phone Book ver 1.0";
  114.     cout << endl
  115.         << endl
  116.         << endl;
  117.     cout << "0.Add A New Contact" << endl;
  118.     cout << "1.Edit A Contact" << endl;
  119.     cout << "2.Delete Contact" << endl;
  120.     cout << "3.Search Contact" << endl;
  121.     cout << "4.View All Contact" << endl;
  122.     cout << "5.Exit" << endl
  123.         << endl;
  124.  
  125.     while (true)//
  126.     {
  127.         try
  128.         {
  129.             cout << "Enter Your Choice:-" << endl;
  130.             cin >> choice;
  131.             if (cin.fail())
  132.             {
  133.                 throw runtime_error("Input must be a number.");
  134.             }
  135.             if (choice < 0 || choice > 5)
  136.             {
  137.                 throw runtime_error("Number is not valid.");
  138.             }
  139.             break;
  140.         }
  141.         catch (const exception& e)
  142.         {
  143.             cerr << "Error: " << e.what() << endl;
  144.             cin.clear();
  145.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  146.         }
  147.     }
  148.  
  149.     return choice;
  150. }
  151.  
  152. string getValue(string prompt)//
  153. {
  154.     string value;
  155.     while (true)
  156.     {
  157.         try
  158.         {
  159.             cout << prompt << ": ";
  160.             getline(cin, value);
  161.             if (value.empty())
  162.             {
  163.                 throw runtime_error("Value cannot be empty.");
  164.             }
  165.             break;
  166.         }
  167.         catch (const exception& e)
  168.         {
  169.             cerr << "Error: " << e.what() << endl;
  170.         }
  171.     }
  172.     return value;
  173. }
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement