sohom15

Phone_book_2

Oct 4th, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.44 KB | None | 0 0
  1. //=========================================================
  2. // File Name    : headr.h
  3. // Project Name : Phone book
  4. // Version      : v0.0.2
  5. // Copyright    : you are free to copy , share and use this code
  6. // Description  : Phone book program, using POD types , and Binary file
  7. // Compiler     : Gcc MingW
  8. // IDE          : Code::blocks
  9. //===========================================================
  10. //i do not comment my code ...it  was HARD to write ..so it is HARD to read...
  11. //current status: data entry , search name  working ; delete contact not working , display part malfunctioning....
  12. /*
  13. main.cpp
  14. #include"headr.h"
  15. int main()
  16. {
  17.  
  18.     gui();
  19.     return 0;
  20. }
  21. */
  22. #include<iostream>
  23. #include<stdio.h>
  24. #include<cstdlib>
  25. #include<process.h>
  26. #include<string.h>
  27. #include<fstream>
  28. using namespace std;
  29. char nul[1]={""};
  30. char na[10]={"NA"};
  31. class Personal_Info
  32. {
  33. public:
  34.     char name[100];
  35.     char mobile[15];
  36.     char email[15];
  37. public:
  38.     Personal_Info()
  39.     {
  40.         strcpy(name,"NA");
  41.         strcpy(mobile,"NA");
  42.         strcpy(email,"NA");
  43.     }
  44.     void take_name();
  45.     void take_mobile();
  46.     void take_email();
  47.     void show_full_info();
  48.     void validate_mobile();
  49.     void validate_name();
  50.     void validate_email();
  51.     int check(char field[]);
  52. };
  53. int Personal_Info::check(char field[])
  54. {
  55.     return strcmp(name,field);
  56. }
  57. void Personal_Info::validate_name()
  58. {
  59.     if(strcmp(name,nul)==0)
  60.        {
  61.             cout<<"\n..NAME NOT GIVEN...\n";
  62.             cin.ignore();
  63.             strcpy(name,na);
  64.             take_name();
  65.        }
  66. }
  67. void Personal_Info::validate_mobile()
  68. {
  69.     if(strcmp(mobile,nul)==0)
  70.     {
  71.         cout<<"\n..MOBILE NO NOT GIVEN>>\n";
  72.         strcpy(mobile,na);
  73.         take_mobile();
  74.     }
  75. }
  76. void Personal_Info::validate_email()
  77. {
  78.     if(strcmp(email,nul)==0)
  79.     {
  80.         cout<<"\n..EMAIL NOT GIVEN..\n";
  81.         strcpy(email,nul);
  82.     }
  83. }
  84. void Personal_Info::show_full_info()
  85. {
  86.     cout<<"\n***** DISPLAYING DETALIS ****\nS";
  87.     cout<<"\nName      :"<<name;
  88.     cout<<"\nMobile    :"<<mobile;
  89.     cout<<"\nEmail     :"<<email;
  90. }
  91. void Personal_Info::take_name()
  92. {
  93.     cout<<"\nEnter the name ->";
  94.     cin.ignore();
  95.     gets(name);
  96.     validate_name();
  97. }
  98. void Personal_Info::take_mobile()
  99. {
  100.     cout<<"\nEnter the mobile number ->";
  101.     cin.ignore();
  102.     gets(mobile);
  103.     validate_mobile();
  104. }
  105. void Personal_Info::take_email()
  106. {
  107.     cout<<"\nEnter the E-mail  ->";
  108.     cin.ignore();
  109.     gets(email);
  110.     validate_email();
  111. }
  112. class Phonebook
  113. {
  114.     private:
  115.          void save_info();
  116.     protected:
  117.         Personal_Info contact;
  118.     public:
  119.         void search_name();
  120.         void delete_data();
  121.         void show_all();
  122.         void get_data();
  123. };
  124. void Phonebook::save_info()
  125. {
  126.     ofstream filout;
  127.     filout.open("data.bin",ios::out|ios::app|ios::ate);
  128.     int ch;
  129.  
  130.     if(!filout)
  131.     {
  132.         cout<<"\n cannot open file \n";
  133.     }
  134.     cout<<"\n do you want to save this info (1. yes / 2. no) ?\n";
  135.     cin>>ch;
  136.     if(ch==1)
  137.     {
  138.         filout.write((char*)&contact,sizeof(contact));
  139.     }
  140.     else if(ch==2)
  141.         cout<<"\n info discarded \n";
  142.     filout.close();
  143. }
  144. void Phonebook::get_data()
  145. {
  146.     contact.take_name();
  147.     contact.take_mobile();
  148.     contact.take_email();
  149.     contact.show_full_info();
  150.     save_info();
  151. }
  152. void Phonebook::delete_data()
  153. {
  154.  
  155. }
  156. void Phonebook::show_all()
  157. {
  158.     ifstream filout;
  159.     filout.open("data.bin");
  160.     filout.seekg(0,ios::beg);
  161.     cout<<"\n******* Displaying details ******\n";
  162.     if(!filout.good())
  163.         cout<<"\n*** FILE IS DOES NOT EXIST ***\n";
  164.     if(filout.good())
  165.     {
  166.         while(!filout.eof())
  167.         {
  168.             filout.read((char*)&contact,sizeof(contact));
  169.             contact.show_full_info();
  170.         }
  171.     }
  172. }
  173. void Phonebook::search_name()
  174. {
  175.     int token;
  176.     char field[100];
  177.     cout<<"Enter the first name of the person you wanna search ?? \n";
  178.     cin.ignore();
  179.     gets(field);
  180.     cout<<"person to be searched is "<<field;
  181.     cin.get();
  182.     token=0;
  183.     ifstream readfil;
  184.     readfil.open("data.bin");
  185.     if(!readfil.good())
  186.         cout<<"\n*** FILE IS DOES NOT EXIST ***\n";
  187.     if(readfil.good())
  188.     {
  189.         while(!readfil.eof())
  190.         {
  191.             readfil.read((char*)&contact,sizeof(contact));
  192.             if((contact.check(field))==1)
  193.             {
  194.                 token=1;
  195.                 contact.show_full_info();
  196.             }
  197.         }
  198.         if(token==0)
  199.         {
  200.             cout<<"\n*** PERSON NOT FOUND ***\n";
  201.  
  202.         }
  203.     }
  204. }
  205. void gui()
  206. {
  207.     int ch;
  208.     Phonebook p;
  209.     while(1)
  210.         {
  211.             cout<<"*********PHONE BOOK**********\n";
  212.             cout<<"| 1) Add new record         |\n";
  213.             cout<<"| 2) Display All Records    |\n";
  214.             cout<<"| 3) Search Person Name     |\n";
  215.             cout<<"| 4) Delete record          |\n";
  216.             cout<<"| 5) Exit                   |\n";
  217.             cout<<"*****************************\n";
  218.             cout<<"Enter your desired operation : ";
  219.             cin>>ch;
  220.             switch(ch)
  221.                 {
  222.                     case 1:
  223.                         p.get_data();
  224.                         break;
  225.                     case 2:
  226.                         p.show_all();
  227.                         break;
  228.                     case 3:
  229.                         p.search_name();
  230.                         break;
  231.                     case 4:break;
  232.                     case 5:exit(1);
  233.                     default:cout<<"\n Enter a valid choice";
  234.                 }
  235.         }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment