I made this widget at MyFlashFetish.com.

Friday 3 February 2012

C++ coding (phonebook system)

so,here i show my simple coding for c++ basic programing for references. I'm sorry if there's got something wrong  with the codes or mayb hard for you guys to understand.  :)


i)

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <conio.h>

using namespace std;

struct phonebook
{
       char first[20];
       char last[20];
       char number[20];
       char email[20];
       
};

   
void Add();
void Edit();
void Show();
void Delete();
void Save();
void Member();
phonebook lastcontacts[50];
int bilcontact;

//======================ADD CONTACT FUNCTION=====================
void Add()
{
       
          char first[20]; //variable
          char last[20];
          char number[20];
          char email[20];

     cout << "\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
     cout << "\t\tPlease Write Down Your Contact.... " << endl;
     cout << "\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" <<endl;
     cout << endl;
     cout << "\t\tFirst Name : " ;
     cin >> first;
     cout << "\t\tLast Name : ";
     cin >> last;
     cout << "\t\tNumber : ";
     cin >> number;
     cout << "\t\tE-mail : ";
     cin >> email;
 
   
     strcpy(lastcontacts[bilcontact].first , first);
     strcpy(lastcontacts[bilcontact].last, last);
     strcpy(lastcontacts[bilcontact].email, email);
     strcpy(lastcontacts[bilcontact].number, number);
     bilcontact = bilcontact + 1;
   
     system ("cls");
   
}

//======================EDIT CONTACT FUNCTION=====================
void Edit()
{        
           char first[20]; //variables
           char last[20];
           char number[20];
           char email[20];
           int i;  
 
     cout << "\t\t=============================================" <<endl;
     cout << "\t\t\tPlease enter the details of contact to edit :" << endl;
     cout << "\t\t=============================================" << endl;
     cout <<endl;
     cout << "\t\tFirst name:" ;
     cin >> first;
     cout << "\t\tLast name:";
     cin >> last;
     cout << endl;
     cout << "\t\t=============================================" << endl;
     for(i=0; i < bilcontact; i++)
     {      
          if (strcmp(first, lastcontacts[i].first) == 0 && strcmp(last,lastcontacts[i].last) == 0)
          {
           
       
             cout <<"\t\t=============================================" << endl;
             cout << "\t\tEdit Your First Name : ";
             cin >> lastcontacts[i].first;
             cout << "\t\tEdit Your Last Name : ";
             cin >> lastcontacts[i].last;
             cout << "\t\tEdit Your Number : " ;
             cin >> lastcontacts[i].number;
             cout << "\t\tEdit Your Email : " ;
             cin >> lastcontacts[i].email;
             cout <<"=============================================" << endl;
             break;                
          }      
           
     }
   
              if (i == bilcontact)
     {
              cout << endl;
              cout << "\t\tRecord Not Found! Please Check Your Spelling." << endl;    
       getch();
     }
     }

//===================SHOW CONTACT FUNCTION========================
void Show()
{
            int i;
         
    for(i=0; i < bilcontact; i++)
    {
   
      cout << "\t\t\t\tFirst Name : " ;    
      cout << lastcontacts[i].first << endl;
      cout << "\t\t\t\tLast Name : " ;
      cout << lastcontacts[i].last << endl;
      cout << "\t\t\t\tEmail : " ;
      cout << lastcontacts[i].email  << endl;
      cout << "\t\t\t\tNumber : " ;
      cout << lastcontacts[i].number << endl;
      cout << endl;
   
      getch();
    }  
   
}

//=================SAVE CONTACT FUNCTION=======================          
void Save()
{  
     ofstream savefile("phonebook.txt");
     int i;
 
    for (i=0; i < bilcontact; i++)
    {
   
        savefile << "First Name : " <<lastcontacts[i].first <<"\n"
        << "Last Name : " << lastcontacts[i].last <<"\n"
        << "Number : " <<lastcontacts[i].number << "\n"
        << "Email : " <<lastcontacts[i].email << "\n"
        << " " << endl;
}

     savefile.close();
}



//===================MAIN MENU OPTION===========================   
int main()
{
 
cout << endl;
cout << "+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~" << endl;
cout << "================================================================================" << endl;
cout << "\t\t\tSelamat Datang Ke PhoneBook Kami :) "<< endl;
cout << "\t\t\t     Assalamualaikum w.b.t " << endl;
cout << "================================================================================" << endl;
cout << "+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~" << endl;
cout << endl;


int choice;
do
{
    cout << endl;
    cout << "\t\t\t\t==========================" << endl;
    cout << "\t\t\t\t|| \t\t\t||" << endl;
    cout << "\t\t\t\t|| 1. Add Contacts. \t||" << endl;
    cout << "\t\t\t\t|| 2. Edit Contacts. \t||" << endl;
    cout << "\t\t\t\t|| 3. Save Contacts. \t||" << endl;
    cout << "\t\t\t\t|| 4. Show All Contact.\t||" <<endl;
    cout << "\t\t\t\t|| 5. Exit. \t\t||" << endl;
    cout << "\t\t\t\t|| \t\t\t||" <<endl;
    cout << "\t\t\t\t==========================\n" << endl;
    cout << "Selection :" << endl;
    cin >> choice;
    system ("cls");
 
    switch (choice)
    {
           case 1:                                                              
             
                   {
                        Add();                                                
                        system ("cls");
                        }
                        break;
                     
           case 2:                                                                .
                {
                     
                        Edit();                                                
                        system ("cls");
                        }
                        break;
                     
           case 3:                                                              
                {
                        cout << "\t\t\t=========================================" << endl;
                        cout << "\t\t\tYour Contacts Has Been Saved. Thanks You!" << endl;
                        cout << "\t\t\t=========================================" << endl;
                        Save();                                                
                        getch();
                        system ("cls");
                        }
                        break;
                         
         
           case 4:                                                            
                {        
                        cout << "\t\t\t\t   Contact Information. " << endl;
                        cout << "\t\t\t\t==========================" << endl;
                        Show();                                                
                        cout << "\t\t\t\t==========================" << endl;
                     
                        getch();
                        system ("cls");
                        }
                        break;
         
                     
           case 5:                                                              
                {
                       }
                       return 0;

               }
}
                                 
while (choice != 5);            

system ("pause");
return 0;
}



========================================================================
========================================================================

ii) using if else loops (credit to Nur Atikah) =)


#include <iostream>
#include<fstream>
#include <conio.h>
#include <string>

using namespace std;

            typedef struct contact
{
            string first_name;
            string last_name;
            char email[10];
            char phone[10];
 } c,search;
           
