C++ Program to Build a Phonebook Contact Management System

Using this article, You are going to know about “C++ Program to Build a Phonebook Contact Management System“. Yes, Shall we start the article to learn about it? Come on! Let’s go.

C++ Program to Build a Phonebook Contact Management System

C++ Program to Build a Phonebook Contact Management System

Source Code

// including header files
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;

//global variables
string fname,lname,phone_num;

// Functions
void addContact();
void searchContact();
void self_exit();
bool check_digits(string);
bool check_numbers(string);

int main()
{
    short int choice;
    system("cls");
    system("color 0A");
    cout << "nnntttContact Management.";
    cout << "nnt1. Add Contactnt2. Search Contactnt3. Exitnt> ";
    cin >> choice;

    switch(choice)
    {
        case 1:
            addContact();
            break;
        case 2:
            searchContact();
            break;
        case 3:
            self_exit();
            break;
        default:
            cout << "nntInvalid Input !";
            cout << "ntPress Any Key To Continue..";
            getch();
            main();
    }
    return 0;
}

void self_exit()
{
    system("cls");
    cout << "nnnttThank You For Using !";
    exit(1);
}


void addContact()
{
    ofstream phone("number.txt", ios::app);
    system("cls");
    cout << "nntEnter First Name : ";
    cin >> fname;
    cout << "ntEnter Last Name : ";
    cin >> lname;
    cout << "ntEnter 10-Digit Phone Number : ";
    cin >> phone_num;

    if(check_digits(phone_num) == true)
    {
        if(check_numbers(phone_num) == true)
        {
            if(phone.is_open())
            {
                phone << fname << " " << lname << " " << phone_num << endl;
                cout << "ntContact Saved Successfully !";
            }
            else
            {
                cout << "ntError Opening File !";
            }
        }
        else
        {
            cout << "ntA Phone Number Must Contain Numbers Only !";
        }
    }
    else
    {
        cout << "ntA Phone Number Must COntain 10 Digits.";
    }
    phone.close();
}

void searchContact()
{
    bool found = false;
    ifstream myfile("number.txt");
    string keyword;
    cout << "ntEnter Name To Search : ";
    cin >> keyword;
    while(myfile >> fname >> lname >> phone_num)
    {
        if(keyword == fname || keyword == lname)
        {
            system("cls");
            cout << "nnnttContact details..";
            cout << "nntFirst Name : " << fname;
            cout << "ntLast Name : " << lname;
            cout << "ntPhone Number : " << phone_num;
            found = true;
            break;
        }
    }
    if(found == false)
    cout << "ntNo Such Contact Found";
}

bool check_digits(string x)
{
    if(x.length() == 10)
    return true;
    else
    return false;
}

bool check_numbers(string x)
{
    bool check = true;

    for(int i=0; i < x.length(); i++)
    {
        if(!(int(x[i]) >= 48 && int(x[i]) <= 57))
        {
            check = false;
            break;
        }
    }

    if(check == true)
    return true;
    if(check == false)
    return false;
}

Final Words

I hope this article helps you to learn “C++ Program to Build a Phonebook Contact Management System“. If you face any issues please let me know via the comment section. Share this article with other C/C++ program developers via social networks. Thank You!

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment