/*
Problem Statement:
Write a menu driven program that will create a data file containing the list of telephone
numbers in the following form
Ganesh 23456
Gaytonde 9876
……….. ………
Use a class object to store each set of data, access the file created and implement the following
tasks
I. Determine the telephone number of specified person
II. Determine the name if telephone number is known
III. Update the telephone number, whenever there is a change.
*/
#include<iostream>
#include<stdlib.h>
#include<fstream>
#include<string.h>
using namespace std;
class tel_dic
{
public:
int phone_num;
char name[15];
void displaydata();
void getdata();
int search_num(char [] );
int search_name(int);
void update(fstream &);
};
void tel_dic::getdata()
{
cout<<"Enter phone no: ";
cin>>phone_num;
cout<<"Enter name: ";
cin>>name;
}
void tel_dic::displaydata()
{
cout<<"\nphone no. : "<<phone_num;
cout<<"\nname: "<<name<<endl;
}
int tel_dic::search_num(char name1[])
{
int flag=0;
if(strcmp(name,name1)==0)
{
flag=1;
// break;
}
return flag;
}
int tel_dic::search_name(int phone_num1)
{
int flag=0;
if(phone_num==phone_num1)
{
flag=1;
// break;
}
return flag;
}
void tel_dic::update(fstream &f)
{
int new_num;
cout<<"\nEnter new no.:";
cin>>new_num;
phone_num=new_num;
f.write((char *) this,sizeof(tel_dic));
cout<<"\n Record updated successfully";
displaydata();
}
int main()
{
int num;
cout<<"\n Enter no. of records : ";
cin>>num;
tel_dic obj;
fstream file;
int ch;
char ans;
int val;
do{
cout<<"\n MENU \n1. write \n2.read \n3.find name \n 4. find phone no. \n 5. update phone no. ";
cout<<"\n Enter choice:";
cin>>ch;
switch(ch)
{
case 1: //write data in file
file.open("dic.dat",ios::out|ios::app);
obj.getdata();
file.write((char *) &obj,sizeof(obj));
file.close();
break;
case 2://read file
file.open("dic.dat",ios::in);
file.seekg(0);
//for(int i=0;i<num;i++)
while(!file.eof())
{
file.read((char *) &obj,sizeof(obj));
if(file.eof()) break;
obj.displaydata();
}
file.close();
break;
case 3: //search phone no.
file.open("dic.dat",ios::in);
file.seekg(0);
val=0;
char name1[30];
cout<<"\nEnter name whose phone no. u want to search : ";
cin>>name1;
//for(int i=0;i<num;i++)
while(!file.eof())
{
file.read((char *) &obj,sizeof(obj));
val=obj.search_num(name1);
if(val==1)
{ cout<<"\n record found....";
obj.displaydata();
break;
}
}
if(val==0){cout<<"\n record not found" ;}
file.close();
break;
case 4 ://search name
file.open("dic.dat",ios::in);
file.seekg(0);
val=0;
int phone1;
cout<<"\nEnter phone no. whose name u want to search : ";
cin>>phone1;
while(!file.eof())
{
file.read((char *) &obj,sizeof(obj));
val=obj.search_name(phone1);
if(val==1)
{ cout<<"\n record found....";
obj.displaydata();
break;
}
}
if(val==0){cout<<"\n record not found" ;}
file.close();
break;
case 5://///// update phone no.
file.open("dic.dat",ios::in);
file.seekg(0);
val=0;
cout<<"\nEnter phone no. which u want to update : ";
cin>>phone1;
//for(int i=0;i<num;i++)
while(!file.eof())
{
file.read((char *) &obj,sizeof(obj));
val=obj.search_name(phone1);
if(val==1)
{ cout<<"\n record found....";
obj.update(file);
break;
}
}
if(val==0){cout<<"\n record not found" ;}
file.close();
}
cout<<"\n Want to continue:";
cin>>ans;
}while(ans=='y');
}
Problem Statement:
Write a menu driven program that will create a data file containing the list of telephone
numbers in the following form
Ganesh 23456
Gaytonde 9876
……….. ………
Use a class object to store each set of data, access the file created and implement the following
tasks
I. Determine the telephone number of specified person
II. Determine the name if telephone number is known
III. Update the telephone number, whenever there is a change.
*/
#include<iostream>
#include<stdlib.h>
#include<fstream>
#include<string.h>
using namespace std;
class tel_dic
{
public:
int phone_num;
char name[15];
void displaydata();
void getdata();
int search_num(char [] );
int search_name(int);
void update(fstream &);
};
void tel_dic::getdata()
{
cout<<"Enter phone no: ";
cin>>phone_num;
cout<<"Enter name: ";
cin>>name;
}
void tel_dic::displaydata()
{
cout<<"\nphone no. : "<<phone_num;
cout<<"\nname: "<<name<<endl;
}
int tel_dic::search_num(char name1[])
{
int flag=0;
if(strcmp(name,name1)==0)
{
flag=1;
// break;
}
return flag;
}
int tel_dic::search_name(int phone_num1)
{
int flag=0;
if(phone_num==phone_num1)
{
flag=1;
// break;
}
return flag;
}
void tel_dic::update(fstream &f)
{
int new_num;
cout<<"\nEnter new no.:";
cin>>new_num;
phone_num=new_num;
f.write((char *) this,sizeof(tel_dic));
cout<<"\n Record updated successfully";
displaydata();
}
int main()
{
int num;
cout<<"\n Enter no. of records : ";
cin>>num;
tel_dic obj;
fstream file;
int ch;
char ans;
int val;
do{
cout<<"\n MENU \n1. write \n2.read \n3.find name \n 4. find phone no. \n 5. update phone no. ";
cout<<"\n Enter choice:";
cin>>ch;
switch(ch)
{
case 1: //write data in file
file.open("dic.dat",ios::out|ios::app);
obj.getdata();
file.write((char *) &obj,sizeof(obj));
file.close();
break;
case 2://read file
file.open("dic.dat",ios::in);
file.seekg(0);
//for(int i=0;i<num;i++)
while(!file.eof())
{
file.read((char *) &obj,sizeof(obj));
if(file.eof()) break;
obj.displaydata();
}
file.close();
break;
case 3: //search phone no.
file.open("dic.dat",ios::in);
file.seekg(0);
val=0;
char name1[30];
cout<<"\nEnter name whose phone no. u want to search : ";
cin>>name1;
//for(int i=0;i<num;i++)
while(!file.eof())
{
file.read((char *) &obj,sizeof(obj));
val=obj.search_num(name1);
if(val==1)
{ cout<<"\n record found....";
obj.displaydata();
break;
}
}
if(val==0){cout<<"\n record not found" ;}
file.close();
break;
case 4 ://search name
file.open("dic.dat",ios::in);
file.seekg(0);
val=0;
int phone1;
cout<<"\nEnter phone no. whose name u want to search : ";
cin>>phone1;
while(!file.eof())
{
file.read((char *) &obj,sizeof(obj));
val=obj.search_name(phone1);
if(val==1)
{ cout<<"\n record found....";
obj.displaydata();
break;
}
}
if(val==0){cout<<"\n record not found" ;}
file.close();
break;
case 5://///// update phone no.
file.open("dic.dat",ios::in);
file.seekg(0);
val=0;
cout<<"\nEnter phone no. which u want to update : ";
cin>>phone1;
//for(int i=0;i<num;i++)
while(!file.eof())
{
file.read((char *) &obj,sizeof(obj));
val=obj.search_name(phone1);
if(val==1)
{ cout<<"\n record found....";
obj.update(file);
break;
}
}
if(val==0){cout<<"\n record not found" ;}
file.close();
}
cout<<"\n Want to continue:";
cin>>ans;
}while(ans=='y');
}
0 Comments