26.8.15

Write an interactive, menu-driven program

Write an interactive, menu-driven program that will access the file created in program No.17 and implement the following tasks:
i)   To determine the telephone numbers of the specified person.

ii)   To determine the name if a telephone number is given.

iii)   To update the telephone number whenever there is a change.

#include<iostream.h>

#include<fstream.h>

#include<iomanip.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h> class phone

{

public: long int no; char name[10]; void getdata()

{

cout<<endl<<"enter the name :"; cin>>name;
cout<<endl<<"enter the telephone number :"; cin>>no;

}

void putdata()

{

cout<<endl<<setw(10)<<name<<endl<<setw(10)<<no<<endl;

}

};


int main()

{


phone ph; fstream f; clrscr();
int n; f.open("pp1.txt",ios::trunc|ios::in|ios::out|ios::ate); cout<<"Enter the number of records :";

cin>>n;

for(int i=0;i<n;i++)

{

ph.getdata();

f.write((char *)&ph,sizeof(ph));

}

f.seekg(0);

cout<<endl<<"conents of the file are as follows "; while(f.read((char *)&ph,sizeof(ph))) ph.putdata();

int choice; while(1)

{

cout<<"\n1.get name from telephone number"; cout<<"\n2.get telephone number from name"; cout<<"\n3.alter the telephone number"; cout<<"\n4.EXIT";
cout<<"\nenter ur choice "; cin>>choice;
f.seekg(0);

f.clear();

switch(choice)

{

case 1:cout<<"\nEnter the telephone number to get the name "; int no;

cin>>no;

f.seekg(0);

f.clear();

while(f.read((char *)&ph,sizeof(ph)))

{
if(ph.no==no)

{

ph.putdata();

}

}

break;

case 2:cout<<"\nEnter the name to get the telephone number"; char search_name[10];

cin>>search_name; while(f.read((char *)&ph,sizeof(ph)))

{

if(strcmp(ph.name,search_name)==0) ph.putdata();

}

break;

case 3:cout<<"\nEnter the name to modify the telephone number "; char searchname[10];

cin>>searchname; while(f.read((char *)&ph,sizeof(ph)))

{

if(strcmp(ph.name,searchname)==0)

{

ph.putdata();

cout<<endl<<"Enter the new details to modify :"; ph.getdata();

int loc=sizeof(ph); int p=f.tellp(); cout<<endl<<p; f.seekp(p-loc);

f.write((char *)&ph,sizeof(ph));

}

}

cout<<endl<<"After modification the content of the file is as follows ";

f.clear();

f.seekg(0);

while(f.read((char *)&ph,sizeof(ph)))

ph.putdata();

break;

case 4:exit(0);

}

}

}

******************************OUTPUT**********************************

Enter the number of records :3 enter the name :aaa

enter the telephone number :11111 enter the name :bbb

enter the telephone number :22222 enter the name :ccc

enter the telephone number :33333 conents of the file are as follows

aaa 11111 bbb 22222 ccc 33333
1.get name from telephone number 2.get telephone number from name 3.alter the telephone number 4.EXIT
enter ur choice 1

Enter the telephone number to get the name 11111 found
aaa 11111
not foundnot foundnot found 1.get name from telephone number 2.get telephone number from name 3.alter the telephone number 4.EXIT

enter ur choice 2

Enter the name to get the telephone numberccc ccc

33333

1.get name from telephone number 2.get telephone number from name 3.alter the telephone number 4.EXIT
enter ur choice 3

Enter the name to modify the telephone number ccc ccc

33333

Enter the new details to modify : enter the name :ccc
enter the telephone number :44444 42

After modification the content of the file is as follows aaa
11111 bbb 22222 ccc 44444

1.get name from telephone number 2.get telephone number from name 3.alter the telephone number 4.EXIT
enter ur choice 4

C++ program to read the file and output the list in the tabular format

A file contains a list of names and telephone numbers in the following form:

Name             Tel. No.

Write a C++ program to read the file and output the list in the tabular format. The name should be left-justified and numbers right-justified. Use a class object to store each set of data.

#include<iostream.h>

#include<conio.h>

#include<iomanip.h>

#include<fstream.h> class phone

{

public:

char name[20]; long int phone_no; void getdata()

{

cout<<"Enter the name and phone_no"<<"\n"; cin>>name>>phone_no;

}

void putdata()

{

cout<<endl<<setw(20)<<setiosflags(ios::left)<<name<<setw(10) <<setiosflags(ios::right)<<phone_no<<endl;

}

};

