Contact Book in C++ using File Handling - GeeksforGeeks

09 Mar, 2021
Straightaway prior brushing up knowledge of the language and file handling is required. So let us do discuss C++ Language concepts in File Handling and loops. Straight we know that for compiling the code, an IDE for compiling C++ language is required such as Code Block, Visual Studio Code, Dev C++, etc is required to run our program.
This software aims to cream a contact book in C++ using File Handling. In C++, files are mainly dealt with by using three classes fstream, ifstream, ofstream available in the fstream header file.
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.
Implementation: 
The contacts will be saved to a file. Contact Book will be providing the following features to the user:
1. Add contact.
2. Search Contact.
3. Help.
4. Exit.
Details of contact that will be saved are as follows:
1. First name.
2. Last name.
3. Phone Number.
Example
C++
// Importing input output operations file#include <iostream>// Importing file class#include <fstream>// Importing standard library file#include <stdlib.h>  using namespace std;  // Variables declared outside any function// hence scope is global, hence global variablesstring fname, lname, phone_num;  // Methods  // Helper Methods followed by// Main driver method  void addContact();void searchContact();void help();void self_exit();bool check_digits(string);bool check_numbers(string);  // Method 1// Helper methodvoid self_exit(){    system("cls");    cout << "\n\n\n\t\tThank You for using Contact-Saver!";    exit(1);}  // Method 2// Helper methodvoid help(){    cout << "Help Center";    cout << endl << endl;    system("pause");    system("cls");}  // Method 3// Helper methodvoid addContact(){    ofstream phone("number.txt", ios::app);    system("cls");    cout << "\n\n\tEnter First Name : ";    cin >> fname;    cout << "\n\tEnter Last Name : ";    cin >> lname;    cout << "\n\tEnter 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 << "\n\tContact saved successfully !";            }            else {                cout << "\n\tError in opening record!";            }        }        else {            cout << "\n\tOnly numbers are allowed!";        }    }    else {        cout << "\n\tPhone number should be of 10 digits "                "only.";    }    cout << endl << endl;    system("pause");    system("cls");    phone.close();}  // Method 4// Helper methodvoid searchContact(){    bool found = false;    ifstream myfile("number.txt");    string keyword;    cout << "\n\tEnter Name to search : ";    cin >> keyword;    while (myfile >> fname >> lname >> phone_num) {        if (keyword == fname || keyword == lname) {            system("cls");            cout << "\n\n\n\t\tCONTACT DETAILS";            cout << "\n\nFirst Name : " << fname;            cout << "\nLast Name : " << lname;            cout << "\nPhone Number : " << phone_num;            found = true;            break;        }    }    if (found == false)        cout << "\nNo such contact is found!";      cout << endl << endl;    system("pause");    system("cls");}  // Method 5// Helper methodbool check_digits(string x){    if (x.length() == 10)        return true;    else        return false;}  // Method 6// Helper methodbool 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;      cout << endl << endl;    system("pause");    system("cls");}  // Method 7// Main driver methodint main(){    int choice;    system("cls");    system("color 0A");    while (1) {        cout << "\n\n\n\t\t\tCONTACT SAVER";        cout << "\n\n\t1. Add Contact\n\t2. Search "                "Contact\n\t3. Help\n\t4. Exit\n\t> ";        cin >> choice;          // Switch case        switch (choice) {        case 1:            addContact();            break;          case 2:            searchContact();            break;          case 3:            help();            break;          case 4:            self_exit();            break;          default:            cout << "\n\n\tInvalid Input!";        }    }    return 0;}
Output
cpp-file-handling
C++
C++ Programs
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
std::string class in C++
Header files in C/C++ and its uses
Program to print ASCII Value of a character
How to return multiple values from a function in C or C++?
C++ Program for QuickSort
Sorting a Map by value in C++ STL