Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=========================================================
- // File Name : headr.h
- // Project Name : Phone book
- // Version : v0.0.2
- // Copyright : you are free to copy , share and use this code
- // Description : Phone book program, using POD types , and Binary file
- // Compiler : Gcc MingW
- // IDE : Code::blocks
- //===========================================================
- //current status: data entry , search name working ; delete contact not working , display part malfunctioning....
- /*
- main.cpp
- #include"headr.h"
- int main()
- {
- gui();
- return 0;
- }
- */
- #include<iostream>
- #include<stdio.h>
- #include<cstdlib>
- #include<process.h>
- #include<string.h>
- #include<fstream>
- using namespace std;
- char nul[1]={""};
- char na[10]={"NA"};
- class Personal_Info
- {
- public:
- char name[100];
- char mobile[15];
- char email[15];
- public:
- Personal_Info()
- {
- strcpy(name,"NA");
- strcpy(mobile,"NA");
- strcpy(email,"NA");
- }
- void take_name();
- void take_mobile();
- void take_email();
- void show_full_info();
- void validate_mobile();
- void validate_name();
- void validate_email();
- int check(char field[]);
- };
- int Personal_Info::check(char field[])
- {
- return strcmp(name,field);
- }
- void Personal_Info::validate_name()
- {
- if(strcmp(name,nul)==0)
- {
- cout<<"\n..NAME NOT GIVEN...\n";
- cin.ignore();
- strcpy(name,na);
- take_name();
- }
- }
- void Personal_Info::validate_mobile()
- {
- if(strcmp(mobile,nul)==0)
- {
- cout<<"\n..MOBILE NO NOT GIVEN>>\n";
- strcpy(mobile,na);
- take_mobile();
- }
- }
- void Personal_Info::validate_email()
- {
- if(strcmp(email,nul)==0)
- {
- cout<<"\n..EMAIL NOT GIVEN..\n";
- strcpy(email,nul);
- }
- }
- void Personal_Info::show_full_info()
- {
- cout<<"\n***** DISPLAYING DETALIS ****\nS";
- cout<<"\nName :"<<name;
- cout<<"\nMobile :"<<mobile;
- cout<<"\nEmail :"<<email;
- }
- void Personal_Info::take_name()
- {
- cout<<"\nEnter the name ->";
- cin.ignore();
- gets(name);
- validate_name();
- }
- void Personal_Info::take_mobile()
- {
- cout<<"\nEnter the mobile number ->";
- cin.ignore();
- gets(mobile);
- validate_mobile();
- }
- void Personal_Info::take_email()
- {
- cout<<"\nEnter the E-mail ->";
- cin.ignore();
- gets(email);
- validate_email();
- }
- class Phonebook
- {
- private:
- void save_info();
- protected:
- Personal_Info contact;
- public:
- void search_name();
- void delete_data();
- void show_all();
- void get_data();
- };
- void Phonebook::save_info()
- {
- ofstream filout;
- filout.open("data.bin",ios::out|ios::app|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.write((char*)&contact,sizeof(contact));
- }
- else if(ch==2)
- cout<<"\n info discarded \n";
- filout.close();
- }
- void Phonebook::get_data()
- {
- contact.take_name();
- contact.take_mobile();
- contact.take_email();
- contact.show_full_info();
- save_info();
- }
- void Phonebook::delete_data()
- {
- }
- void Phonebook::show_all()
- {
- ifstream filout;
- filout.open("data.bin");
- filout.seekg(0,ios::beg);
- cout<<"\n******* Displaying details ******\n";
- if(!filout.good())
- cout<<"\n*** FILE IS DOES NOT EXIST ***\n";
- if(filout.good())
- {
- while(!filout.eof())
- {
- filout.read((char*)&contact,sizeof(contact));
- contact.show_full_info();
- }
- }
- }
- void Phonebook::search_name()
- {
- int token;
- char field[100];
- cout<<"Enter the first name of the person you wanna search ?? \n";
- cin.ignore();
- gets(field);
- cout<<"person to be searched is "<<field;
- cin.get();
- token=0;
- ifstream readfil;
- readfil.open("data.bin");
- if(!readfil.good())
- cout<<"\n*** FILE IS DOES NOT EXIST ***\n";
- if(readfil.good())
- {
- while(!readfil.eof())
- {
- readfil.read((char*)&contact,sizeof(contact));
- if((contact.check(field))==1)
- {
- token=1;
- contact.show_full_info();
- }
- }
- if(token==0)
- {
- cout<<"\n*** PERSON NOT FOUND ***\n";
- }
- }
- }
- 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 Person Name |\n";
- cout<<"| 4) Delete record |\n";
- cout<<"| 5) Exit |\n";
- cout<<"*****************************\n";
- cout<<"Enter your desired operation : ";
- cin>>ch;
- switch(ch)
- {
- case 1:
- p.get_data();
- break;
- case 2:
- p.show_all();
- break;
- case 3:
- p.search_name();
- break;
- case 4:break;
- case 5:exit(1);
- default:cout<<"\n Enter a valid choice";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement