Write a menu driven program that will create a data file containing the list of telephoneUse a class object to store each set of data, access the file created and implement the following tasks Determine the telephone number of specified person Determine the name if telephone number is known Update the telephone number, whenever there is a change.

Write a menu driven program that will create a data file containing the list of telephone
numbers in the following form
John 23456
Ahmed 9876

Use a class object to store each set of data, access the file created and implement the following
tasks
Determine the telephone number of specified person
Determine the name if telephone number is known
Update the telephone number, whenever there is a change.


#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
#include<string.h>
class Person
{
    public:
                  char name[10];
                  int PhNo;
       
                 void input_data()
                {
                       cout<<"Enter the Name:";
                       cin>> name;
                       cout<<"Enter the PhNo:";
                       cin>>PhNo;
                }
                void put_data()
               {
                       cout<<setw(10)<<name<<setw(10)<<PhNo<<endl;
               }
};

int main()
{
    Person rec;
    int Phone,pos,choice,offset,i;
    fstream fp;
    ifstream in;
    ofstream out;
    char nm[20];
   
    char ans;
    do
    {
        cout<<"\n1.Read file";
        cout<<"\n2.Write file";
        cout<<"\n3.Determine Name if telephone number is specified";
        cout<<"\n4.Determine telephone if name is specifeied";
        cout<<"\n5.Update telephone number";
        cout<<"\nEnter the choice:";
        cin>>choice;
        switch(choice)
        {
            case 1:
                in.open("test.txt",ios::in|ios::binary);
                cout<<"\nThe contents of file are:\n";
                while(in.read((char*)&rec,sizeof(rec)))
                {
                    rec.put_data();
                }
                in.close();
                break;
               
            case 2:
                rec.input_data();
                char ch;
                cin.get(ch);
                out.open("test.txt",ios::out|ios::app|ios::binary);
                out.write((char*)&rec,sizeof(rec));
                out.close();
                break;
               
            case 3:
                cout<<"\nEnter the phone No:";
                cin>>Phone;
                fp.open("test.txt",ios::ate|ios::in|ios::out|ios::binary);
                fp.seekg(0,ios::beg);
                pos=-1;
                i=0;
                while(fp.read((char*)&rec,sizeof(rec)))
                {
                    if(Phone == rec.PhNo)
                    {
                        pos = i;
                        break;
                    }
                    i++;
                }
                offset=pos*sizeof(rec);
                fp.seekp(offset);
                fp.read((char*)&rec,sizeof(rec));
                cout<<"\nName:"<<rec.name;
                fp.close();
                break;
               
            case 4:
                cout<<"\nEnter the Name:";
                cin>>nm;
                fp.open("test.txt",ios::ate|ios::in|ios::out|ios::binary);
                fp.seekg(0, ios::beg);
                pos = -1;
                i = 0;
                while(fp.read((char*)&rec,sizeof(rec)))
                {
                    if((strcmp(nm,rec.name))==0)
                    {
                        pos = i;
                        break;
                    }
                    i++;               
                }
                offset = pos*sizeof(rec);
                fp.seekp(offset);
                fp.read((char*)&rec, sizeof(rec));
                cout<<"\nTelephone Number:"<<rec.PhNo;
                fp.close();
                break;
               
            case 5:
                cout<<"\nEnter the Name:";
                cin>>nm;
                fp.open("test.txt",ios::ate|ios::in|ios::out|ios::binary);
                fp.seekg(0,ios::beg);
                pos = -1;
                i=0;
                while(fp.read((char*)&rec,sizeof(rec)))
                {
                    if((strcmp(nm,rec.name)) == 0)
                    {
                        pos = i;
                        break;
                    }
                    i++;
                }
                offset = (pos)*sizeof(rec);
                fp.seekp(offset);
                cout<<"\nCureent Phone :"<<rec.PhNo;
                cout<<"\nEnter new telephoine Number:";
                cin>>Phone;
                rec.PhNo=Phone;
                fp.write((char*)&rec,sizeof(rec))<<flush;
                cout<<"\nrecord updated !!\n";
                fp.seekg(0);
                while(fp.read((char*)&rec,sizeof(rec)))
                {
                    rec.put_data();
                }
                fp.close();
                break;
            }
            cout<<"\n Do You want to continue?(y/n)";
            cin>>ans;
        }while(ans=='y');
        return 0;
}

Comments

Post a Comment

Popular posts from this blog

Write a function template selection Sort. Write a program that inputs, sorts and outputs an integer array and a float array.

C++ program that creates an output file, writes information to it, closes the file and open it again as an input file and read the information from the file.

Write C++ program using STL for Sorting and searching