sohom15

Phone_Book

Oct 2nd, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.83 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 part working......the search name part is not working
  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.     //char *ret_name(){return name;};
  49.     void validate_mobile();
  50.     void validate_name();
  51.     void validate_email();
  52. };
  53. void Personal_Info::validate_name()
  54. {
  55.     if(strcmpi(name,nul)==0)
  56.        {
  57.             cout<<"\n..NAME NOT GIVEN...\n";
  58.             cin.ignore();
  59.             strcpy(name,na);
  60.             take_name();
  61.        }
  62. }
  63. void Personal_Info::validate_mobile()
  64. {
  65.     if(strcmpi(mobile,nul)==0)
  66.     {
  67.         cout<<"\n..MOBILE NO NOT GIVEN>>\n";
  68.         strcpy(mobile,na);
  69.         take_mobile();
  70.     }
  71. }
  72. void Personal_Info::validate_email()
  73. {
  74.     if(strcmpi(email,nul)==0)
  75.     {
  76.         cout<<"\n..EMAIL NOT GIVEN..\n";
  77.         strcpy(email,nul);
  78.     }
  79. }
  80. void Personal_Info::show_full_info()
  81. {
  82.     cout<<"\n***** DISPLAYING DETALIS ****\nS";
  83.     cout<<"\nName      :"<<name;
  84.     cout<<"\nMobile    :"<<mobile;
  85.     cout<<"\nEmail     :"<<email;
  86. }
  87. void Personal_Info::take_name()
  88. {
  89.     cout<<"\nEnter the name ->";
  90.     cin.ignore();
  91.     gets(name);
  92.     validate_name();
  93. }
  94. void Personal_Info::take_mobile()
  95. {
  96.     cout<<"\nEnter the mobile number ->";
  97.     cin.ignore();
  98.     gets(mobile);
  99.     validate_mobile();
  100. }
  101. void Personal_Info::take_email()
  102. {
  103.     cout<<"\nEnter the E-mail  ->";
  104.     cin.ignore();
  105.     gets(email);
  106.     validate_email();
  107. }
  108. class Phonebook
  109. {
  110.     private:
  111.          void save_info();
  112.     protected:
  113.         Personal_Info contact;
  114.     public:
  115.         void search_name();
  116.         void delete_data();
  117.         void show_all();
  118.         void get_data();
  119. };
  120. void Phonebook::save_info()
  121. {
  122.     ofstream filout;
  123.     filout.open("data.bin",ios::out|ios::app|ios::ate);
  124.     int ch;
  125.  
  126.     if(!filout)
  127.     {
  128.         cout<<"\n cannot open file \n";
  129.     }
  130.     cout<<"\n do you want to save this info (1. yes / 2. no) ?\n";
  131.     cin>>ch;
  132.     if(ch==1)
  133.     {
  134.         filout.write((char*)&contact,sizeof(contact));
  135.     }
  136.     else if(ch==2)
  137.         cout<<"\n info discarded \n";
  138.     filout.close();
  139. }
  140. void Phonebook::get_data()
  141. {
  142.     contact.take_name();
  143.     contact.take_mobile();
  144.     contact.take_email();
  145.     contact.show_full_info();
  146.     save_info();
  147. }
  148. void Phonebook::delete_data()
  149. {
  150.  
  151. }
  152. void Phonebook::show_all()
  153. {
  154.     ifstream filout;
  155.     char line[100];
  156.     filout.open("data.bin");
  157.     filout.seekg(0,ios::beg);
  158.     cout<<"\n******* Displaying details ******\n";
  159.     if(!filout.good())
  160.         cout<<"\n*** FILE IS DOES NOT EXIST ***\n";
  161.     if(filout.good())
  162.     {
  163.         while(!filout.eof())
  164.         {
  165.             filout.read((char*)&contact,sizeof(contact));
  166.             contact.show_full_info();
  167.         }
  168.     }
  169. }
  170. /*void Phonebook::search_name()
  171. {
  172.     int token,index;
  173.     string field,line;
  174.     char line2[100];
  175.     cout<<"Enter the first name of the person you wanna search ?? \n";
  176.     cin.ignore();
  177.     getline(cin,field);
  178.     cout<<"person to be searched is "<<field;
  179.     cin.get();
  180.     token=0;
  181.     index=0;
  182.     ifstream readfil;
  183.     readfil.open("data.binS");
  184.     if(!readfil)
  185.     {
  186.         cout<<"\n\n***file can not be opened***\n";
  187.     }
  188.     while(readfil.good())
  189.     {
  190.         char line1[100];
  191.  
  192.         readfil.read((char*)&contact,sizeof(contact));
  193. //        line1=&contact.ret_name();
  194. //        if(strcmpi(line1,field))
  195.         {
  196.             token=1;
  197.            /*
  198.             readfil.seekg(0,ios::beg);
  199.             for( int i=0 ; i<index ; ++i)
  200.             {
  201.                 readfil.ignore(256,'\n');
  202.             }
  203.             readfil.getline(line2,100,'|');
  204.             cout<<"\n\n***the person id found***\n\n"<<line2<<endl<<endl;
  205.             cout<<"\n**** PERSON IS FOUND ****\n";
  206.             contact.show_full_info();
  207.             break;
  208.         }
  209.         else
  210.         {
  211.             readfil.ignore(256,'\n');
  212.         }
  213.         index++;
  214.     }
  215.     if(token==0)
  216.         cout<<"\n\n***person not found***\n\n";
  217. }*/
  218. void gui()
  219. {
  220.     int ch;
  221.     Phonebook p;
  222.     while(1)
  223.         {
  224.             cout<<"*********PHONE BOOK**********\n";
  225.             cout<<"| 1) Add new record         |\n";
  226.             cout<<"| 2) Display All Records    |\n";
  227.             cout<<"| 3) Search Person Name     |\n";
  228.             cout<<"| 4) Delete record          |\n";
  229.             cout<<"| 5) Exit                   |\n";
  230.             cout<<"*****************************\n";
  231.             cout<<"Enter your desired operation : ";
  232.             cin>>ch;
  233.             switch(ch)
  234.                 {
  235.                     case 1:
  236.                         p.get_data();
  237.                         break;
  238.                     case 2:
  239.                         p.show_all();
  240.                         break;
  241.                     case 3:
  242.                         //p.search_name();
  243.                         break;
  244.                     case 4:break;
  245.                     case 5:exit(1);
  246.                     default:cout<<"\n Enter a valid choice";
  247.                 }
  248.         }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment