|
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
| 3 | +#include <iomanip> |
| 4 | +#include <string> |
| 5 | +using namespace std; |
| 6 | +class ContactManager { |
| 7 | +private: |
| 8 | + string phoneNo, name, address, description; |
| 9 | + fstream file; |
| 10 | + |
| 11 | +public: |
| 12 | + void menu(); |
| 13 | + void addContact(); |
| 14 | + void showAll(); |
| 15 | + void searchContact(); |
| 16 | +}; |
| 17 | +// Function to display menu |
| 18 | +void ContactManager::menu() { |
| 19 | + char choice; |
| 20 | + do { |
| 21 | + system("cls"); |
| 22 | + cout << " # CONTACT MANAGEMENT APP \n"; |
| 23 | + cout << "________________________________\n"; |
| 24 | + cout << "1 → Add Contact\n"; |
| 25 | + cout << "2 → Show All Contacts\n"; |
| 26 | + cout << "3 → Search Contact\n"; |
| 27 | + cout << "0 → Exit\n"; |
| 28 | + cout << "________________________________\n"; |
| 29 | + cout << "Enter your choice: "; |
| 30 | + cin >> choice; |
| 31 | + cin.ignore(); |
| 32 | + |
| 33 | + switch (choice) { |
| 34 | + case '1': |
| 35 | + addContact(); |
| 36 | + break; |
| 37 | + case '2': |
| 38 | + showAll(); |
| 39 | + break; |
| 40 | + case '3': |
| 41 | + searchContact(); |
| 42 | + break; |
| 43 | + case '0': |
| 44 | + cout << "\nExiting the program... Goodbye!\n"; |
| 45 | + break; |
| 46 | + default: |
| 47 | + cout << "\nInvalid choice! Please try again.\n"; |
| 48 | + } |
| 49 | + |
| 50 | + if (choice != '0') { |
| 51 | + cout << "\nPress Enter to continue..."; |
| 52 | + cin.get(); |
| 53 | + } |
| 54 | + |
| 55 | + } while (choice != '0'); |
| 56 | +} |
| 57 | +// Function to add new contact |
| 58 | +void ContactManager::addContact() { |
| 59 | + cout << "\nEnter Phone Number: "; |
| 60 | + getline(cin, phoneNo); |
| 61 | + cout << "Enter Name: "; |
| 62 | + getline(cin, name); |
| 63 | + cout << "Enter Address: "; |
| 64 | + getline(cin, address); |
| 65 | + cout << "Enter Description: "; |
| 66 | + getline(cin, description); |
| 67 | + |
| 68 | + file.open("contacts.csv", ios::out | ios::app); |
| 69 | + if (!file){ |
| 70 | + cerr<<"Error opening file for writing!\n"; |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + file << phoneNo << "," << name << "," << address << "," << description << "\n"; |
| 75 | + file.close(); |
| 76 | + |
| 77 | + cout << "\nContact added successfully!"; |
| 78 | +} |
| 79 | +// Function to display all contacts |
| 80 | +void ContactManager::showAll() { |
| 81 | + file.open("contacts.csv", ios::in); |
| 82 | + if (!file) { |
| 83 | + cerr << "Error opening file! No data found.\n"; |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + cout << "\n CONTACT LIST \n"; |
| 88 | + cout << left << setw(15) << "Phone No" |
| 89 | + << setw(20) << "Name" |
| 90 | + << setw(25) << "Address" |
| 91 | + << setw(30) << "Description" << endl; |
| 92 | + cout << "________________________________\n"; |
| 93 | + |
| 94 | + bool found = false; |
| 95 | + while(getline(file, phoneNo, ',')){ |
| 96 | + getline(file, name, ','); |
| 97 | + getline(file, address, ','); |
| 98 | + getline(file, description, '\n'); |
| 99 | + |
| 100 | + cout << left << setw(15) << phoneNo |
| 101 | + << setw(20) << name |
| 102 | + << setw(25) << address |
| 103 | + << setw(30) << description << endl; |
| 104 | + |
| 105 | + found = true; |
| 106 | + } |
| 107 | + if(!found) |
| 108 | + cout << "\nNo contacts found.\n"; |
| 109 | + |
| 110 | + file.close(); |
| 111 | +} |
| 112 | +// Function to search a contact by phone number |
| 113 | +void ContactManager::searchContact() { |
| 114 | + cout << "\nEnter Phone Number to Search: "; |
| 115 | + string searchPhone; |
| 116 | + getline(cin, searchPhone); |
| 117 | + |
| 118 | + file.open("contacts.csv", ios::in); |
| 119 | + if (!file) { |
| 120 | + cerr << "Error opening file! No data found.\n"; |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + bool found = false; |
| 125 | + while (getline(file, phoneNo, ',')) { |
| 126 | + getline(file, name, ','); |
| 127 | + getline(file, address, ','); |
| 128 | + getline(file, description, '\n'); |
| 129 | + |
| 130 | + if (phoneNo == searchPhone) { |
| 131 | + cout << "\n Contact Found!\n"; |
| 132 | + cout << "Phone Number: " << phoneNo << endl; |
| 133 | + cout << "Name: " << name << endl; |
| 134 | + cout << "Address: " << address << endl; |
| 135 | + cout << "Description: " << description << endl; |
| 136 | + found = true; |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if (!found) |
| 142 | + cout << "\n No contact found with that phone number.\n"; |
| 143 | + |
| 144 | + file.close(); |
| 145 | +} |
| 146 | + |
| 147 | +int main() { |
| 148 | + ContactManager app; |
| 149 | + app.menu(); |
| 150 | + return 0; |
| 151 | +} |
0 commit comments