int main ()
{
     int menu, found,j;
     int saiz=0;
     contact c[100];
     string s1_name,s2_name;
     ofstream outfile("D:\\outfile1.txt");
     contact search;          
           
while (menu!=4)
{
      cout<<"\n\n\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
      cout<<"\n\t\t         WELCOME TO PHONE BOOK SYSTEM";
      cout<<"\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
   
      cout<<"\n\n Enter your selection by pressing button 0 to 4";
      cout<<"\n\n ---0--- \tVIEW CONTACT PERSON";
      cout<<"\n\n ---1--- \tADD CONTACT PERSON";
      cout<<"\n\n ---2--- \tEDIT CONTACT PERSON";
      cout<<"\n\n ---3--- \tSEARCH CONTACT PERSON";
      cout<<"\n\n ---4--- \tEXIT";
      cout<<endl<<endl;
   
      cout<<"Your selection is: ";
      cin>>menu;
      system ("cls");
   
   
         if (menu==1)
      {
         cout<<"\n\n\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
         cout<<"\n\t\t           ADD CONTACT PERSON";
         cout<<"\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";        
       
         cout<<"\n\n Please add the information below:";
         cout<<"\n\n 1. first name: ";
         cin>>c[saiz].first_name;
         cout<<"\n 2. last name: ";
         cin>>c[saiz].last_name;
         cout<<"\n 3. email address: ";
         cin>>c[saiz].email;
         cout<<"\n 4. phone number: ";
         cin>>c[saiz].phone;
         saiz++;
       
       /*  cin>>c[saiz].first_name>>c[saiz].last_name>>c[saiz].email>>c[saiz].phone;
         saiz++;   */
     
         cout<<"\n DONE!";
         cout<<"\n\n(B)ack to main menu";
         menu=getch();
         system ("cls");
       
                   
     }
         else if (menu==2)
     {
         cout<<"\n\n\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
         cout<<"\n\t\t           EDIT CONTACT PERSON";
         cout<<"\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
         
         cout<<"\n\n Please enter the information below to edit: ";
         found =0;  
     
         cout<<"\n\n First name: ";
         cin>>s1_name;
     
         cout<<"\n\n Last name: ";
         cin>>s2_name;
                             
     // cout<<search.first_name<<search.last_name<<endl;
          for (int k=0;k<saiz;k++)
      {
          cout<<"\n\n Searching..."<<k<<"...1...2...3..."<<endl;
          if (s1_name==c[k].first_name && c[k].last_name==s2_name)
      {
          search=c[k]; found=1; j=k; break;
      }          
         
      }
          if (found==1)
      {
          cout<<"\n\n\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
          cout<<"\n\t\t           FOUND!!!";
          cout<<"\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
          cout<<"\n 1. first name: ";
          cout<<search.first_name;
          cout<<"\n 2. last name: ";
          cout<<search.last_name;
          cout<<"\n 3. email address: ";
          cout<<search.email;
          cout<<"\n 4. phone number: ";
          cout<<search.phone;
       
     // cout<<search.first_name<<search.last_name<<search.email<<search.phone<<endl;
      }
   
         cout<<"\n\n\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
         cout<<"\n\t\t           EDITING CONTACT PERSON";
         cout<<"\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
         cout<<"\n 1. new first name:";
         cin>>c[j].first_name;
         cout<<"\n 2. new last name:";
         cin>>c[j].last_name;
         cout<<"\n 3. new email address:";
         cin>>c[j].email;
         cout<<"\n 4. phone number:";
         cin>>c[j].phone;
   
         cout<<"\n DONE!";
         cout<<"\n\n(B)ack to main menu";
         menu=getch();
         system ("cls");
   
      }
         
         else if (menu==3)
      {
         cout<<"\n\n\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
         cout<<"\n\t\t           SEARCH CONTACT PERSON";
         cout<<"\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
         
         cout<<"\n\n Please enter the information below to begin search: ";
         found =0;  
     
         cout<<"\n\n First name: ";
         cin>>s1_name;
     
         cout<<"\n\n Last name: ";
         cin>>s2_name;
                             
     // cout<<search.first_name<<search.last_name<<endl;
          for (int k=0;k<saiz;k++)
      {
          cout<<"\n\n Searching..."<<k<<"..."<<endl;
          if (s1_name==c[k].first_name && c[k].last_name==s2_name)
      {
          search=c[k]; found=1; j=k; break;
      }          
         
      }
          if (found==1)
      {
          cout<<"\n\n\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
          cout<<"\n\t\t           FOUND!!!";
          cout<<"\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
          cout<<"\n 1. first name: ";
          cout<<search.first_name;
          cout<<"\n 2. last name: ";
          cout<<search.last_name;
          cout<<"\n 3. email address: ";
          cout<<search.email;
          cout<<"\n 4. phone number: ";
          cout<<search.phone;
      }
       
         cout<<"\n\n(B)ack to main menu";
         menu=getch();
         system ("cls");          
      }
         else if (menu==4)
      {      
   
         cout<<"\n\n\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
         cout<<"\n\t\t           GOODBYE & HAVE NICE DAY";
         cout<<"\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
       
         cout<<endl<<endl;
      }
         else if (menu==0)
      {
         for (int i=0;i<saiz;i++)
      {
       
     
          cout<<"\n\n\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
          cout<<"\n\t\t           VIEWING..........";
          cout<<"\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
     
          cout<<"\n 1. first name: ";
          cout<<c[i].first_name;
          cout<<"\n 2. last name: ";
          cout<<c[i].last_name;
          cout<<"\n 3. email address: ";
          cout<<c[i].email;
          cout<<"\n 4. phone number: ";
          cout<<c[i].phone;
             
               
          cout<<endl;
         
      }
      {
         cout<<"\n\n(B)ack to main menu";
         menu=getch();
         system ("cls");
      }
      }
         else
     {
        cout<<"\n\n\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
        cout<<"\n\t\t       PLEASE ENTER YOUR SELECTION AGAIN";
        cout<<"\n\t\t+++++++++++++++++++++++++++++++++++++++++++++";
   
        cout<<"\n\n Enter your selection by pressing button 0 to 4";
        cout<<"\n\n ---0--- \tVIEW CONTACT PERSON";
        cout<<"\n\n ---1--- \tADD CONTACT PERSON";
        cout<<"\n\n ---2--- \tEDIT CONTACT PERSON";
        cout<<"\n\n ---3--- \tSEARCH CONTACT PERSON";
        cout<<"\n\n ---4--- \tEXIT";
        cout<<endl<<endl;
   
        cout<<"Your selection is: ";
        cin>>menu;
        system("cls");
   
      }
   
           if (outfile.is_open())
  {
    outfile << "PHONEBOOK" <<endl;
    outfile << "first name: " ;  
    outfile << "This is another line.\n";
    outfile.close();
       
          }  
      }
        system ("pause");
        system ("cls");
        return 0;
      }

No comments:

Post a Comment