Phone-book Project in CPP
This is simple project in CPP that will store name and number of people. We can search contact, add contact and remove contact. All contacts are stored in file with the help of file handling.
Features:
- File handling
- Simple
- Search Contact
- Delete Contact
- Create Contact
- Update Contact
- Sort Contacts by Name
Output :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#include <stdio.h> | |
#include <cstring> | |
#include <fstream> | |
#include <iomanip> | |
#include <iostream> | |
using namespace std; | |
void createContact(); | |
void displayAllContacts(); | |
void searchContact(); | |
void deleteContact(); | |
void sortAllContact(); | |
void actualSorting(); | |
void updateContact(); | |
void delete_line(const char*, int); | |
int main() { | |
int exit = 1; | |
while (exit) { | |
cout << "1.View All Contacts\n2.Search Number\n3.Create " | |
"Contact\n4.Remove Contact\n5.Sort Contacts\n6.Update " | |
"Contact\n0.Exit\n"; | |
cin >> exit; | |
switch (exit) { | |
case 1: | |
displayAllContacts(); | |
break; | |
case 2: | |
searchContact(); | |
break; | |
case 3: | |
createContact(); | |
break; | |
case 4: | |
deleteContact(); | |
break; | |
case 5: | |
sortAllContact(); | |
break; | |
case 6: | |
updateContact(); | |
break; | |
case 0: | |
cout << "Thanks For Using This System!\n"; | |
break; | |
default: | |
cout << "Enter Valid Input"; | |
break; | |
} | |
} | |
return 0; | |
} | |
void sortAllContact() { | |
// First create temporary contacts file | |
ofstream fout; | |
string line; | |
remove("temp.txt"); | |
fout.open("temp.txt", ios::app); | |
fstream fin("sample.txt"); | |
while (getline(fin, line)) { | |
fout << line << endl; | |
} | |
fout.close(); | |
actualSorting(); | |
} | |
void actualSorting() { | |
ofstream fout; | |
string myText; | |
char v = 'a'; | |
// cout << int(v); | |
// cout << endl << char(int(v)); | |
int letter = 97; | |
remove("sample.txt"); | |
fout.open("sample.txt", ios::app); | |
SORTING: | |
ifstream fin("temp.txt"); | |
while (getline(fin, myText)) { | |
char myChar = myText[0]; | |
if (isupper(myChar)) { | |
myChar = tolower(myChar); | |
} | |
if (int(myChar) == letter) { | |
fout << myText << endl; | |
} | |
} | |
fin.seekg(0); | |
fin.close(); | |
if (letter != 124) { | |
letter++; | |
goto SORTING; | |
} | |
cout << "\t\tContact Sorted Successfully!!\n"; | |
fout.close(); | |
} | |
void updateContact() { | |
int lineNumber = 0; | |
string myText, search, smallString; | |
ifstream MyReadFile("sample.txt"); | |
cout << "Search Contact To Update - "; | |
cin >> search; | |
while (getline(MyReadFile, myText)) { | |
// cout << myText << endl; | |
smallString = myText; | |
transform(smallString.begin(), smallString.end(), smallString.begin(), | |
::toupper); | |
transform(search.begin(), search.end(), search.begin(), ::toupper); | |
if (smallString.find(search) != string::npos) { | |
cout << "ID - " << lineNumber << " " << myText << endl; | |
} | |
lineNumber++; | |
} | |
MyReadFile.close(); | |
int id, choice; | |
cout << "Enter Contact ID from Above Data To Update Contact - "; | |
cin >> id; | |
ifstream fin("sample.txt"); | |
ofstream fout; | |
remove("temp.txt"); | |
fout.open("temp.txt", ios::app); | |
string line; | |
int lineCount = 0; | |
while (getline(fin, line)) { | |
if (lineCount == id) { | |
cout << line; | |
string fname, mname, lname, number; | |
stringstream sss(line); | |
string word; | |
int index = 0; | |
while (sss >> word) { | |
switch (index) { | |
case 0: | |
fname = word; | |
break; | |
case 1: | |
mname = word; | |
break; | |
case 2: | |
lname = word; | |
break; | |
case 3: | |
number = word; | |
break; | |
default: | |
break; | |
} | |
index++; | |
} | |
cout << fname << "\t" << mname << "\t" << lname << "\t" << number | |
<< endl; | |
// we got orgiginal line here | |
// now ask user which field to update | |
cout << endl; | |
int myUpdate; | |
cout << "\t\tUpdate Contact Details\n1.First Name\n2.Middle " | |
"Name\n3.Last " | |
"Name\n4.Mobile Number\n5.Save And Exit\n6.Exit\n"; | |
cin >> myUpdate; | |
switch (myUpdate) { | |
case 1: | |
cout << "Enter First Name: "; | |
cin >> fname; | |
break; | |
case 2: | |
cout << "Enter Middle Name: "; | |
cin >> mname; | |
break; | |
case 3: | |
cout << "Enter Last Name: "; | |
cin >> lname; | |
break; | |
case 4: | |
cout << "Enter Mobile Number: "; | |
cin >> number; | |
break; | |
case 5: | |
break; | |
case 6: | |
break; | |
default: | |
break; | |
} | |
fout << fname << " " << mname << " " << lname << " " << number | |
<< "\n"; | |
} else { | |
fout << line << endl; | |
} | |
lineCount++; | |
} | |
fout.close(); | |
remove("sample.txt"); | |
rename("temp.txt", "sample.txt"); | |
} | |
void createContact() { | |
char firstName[30], middleName[30], lastName[30]; | |
char number[30]; | |
fname: | |
cout << "Enter First Name - "; | |
cin >> firstName; | |
mname: | |
cout << "Enter Middle Name - "; | |
cin >> middleName; | |
lname: | |
cout << "Enter Last Name - "; | |
cin >> lastName; | |
cout << "Enter Mobile Number - "; | |
mo: | |
cin >> number; | |
if (strlen(number) != 10) { | |
cout << "Enter Valid 10 Digit Mobile Number - "; | |
goto mo; | |
} | |
ofstream fout; | |
string line; | |
fout.open("sample.txt", ios::app); | |
fout << firstName << " " << middleName << " " << lastName << " " << number | |
<< "\n"; | |
cout << "\t\tContact Created Successfully!!\n"; | |
fout.close(); | |
} | |
void displayAllContacts() { | |
string myText; | |
ifstream MyReadFile("sample.txt"); | |
cout << "+" << setfill('-') << setw(80) << "+" << endl; | |
cout << "|" << setfill(' ') << setw(46) << "All Contacts" << setw(34) << "|" | |
<< endl; | |
cout << "+" << setfill('-') << setw(20) << "+" << setw(20) << "+" | |
<< setw(20) << "+" << setw(20) << "+" << endl; | |
cout << "|" << setfill(' ') << setw(10) << "FName" << setw(10) << "|" | |
<< setw(10) << "MName" << setw(10) << "|" << setw(10) << "LName" | |
<< setw(10) << "|" << setw(10) << "Number" << setw(10) << "|" << endl; | |
cout << "+" << setfill('-') << setw(20) << "+" << setw(20) << "+" | |
<< setw(20) << "+" << setw(20) << "+" << endl; | |
while (getline(MyReadFile, myText)) { | |
string fname, mname, lname, number; | |
stringstream ss(myText); | |
string word; | |
int index = 0; | |
while (ss >> word) { | |
switch (index) { | |
case 0: | |
fname = word; | |
break; | |
case 1: | |
mname = word; | |
break; | |
case 2: | |
lname = word; | |
break; | |
case 3: | |
number = word; | |
break; | |
default: | |
break; | |
} | |
index++; | |
} | |
int w1 = ((20 - fname.size()) / 2) + fname.size(); | |
int w2 = 20 - w1; | |
int w3 = ((20 - mname.size()) / 2) + mname.size(); | |
int w4 = 20 - w3; | |
int w5 = ((20 - lname.size()) / 2) + lname.size(); | |
int w6 = 20 - w5; | |
int w7 = ((20 - number.size()) / 2) + number.size(); | |
int w8 = 20 - w7; | |
cout << "|" << setfill(' ') << setw(w1) << fname << setw(w2) << "|" | |
<< setw(w3) << mname << setw(w4) << "|" << setw(w5) << lname | |
<< setw(w6) << "|" << setw(w7) << number << setw(w8) << "|" | |
<< endl; | |
} | |
cout << "+" << setfill('-') << setw(20) << "+" << setw(20) << "+" | |
<< setw(20) << "+" << setw(20) << "+" << endl; | |
MyReadFile.close(); | |
} | |
void searchContact() { | |
string myText, search, smallString; | |
ifstream MyReadFile("sample.txt"); | |
cout << "Enter Number or Name to search - "; | |
cin >> search; | |
cout << "\t\tFound Record(s) :\n"; | |
while (getline(MyReadFile, myText)) { | |
// cout << myText << endl; | |
smallString = myText; | |
transform(smallString.begin(), smallString.end(), smallString.begin(), | |
::toupper); | |
transform(search.begin(), search.end(), search.begin(), ::toupper); | |
if (smallString.find(search) != string::npos) { | |
int w1 = ((60 - myText.size()) / 2) + myText.size(); | |
int w2 = 60 - w1; | |
cout << "+" << setfill('-') << setw(60) << "+" << endl; | |
cout << "|" << setfill(' ') << setw(w1) << myText << setw(w2) << "|" | |
<< endl; | |
cout << "+" << setfill('-') << setw(60) << "+" << endl; | |
} | |
} | |
MyReadFile.close(); | |
} | |
void deleteContact() { | |
int lineNumber = 0; | |
string myText, search, smallString; | |
ifstream MyReadFile("sample.txt"); | |
cout << "Search Contact To Delete - "; | |
cin >> search; | |
while (getline(MyReadFile, myText)) { | |
// cout << myText << endl; | |
smallString = myText; | |
transform(smallString.begin(), smallString.end(), smallString.begin(), | |
::toupper); | |
transform(search.begin(), search.end(), search.begin(), ::toupper); | |
if (smallString.find(search) != string::npos) { | |
cout << "ID - " << lineNumber << " " << myText << endl; | |
} | |
lineNumber++; | |
} | |
MyReadFile.close(); | |
int id, choice; | |
cout << "Enter Contact ID from Above Data To Delete Contact - "; | |
cin >> id; | |
cout << "Are You Sure? This can't be undone ! Enter 0 to Cancel - "; | |
cin >> choice; | |
if (!choice == 0) { | |
delete_line("sample.txt", id); | |
cout << "+" << setfill('-') << setw(60) << "+" << endl; | |
cout << "|" << setfill(' ') << setw(46) | |
<< "Contact Deleted Successfully!" << setw(14) << "|" << endl; | |
cout << "+" << setfill('-') << setw(60) << "+" << endl; | |
} | |
} | |
void delete_line(const char* file_name, int n) { | |
// this snippet is copied from geeksForgeeks.com | |
ifstream is(file_name); | |
ofstream ofs; | |
ofs.open("temp.txt", ofstream::out); | |
char c; | |
int line_no = 0; | |
while (is.get(c)) { | |
if (c == '\n') line_no++; | |
if (line_no != n) ofs << c; | |
} | |
ofs.close(); | |
is.close(); | |
remove(file_name); | |
rename("temp.txt", file_name); | |
} |