Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to create a basic contact manager that allows users to add, search, and display contacts in C++

1 Answer

0 votes
#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.

*/

 



answered Aug 18, 2025 by avibootz
edited Aug 22, 2025 by avibootz

Related questions

1 answer 149 views
1 answer 200 views
200 views asked Oct 21, 2015 by avibootz
1 answer 241 views
1 answer 74 views
1 answer 146 views
1 answer 174 views
2 answers 183 views
183 views asked Jun 23, 2016 by avibootz
...