Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views15 pages

Simple Contact List Program Implemented in CPP

Uploaded by

carltanalgo21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views15 pages

Simple Contact List Program Implemented in CPP

Uploaded by

carltanalgo21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Simple Contact List Program

Implemented in C++

By

El Jhon Braña
John Rayven Sugatan

BSIT 1-A
Description

The Contact Management Program is a simple


console application that allows users to manage a
list of contacts. Users can add new contacts,
remove existing ones, search for specific contacts,
and display the entire contact list. Each contact
consists of a name, phone number, and email
address. The program utilizes a menu-driven
interface for user interaction.

Program

#include <iostream>

#include <unordered_map>

#include <string>

using namespace std;

struct Contact {

int number;

string email;

};

class Contacts {

private:
unordered_map<string, Contact> contactList;

public:

void add(string name, int number, string email);

void remove(string n);

void search(string n);

void show();

};

void Contacts::add(string name, int number, string email) {

contactList[name] = { number, email };

cout << "Contact Added." << endl;

void Contacts::remove(string n) {

if (contactList.find(n) != contactList.end()) {

contactList.erase(n);

cout << "Contact " << n << " removed." << endl;

else {

cout << "Contact not found." << endl;

void Contacts::search(string n) {

auto it = contactList.find(n);

if (it != contactList.end()) {

cout << "## Contact Found ##" << "\n\n";


cout << "Contact Name: " << it->first

<< ", Number: " << it->second.number

<< ", Email: " << it->second.email << endl;

else {

cout << "Contact not found." << endl;

void Contacts::show() {

cout << "## Contact List ##" << "\n\n";

for (const auto& pair : contactList) {

cout << "Contact Name: " << pair.first

<< ", Contact Number: " << pair.second.number

<< ", Email: " << pair.second.email << endl;

void sleep() {

cout << "\nPress Enter to continue...";

cin.ignore(numeric_limits<streamsize>::max(), '\n');

cin.get();

int main() {

int selectedOption;

Contacts contacts;
while (true) {

cout << "\nContact List" << endl;

cout << "Options:\n"

<< "1. Add Contacts\n"

<< "2. Remove Contacts\n"

<< "3. Show Contacts\n"

<< "4. Search Contacts\n"

<< "5. Exit\n"

<< "Choose: ";

cin >> selectedOption;

switch (selectedOption) {

case 1: {

string name;

string email;

int number;

cout << "\nAdd Contacts" << endl;

cout << "Contact name: ";

cin.ignore();

getline(cin, name);

cout << "Contact Number: ";

cin >> number;

cout << "Email: ";

cin.ignore();
getline(cin, email);

contacts.add(name, number, email);

sleep();

break;

case 2: {

string selected;

cout << "\nRemove contact by name: " << endl;

cin.ignore();

getline(cin, selected);

contacts.remove(selected);

sleep();

break;

case 3: {

contacts.show();

sleep();

break;

case 4: {

string contact;

cout << "\nSearch contacts: " << endl;

cin.ignore();

getline(cin, contact);
contacts.search(contact);

break;

case 5: {

cout << "Exiting the program." << endl;

return 0;

default: {

cout << "Invalid option. Please try again." << endl;

sleep();

return 0;

return 0;

Functions
1.Add Contacts: Users can input a contact's
name, phone number, and email address to
add it to the contact list.
2.Remove Contacts: Users can remove a
contact by specifying the contact's name.
3.Show Contacts: Users can view all contacts
stored in the contact list.
4.Search Contacts: Users can search for a
specific contact by name and view their details.
5.Exit: Users can exit the program.

Data Structure
The program uses an unordered_map from the
C++ Standard Library to store contacts. The key is
a string representing the contact's name, and the
value is a Contact struct that contains the contact's
phone number and email address.
Reason for Choosing unordered_map:
 Fast Access: unordered_map provides average
constant time complexity (O(1)) for insertions,
deletions, and lookups, making it efficient for
managing contacts.
 Unique Keys: It ensures that each contact
name is unique, preventing duplicate entries.
 Dynamic Size: It can grow dynamically as more
contacts are added, without the need for
manual resizing.
Implementations
1.Adding a Contact

void Contacts::add(string name, int


number, string email) {
contactList[name] = { number,
email };
cout << "Contact Added." <<
endl;
}

2.Removing a Contact
void Contacts::remove(string n) {
if (contactList.find(n) !=
contactList.end()) {
contactList.erase(n);
cout << "Contact " << n <<
" removed." << endl;
} else {
cout << "Contact not
found." << endl;
}
}
3.Contact Search
void Contacts::search(string n) {
auto it =
contactList.find(n);
if (it != contactList.end())
{
cout << "## Contact
Found ##" << "\n\n";
cout << "Contact Name: "
<< it->first
<< ", Number: " <<
it->second.number
<< ", Email: " <<
it->second.email << endl;
} else {
cout << "Contact not
found." << endl;
}
}
4.Show Contacts
void Contacts::show() {
cout << "## Contact List ##" << "\
n\n";
for (const auto& pair : contactList) {
cout << "Contact Name: " <<
pair.first
<< ", Contact Number: " <<
pair.second.number
<< ", Email: " <<
pair.second.email << endl;
}
}

Sample Output

Contact List
Options:
1. Add Contacts
2. Remove Contacts
3. Show Contacts
4. Search Contacts
5. Exit
Choose: 1

Add Contacts
Contact name: El Jhon
Contact Number: 1234567890
Email: [email protected]
Contact Added.

Press Enter to continue...

Contact List
Options:
1. Add Contacts
2. Remove Contacts
3. Show Contacts
4. Search Contacts
5. Exit
Choose: 3

## Contact List ##

Contact Name: El Jhon, Contact Number:


1234567890, Email: [email protected]

Press Enter to continue...

Contact List
Options:
1. Add Contacts
2. Remove Contacts
3. Show Contacts
4. Search Contacts
5. Exit
Choose: 4

Search contacts:
John Doe
## Contact Found ##

Contact Name: El Jhon, Contact Number:


1234567890, Email: [email protected]

Press Enter to continue...

Contact List
Options:
1. Add Contacts
2. Remove Contacts
3. Show Contacts
4. Search Contacts
5. Exit
Choose: 2

Remove contact by name:


El Jhon
Contact El Jhon removed.
Press Enter to continue...

Contact List
Options:
1. Add Contacts
2. Remove Contacts
3. Show Contacts
4. Search Contacts
5. Exit
Choose: 5
Exiting the program.

You might also like