void main()

{

fstream f; phone ph; int n;


f.open("phonefile",ios::out|ios::in|ios::app);
clrscr();

cout<<"\nEnter the total no_of records:"; cin>>n;
for(int i=0;i<n;i++)

{

ph.getdata();

f.write((char *)&ph,sizeof(ph));

}

f.seekg(0);

cout<<"\n\nThe content of the file is:\n\n"; cout<<setw(20)<<setiosflags(ios::left)<<"NAME"

<<setw(10)<<setiosflags(ios::right)<<"TELEPHONE_NO"; while(f.read((char *)&ph,sizeof(ph)))

ph.putdata();

f.close();

getch();

}


**********************************OUTPUT********************************

Enter the total no_of records:3 Enter the name and phone_no RAGHU
2456570

Enter the name and phone_no RAJEEV
2457859

Enter the name and phone_no RANJU
2451230

The content of the file is:

NAME                TELEPHONE_NO

RAGHU                  2456570

RAJEEV                2457859

RANJU                   2451230

C++ program that uses a single file

Write a C++ program that uses a single file for both reading and writing the data.

#include<iostream.h>

#include<conio.h>

#include<fstream.h> int main()
{

clrscr();

ofstream outf("ITEM.txt"); cout<<"Enter the filename:"; char name[30];

cin>>name;

outf<<name<<"\n"; cout<<"Enter ITEM cost:"; float cost;

cin>>cost;
outf<<cost;
outf.close();

ifstream inf("ITEM.txt"); inf>>name;
inf>>cost;

cout<<"The name of the item is:"<<name; cout<<"\nItem cost is :"<<cost; inf.close();

getch(); return 0;
}

**************************************OUTPUT****************************

Enter the ITEM name:vicks

Enter ITEM cost:20.3
The name of the item is:vicks

Item cost is :20.299999

Design your own manipulator

Design your own manipulator to provide the following output specification for printing money value:
1)  10 columns width

2)  The character '$' at the beginning

3)  Showing '+' sign.

4)  Two digits precision

5)  Filling of unused spaces with ' * '

6)  Trailing zeros shown


#include<iostream.h>

#include<conio.h>

#include<iomanip.h>

ostream &moneyshow(ostream &output)

{

cout<<'$'; cout.fill('*');

cout.setf(ios::showpos);

cout.setf(ios::showpoint);

cout.width(10);

cout.precision(2); return output;

}

void main()

{

double amount; clrscr();

cout<<"enter the value:"; cin>>amount;

cout<<"\nyour money value:"; cout<<moneyshow<<amount; getch();

}

  


*************************OUTPUT 1***************************************

enter the value:12.2

your money value:$****+12.20

*************************OUTPUT 2***************************************

enter the value:34

your money value:$****+34.00

C++ program to read a list

Write a C++ program to read a list containing item name, item code and cost interactively and display the data in a tabular format as shown below:

NAME  CODE  COST

#include<iostream.h>

#include<conio.h>

#include<iomanip.h> int main()

{

int n,itcode[10]; char item[10][10]; float itcost[10]; clrscr();

cout<<"Enter the no of item:"; cin>>n;
for(int i=0;i<n;i++)

{

cout<<"Enter the item details:"<<i+1; cout<<"\nEnter name:"; cin>>item[i];

cout<”Enter code:"; cin>>itcode[i]; cout<<"Enter cost:"; cin>>itcost[i];

}

cout<<endl<<setw(15)<<"Item name"<<setw(15)<<"item code"<<setw(15)<<"cost"<<endl;


cout<<"\n-------------------------------------
\n";
for(i=0;i<n;i++)

{

cout.fill( ' - ' );

cout.setf(ios::left,ios::adjustfield);

cout.width(15);

cout<<item[i];

cout.setf(ios::left,ios::adjustfield);

cout.width(15);

cout<<itcode[i];

cout.setf(ios::right,ios::adjustfield);

cout<<itcost[i]<<endl;

}

getch();

return 0;

}

***********************************OUTPUT*************************


Enter the no of item:3

Enter the item details:1

Enter name:Rice

Enter code:111

Enter cost:35.4

Enter the item details:2

Enter name:Sugar

Enter code:112

Enter cost:12.3

Enter the item details:3

Enter name:Soap

Enter code:113

Enter cost:60.4

Item name   item code           cost

------------------------------------------------

Rice-----------
111------------
35.400002
Sugar----------
112------------
12.3
Soap-----------
113------------
60.400002