Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<string>
- #include<iomanip>
- #include<fstream>
- #include<stdio.h>
- #include<cstdlib>
- //#include<string.h>
- using namespace std;
- const string na("NOT ASSIGNED");
- class Name
- {
- private:
- void replace_null_name();
- public:
- string first_name;
- string middle_name;
- string last_name;
- public:
- Name()
- {
- first_name.assign("") ;
- middle_name.assign(na);
- last_name.assign(na);
- }
- void take_name();
- };
- void Name::take_name()
- {
- cin.ignore();
- cout<<"\nEnter the First Name (mandatory field)\n";
- getline(cin,first_name);
- cout<<"\nEnter the Middle Name\n";
- getline(cin,middle_name);
- cout<<"\nEnter the Last Name\n";
- getline(cin,last_name);
- replace_null_name();
- }
- void Name::replace_null_name()
- {
- if(first_name.empty())
- {
- first_name.assign("");
- cout<<"pls enter a valid name \n";
- take_name();
- }
- if(middle_name.empty())
- middle_name.assign("");
- if(last_name.empty())
- last_name.assign("");
- }
- class Phone_No
- {
- private:
- void validate_mobile();
- void validate_landline();
- public:
- string landline,mobile;
- public:
- Phone_No()
- {
- landline.assign(na);
- mobile.assign(na);
- }
- void take_mobile_no();
- void take_landline();
- };
- void Phone_No::take_mobile_no()
- {
- cout<<"\n Enter the mobile number (mandatory field) \n";
- getline(cin,mobile);
- validate_mobile();
- }
- void Phone_No::take_landline()
- {
- cout<<"\n Enter the Land line number (with code)\n";
- getline(cin,landline);
- validate_landline();
- }
- void Phone_No::validate_mobile()
- {
- if(!mobile.empty())
- {
- if((mobile.length())!=10)
- {
- cout<<"invalid input";
- mobile.assign(na);
- }
- }
- if(mobile==na)
- {
- cout<<"enter at least one phone number\n";
- take_mobile_no();
- }
- }
- void Phone_No::validate_landline()
- {
- if(!landline.empty())
- {
- if((landline.length())!=11)
- {
- cout<<"invalid input\n";
- landline.assign(na);
- }
- }
- if(landline==na)
- {
- cout<<"enter one phone no";
- take_landline();
- }
- }
- class Personal_Info:public Phone_No,public Name
- {
- private:
- void replace_null_value();
- public:
- string email;
- public:
- Personal_Info()
- {
- email.assign(na);
- }
- Personal_Info(string f_n,string m_n,string l_n,string mo,string la,string e_m)
- {
- first_name.assign(f_n);
- middle_name.assign(m_n);
- last_name.assign(l_n);
- mobile.assign(mo);
- landline.assign(la);
- email.assign(e_m);
- }
- void take_full_info();
- void show_full_info();
- };
- void Personal_Info::replace_null_value()
- {
- if(email.empty())
- email.assign("No Email");
- }
- void Personal_Info::take_full_info()
- {
- cin.ignore();
- cout<<"_____ENTER CONTACT DETAILS_____ \n";
- take_name();
- take_mobile_no();
- take_landline();
- cin.get();
- cout<<"\n Enter the E-mail address \n";
- getline(cin,email);
- replace_null_value();
- }
- void Personal_Info::show_full_info()
- {
- cout<<"\n____DISPLAYING DETAILS_____\n";
- cout<<"Name of Contact :";
- cout<<first_name<<" "<<middle_name<<" "<<last_name<<endl;
- cout<<setw(2)<<"Mobile number :";
- cout<<mobile<<endl;
- cout<<setw(10)<<"Landline number :";
- cout<<landline<<endl;
- cout<<setw(11)<<"E-mail address :";
- cout<<email<<endl;
- }
- class Phonebook
- {
- private:
- void save_info();
- protected:
- Personal_Info contact;
- public:
- void search_name();
- void search_no();
- void edit();
- void show_all();
- void get_data();
- };
- void Phonebook::save_info()
- {
- ofstream filout;
- filout.open("data.txt",ios::out|ios::app|ios::binary|ios::ate);
- int ch;
- if(!filout)
- {
- cout<<"\n cannot open file \n";
- }
- cout<<"\n do you want to save this info (1. yes / 2. no) ?\n";
- cin>>ch;
- if(ch==1)
- {
- filout<<contact.first_name<<"~"<<contact.middle_name<<contact.last_name;
- filout<<"~"<<contact.mobile<<"~"<<contact.landline<<"~";
- filout<<contact.email<<"~|"<<endl;
- }
- else if(ch==2)
- cout<<"\n info discarded \n";
- filout.close();
- }
- void Phonebook::get_data()
- {
- contact.take_full_info();
- contact.show_full_info();
- save_info();
- }
- void Phonebook::edit()
- {
- }
- void Phonebook::show_all()
- {
- ifstream filout;
- filout.open("data.txt");
- filout.seekg(0,ios::beg);
- while(filout.good())
- {
- cout<<"\n Displaying details ......\n";
- }
- }
- void Phonebook::search_name()
- {
- int token;
- string field,line;
- char line1[50],line2[50];
- cout<<"Enter the first name of the person you wanna search ?? \n";
- cin.ignore();
- getline(cin,field);
- cout<<"person to be searched is "<<field;
- cin.get();
- token=0;
- ifstream readfil;
- readfil.open("data.txt");
- if(!readfil)
- {
- cout<<"file does not exist";
- }
- while(!readfil.eof())
- {
- readfil.getline(line1,12,'~');
- if(line1==field)
- {
- token=1;
- readfil.seekg(0,ios::beg);
- readfil.getline(line2,50,'|');
- cout<<"the person to be found is \n"<<line2;
- break;
- }
- }
- if(token==0)
- cout<<"\n\n.....data not found...";
- }
- void search_no()
- {
- //comment.
- }
- void gui()
- {
- int ch;
- Phonebook p;
- while(1)
- {
- cout<<"********PHONE BOOK***********\n";
- cout<<"1) Add new record\n";
- cout<<"2) Display All Records\n";
- cout<<"3) Search Telephone No.\n";
- cout<<"4) Search Person Name\n";
- cout<<"5) Update Telephone No.\n";
- cout<<"6) Exit\n";
- cout<<"Choose your choice : ";
- cin>>ch;
- switch(ch)
- {
- case 1:
- p.get_data();
- break;
- case 2:
- p.show_all();
- break;
- case 3:
- search_no();
- break;
- case 4:
- p.search_name();
- break;
- case 5:break;
- case 6:exit(1);
- default:cout<<"\n Enter a valid choice";
- }
- }
- }
- int main()
- {
- gui();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment