#include <iostream>
#include <string>
#include <vector>
struct Contact {
std::string name;
std::string phone;
std::string email;
};
void addContact(std::vector<Contact>& contacts) {
Contact newContact;
std::cout << "Enter name: ";
std::getline(std::cin, newContact.name);
std::cout << "Enter phone: ";
std::getline(std::cin, newContact.phone);
std::cout << "Enter email: ";
std::getline(std::cin, newContact.email);
contacts.push_back(newContact);
std::cout << "Contact added successfully!\n";
}
void searchContact(const std::vector<Contact>& contacts) {
std::string query;
std::cout << "Enter name to search: ";
std::getline(std::cin, query);
bool found = false;
for (const auto& contact : contacts) {
if (contact.name.find(query) != std::string::npos) {
std::cout << "\nName: " << contact.name
<< "\nPhone: " << contact.phone
<< "\nEmail: " << contact.email << "\n";
found = true;
}
}
if (!found) {
std::cout << "No contact found with that name.\n";
}
}
void displayContacts(const std::vector<Contact>& contacts) {
std::cout << "\n--- Contact List ---\n";
for (const auto& contact : contacts) {
std::cout << "Name: " << contact.name
<< ", Phone: " << contact.phone
<< ", Email: " << contact.email << "\n";
}
}
int main() {
std::vector<Contact> contacts;
std::string choice;
while (true) {
std::cout << "\n1. Add Contact\n2. Search Contact\n3. Display All\n4. Exit\nChoose an option: ";
std::getline(std::cin, choice);
if (choice == "1") {
addContact(contacts);
} else if (choice == "2") {
searchContact(contacts);
} else if (choice == "3") {
displayContacts(contacts);
} else if (choice == "4") {
std::cout << "Exit Contact Manager.\n";
break;
} else {
std::cout << "Invalid option. Try again.\n";
}
}
}
/*
run:
1. Add Contact
2. Search Contact
3. Display All
4. Exit
Choose an option: 1
Enter name: aaa
Enter phone: 123
Enter email: aaa@email.com
Contact added successfully!
1. Add Contact
2. Search Contact
3. Display All
4. Exit
Choose an option: 1
Enter name: bbb
Enter phone: 456
Enter email: bbb@email.com
Contact added successfully!
1. Add Contact
2. Search Contact
3. Display All
4. Exit
Choose an option: 1
Enter name: ccc
Enter phone: 789
Enter email: ccc@email.com
Contact added successfully!
1. Add Contact
2. Search Contact
3. Display All
4. Exit
Choose an option: 2
Enter name to search: bbb
Name: bbb
Phone: 456
Email: bbb@email.com
1. Add Contact
2. Search Contact
3. Display All
4. Exit
Choose an option: 3
--- Contact List ---
Name: aaa, Phone: 123, Email: aaa@email.com
Name: bbb, Phone: 456, Email: bbb@email.com
Name: ccc, Phone: 789, Email: ccc@email.com
1. Add Contact
2. Search Contact
3. Display All
4. Exit
Choose an option: 4
Exit Contact Manager.
*/