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.


#include<fstream>
#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
int main()
{
int n,cnt=0;
char name[10],aname[10];
char id[10],findid[10];
int ch;
char ans;
ofstream fout;
ifstream fin;
        // Writing data into file.......


   
        fout.open("data1.txt");
        if(!fout)
        {
            cout<<"Error in opening file ";
            return 0;
        }
        cout<<"File created successfully...\n";
       
        cout<<"How many no of records you want to insert: ";
        cin>>n;

        for(int i=0;i<n;i++)
        {
            cout<<"\nEnter the name: ";
            cin>>name;
   
            cout<<"\nEnter the id: ";
            cin>>id;
       
            cout<<"\nEnetr the author of book:";
            cin>>aname;
   
            fout <<"\n"<<setw(20)<<id <<" "<<setw(20)<<name<<" "<<setw(20)<<aname;
            cnt++;
        }
       
            fout.close();

        cout<<"\nFile saved snd closed successfully\n";

        // Reading data from file......
       
    do
    {
        cout<<"1.Puchase new book\n";
        cout<<"2.Return book\n";
        cout<<"Enter choice ";
        cin>>ch;
       
        switch(ch)
        {
        case 1:
   
            fin.open("data1.txt");
   
            if(!fin)
            {
                cout<<"Error in opening file ";
                return 0;
            }
           
            cout<<"\nEnter the id of book: ";
            cin>>findid;
           
            cout<<"\nFile contents..";
       
            for(int i=0;i<cnt;i++)
            {   
                fin>>id;
                if(strcmp(id,findid)==0)
                {
                    fin>>name;
                    fin>>aname;
                    cout<<"\nName: "<<name;
                    cout<<"\nAuthor: \n"<<aname;
                    cout<<"\nId: \n"<<id;
                   
                }
            }
            fin.close();
            break;
        case 2:
           
            fout.open("data1.txt",ios::app);
   
            if(!fout)
            {
                cout<<"Error in opening file ";
                return 0;
            }
            cout<<"How many no of books you want to return (1 or 2): ";
            cin>>n;
       
            if(n>2)
            {
                cout<<"Invalid...";
            }
           
            else
            {
   
                for(int i=0;i<n;i++)
                {
                    cout<<"\nEnter the name: ";
                    cin>>name;
   
                    cout<<"\nEnter the id: ";
                    cin>>id;
       
                    cout<<"\nEnetr the author of book:";
                    cin>>aname;
   
                    fout <<"\n"<<setw(20)<<id <<" "<<setw(20)<<name<<" "<<setw(20)<<aname;
                }
           
                cout<<"Return successfully...";
            }
           
            fout.close();
            break;
        }
   
        cout<<"You want to serch more data ? ";
        cin>>ans;
    }while(ans=='y' || ans=='Y');           
           
   
return 0;   
}

/*----------------------------------------------------------

Output

[student@localhost Pratiksha]$ g++ filemobile.cpp
[student@localhost Pratiksha]$ ./a.out
File created successfully...
How many no of records you want to insert: 3

Enter the name: Pratiksha

Enter the phone number: 123456789

Enter the name: Ankita

Enter the phone number: 98765432

Enter the name: tayi

Enter the phone number: 2345665432

File saved snd closed successfully
1.Get data from mobile no
2.get data from name
Enter choice 1

Enter the mobile no which you want to search: 123456789

File contents..
name: Pratiksha
 Mo_no:
123456789You want to serch more data ? y
1.Get data from mobile no
2.get data from name
Enter choice 2

Enter the name which you want to search: Ankita

File contents..
name: Ankita
 Mo_no:
98765432You want to serch more data ? n
[student@localhost Pratiksha]$

Comments

Popular posts from this blog

Priority Scheduling Algorithm Java Program.

Implement UNIX system calls like ps, fork, join, exec family, and wait for process management (use shell script/ Java/ C programming).

Implement a class CppArray which is identical to a one-dimensional C++ array (i.e., the index set is a set of consecutive integers starting at 0) except for the following : 1. It performs range checking. 2.It allows one to be assigned to another array through the use of assignment operator. 3.It supports a function that returns the size of the array.