(14) Define a class Employees. Also define classes of MaleEmp...
Shared By:hiren patel (SRIMCA - Tarsadi) Show/Hide Program
/*
Define a class Employees. Also define classes of MaleEmp and FemaleEmp
inheriting from that. Define classes Officers, Clercks and peons again inheriting
from Employee class. Define an array which contains 10 different types of
employees. Define a function ReadDetails() in all above classes.
All array elements should be able to be accessed in
the same routine irrespective of their type.
*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Employees
{
protected:
char name[20],desi[20],gen[7];
public:
virtual void ReadDetail()
{
cout<<"Enter name : - ";cin>>name;
}
void disp()
{
cout<<"--------------------------------"<<endl;
cout<<"\t"<<name<<"\t"<<gen<<"\t"<<desi<<endl;
}
};
class MaleEmp : virtual public Employees
{
public:
void ReadDetail()
{
strcpy(gen,"Male");
}
};
class FemaleEmp : virtual public Employees
{
public:
void ReadDetail()
{
strcpy(gen,"Female");
}
};
class Officers : virtual public Employees,public MaleEmp,public FemaleEmp
{
protected :
void ReadDetail()
{
int ch;
Employees::ReadDetail();
do{
cout<<"Select (1/2) for Male & Female Respectively :: ";
cin>>ch;
}while(ch < 1 || ch > 2);
if(ch==1)
{
MaleEmp::ReadDetail();
}
else
{
FemaleEmp::ReadDetail();
}
strcpy(desi,"Officer");
}
};
class Clercks : virtual public Employees,public MaleEmp,public FemaleEmp
{
protected :
void ReadDetail()
{
int ch;
Employees::ReadDetail();
do{
cout<<"Select (1/2) for Male & Female Respectively :: ";
cin>>ch;
}while(ch < 1 || ch > 2);
if(ch==1)
{
MaleEmp::ReadDetail();
}
else
{
FemaleEmp::ReadDetail();
}
strcpy(desi,"Clerks");
}
};
class Peons : virtual public Employees,public MaleEmp,public FemaleEmp
{
protected :
void ReadDetail()
{
int ch;
Employees::ReadDetail();
do{
cout<<"Select (1/2) for Male & Female Respectively :: ";
cin>>ch;
}while(ch < 1 || ch > 2);
if(ch==1)
{
MaleEmp::ReadDetail();
}
else
{
FemaleEmp::ReadDetail();
}
strcpy(desi,"Peons");
}
};
void main()
{
clrscr();
Employees *e[10];
e[0] = new Officers;
e[1] = new Clercks;
e[2] = new Peons;
e[3] = new Clercks;
e[4] = new Officers;
e[5] = new Peons;
e[6] = new Officers;
e[7] = new Peons;
e[8] = new Officers;
e[9] = new Clercks;
for(int i=0;i<10;i++)
{
e[i]->ReadDetail();
}
clrscr();
cout<<"\nDisplay Data of Employee"<<endl;
cout<<"\tName\tGender\tDesignation\n";
for(i=0;i<10;i++)
{
e[i]->disp();
}
delete []e;
getch();
}
Shared By:Mr.Adarsh Patel (Lecturer - AIIS - Anand) Show/Hide Program
/*
Written by :Adarsh Patel-This is a Sample Program..
Define a class Employees. Also define classes of MaleEmp and FemaleEmp
inheriting from that. Define classes Officers, Clercks and peons again inheriting
from Employee class. Define an array which contains 10 different types of
employees. Define a function ReadDetails() in all above classes. All array elements
should be able to be accessed in the same routine irrespective of their type.
All array elements should be able to be accessed in
the same routine irrespective of their type.
*/
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
class Employees
{
private:
char name[20];
public:
Employees(char n[20])
{
strcpy(name,n);
}
};
class MaleEmp : public Employees
{
private:
int basic;
public:
MaleEmp(int b,char n[20]) : Employees(n)
{
basic=b;
}
void
};
class FemaleEmp : public Employees
{
private:
int basic;
public:
FemaleEmp(int b,char n[20]) : Employees(n)
{
basic=b;
}
};
class Officers : public Employees
{
private:
int basic;
public:
Officers(int b,char n[20]) : Employees(n)
{
basic=b;
}
};
class Clercks : public Employees
{
private:
int basic;
public:
Clercks(int b,char n[20]) : Employees(n)
{
basic=b;
}
};
class Peons : public Employees
{
private:
int basic;
public:
Peons(int b,char n[20]) : Employees(n)
{
basic=b;
}
};
void main()
{
clrscr();
getch();
}
(15) Consider a class network of fig . The class master derives...
Shared By :Mr.S.K.Patel (Lecturer, AITS, Rajkot) Show/Hide Program
/* Program No. 15 Consider a class network of fig . The class master derives information
from both account and admin classes which in turn derived derive information from the class
person. Define all the four classes and write a program to create , update and display
the information contained in master objects.
By- S.K.Patel, Lecturer, MCA Dept., Atmiya Institute of Technology and Science, Rajkot.
Programmed developed in :- Microsoft Visual C++ */
#include<iostream.h>
static int i=1;
class person
{
protected:
int code;
char name[20];
};
class admin : virtual public person
{
protected:
int experience;
};
class account : public virtual person
{
protected:
int pay;
};
class master : public admin, public account
{
public:
void create();
void update(int);
void display();
};
void master ::create()
{
cout << "\n Employee code is -> " << i;
code = i;
cout << "\n Enter employee name -> ";
cin >> name;
cout << " Enter experiance -> ";
cin >> experience;
cout << " Enter pay -> ";
cin >> pay;
if(i>10)
i=0;
else
i+=1;
}
void master ::update(int c)
{
cout << "\n Employee code -> " << c;
cout << "\n Enter employee name -> ";
cin >> name;
cout << " Enter experiance -> ";
cin >> experience;
cout << " Enter pay -> ";
cin >> pay;
}
void master :: display()
{
cout << "\n\n ----- Employee Information -----";
cout << "\n Employee code -> " << code;
cout << "\n Employee name -> " << name;
cout << "\n Experiance -> " << experience;
cout << "\n Pay -> " << pay;
cout << "\n --------------------------------";
}
void main()
{
int n,ch,c;
cout << "\n How many employee profile you want to create? -> ";
cin >> n;
master m[10];
do
{
cout << "\n\n 1. Create a new employee record";
cout << "\n 2. Update an existing employee record";
cout << "\n 3. Display employee record";
cout << "\n 0. Exit test";
cout << "\n\n Enter your choice -> ";
cin >> ch;
switch(ch)
{
case 1:
m[i].create();
break;
case 2:
cout << "\n Enter emplyee to code to update record -> ";
cin >> c;
m[c].update(c);
break;
case 3:
cout << "\n Enter employee code to display record -> ";
cin >> c;
m[c].display();
break;
}
}while(ch!=0);
}
Shared By :DHAVAL M. DEVANI(B.H.GARDI - Rajkot) Show/Hide Program
#include<iostream>
using namespace std;
class person
{
public:
int code;
char name[30];
};
class admin:virtual public person
{
public:
int exp;
};
class account:virtual public person
{
public:
int pay;
};
class master:public admin,public account
{
public:
void getdata()
{
cout<<"Enter the code::-"<<endl;
cin>>code;
cout<<"Enter the name"<<endl;
cin>>name;
cout<<"enter the experience"<<endl;
cin>>exp;
cout<<"enter the pay"<<endl;
cin>>pay;
}
void putdata()
{
cout<<"code::-"<<code<<endl;
cout<<"name::-"<<name<<endl;
cout<<"exp::-"<<exp<<endl;
cout<<"pay::-"<<pay<<endl;
}
void updatedata()
{
cout<<"Enter the code::-"<<endl;
cin>>code;
cout<<"Enter the name"<<endl;
cin>>name;
cout<<"enter the experience"<<endl;
cin>>exp;
cout<<"enter the pay"<<endl;
cin>>pay;
}
};
void main()
{
master m1;
m1.getdata();
m1.putdata();
m1.updatedata();
//m1.putdata();
cin.get();
}
Shared By :Rashesh Patel(LICA - Valsad) Show/Hide Program
/* 15. The class master derives information from both
account and admin classes which in turn derived derive information from the class
person. Define all the four classes and write a program to create , update and
display the information contained in master objects.
*/
#include<conio.h>
#include<iostream.h>
class person
{
protected :
int code;
char pname[15];
};
class admin : virtual public person
{
protected:
float exp;
};
class account : virtual public person
{
protected:
double pay;
};
class master : public admin,public account
{
public:
void get()
{
cout<<"\nEnter Person code,Name,Experince & pay";
cin>>code>>pname>>exp>>pay;
}
void put()
{
cout<<"\nPerson Code is "<<code;
cout<<"\nPerson Name is "<<pname;
cout<<"\nPerson Experince is "<<exp;
cout<<"\nPerson Pay is "<<pay;
}
void update()
{
cout<<"\nOld object is ";
put();
cout<<"\nEnter New values for this object";
get();
cout<<"\nNow New object";
put();
}
int getcode()
{
return code;
}
};
int main()
{
clrscr();
master m1[10];
int ch,i=0,k,flag;
int pcode;
while(1)
{
cout<<"\n=============MENU==============";
cout<<"\n1=Create Object";
cout<<"\n2=Update Object";
cout<<"\n3=Display Object";
cout<<"\n4=Exit";
cout<<"\nEnter Your Choice ";
cin>>ch;
switch(ch)
{
case 1: if(i>9)
{
cout<<"\nNo more object can be created";
break;
}
m1[i++].get();
break;
case 2:
cout<<"\nEnter Code No to update";
cin>>pcode;
flag=0;
for(k=0;k<i;k++)
{
if(pcode==m1[k].getcode())
{
m1[k].update();
flag=1;
break;
}
}
if(flag==0)
cout<<"\nSorry Code not found";
break;
case 3:
cout<<"\nEnter Code No to display";
cin>>pcode;
flag=0;
for(k=0;k<i;k++)
{
if(pcode==m1[k].getcode())
{
m1[k].put();
flag=1;
break;
}
}
if(flag==0)
cout<<"\nSorry Code not found";
break;
case 4: return 0;
default : cout<<"\nInvalid Choice";
}
}
}
(16) Create a base class called shape. Use this class to store two double type values...
Shared By :Mr.S.K.Patel (Lecturer, AITS, Rajkot) Show/Hide Program
/* Prog.No 16 Create a base class called shape. Use this class to store two double type values that
could be used to compute the area of figures. Derive three specific classes
calledtriangle, rectangle and circle from the base shape. Add to the base class, a
member function get_data( ) to initialize base class data members and another
member function display_area( ) to compute and display the area of figures.Make
display_area( ) as a virtual function and redefine this function in derived classes to
suit their requirements. Using these three classes design a program that will accept
dimensions of a triangle or rectangle interactively and store it in one array . After
having read all the input display the area of all the figures whose area has been read
in the program. Remember the two values given as input will be treated as lengths
of two sides in the case of rectangle and as base and height in case of triangle. In
case of circle only one value should be accepted which will be taken as the radius
and the default value of the next parameter should be 0.
By - Mr. S.K.Patle, Lecturer, MCA Dept, Atmiya Institute of Technology & Science, Rajkot.
Developed in :- Microsoft Visual C++ */
#include<iostream.h>
#define PI 3.1415927
class shape
{
protected:
double val1;
double val2;
double area;
public:
void get_data(double v1, double v2)
{
val1 = v1;
val2 = v2;
area=0;
}
virtual void display_area()
{
}
};
class triangle:public shape
{
public:
virtual void display_area()
{
area = (val1*val2)/2;
cout << "\n Area of triangle= " << area;
cout << "\n ----------------------------\n";
}
};
class rectangle:public shape
{
public:
virtual void display_area()
{
area = val1*val2;
cout << "\n Area of rectangle= " << area;
cout << "\n ----------------------------\n";
}
};
class circle:public shape
{
public:
virtual void display_area()
{
area = PI * (val1*val1);
cout << "\n Area of circle= " << area;
cout << "\n ----------------------------\n";
}
};
void main()
{
double v1,v2;
int ch;
shape obS;
triangle obT;
rectangle obR;
circle obC;
shape *s_ptr[] = {&obT, &obR, &obC};
do
{
cout << "\n 1. Compute area of triangle";
cout << "\n 2. Compute area of rectangle";
cout << "\n 3. Compute area of circle";
cout << "\n 0. Exit";
cout << "\n Enter your choice -> ";
cin >> ch;
switch(ch)
{
case 1:
cout << "\n ----------------------------";
cout << "\n Enter value for base -> ";
cin >> v1;
cout << " Enter value for height -> ";
cin >> v2;
s_ptr[0]->get_data(v1,v2);
s_ptr[0]->display_area();
break;
case 2:
cout << "\n ----------------------------";
cout << "\n Enter value for breadth -> ";
cin >> v1;
cout << " Enter value for height -> ";
cin >> v2;
s_ptr[1]->get_data(v1,v2);
s_ptr[1]->display_area();
break;
case 3:
cout << "\n ----------------------------";
cout << "\n Enter value for radius -> ";
cin >> v1;
s_ptr[2]->get_data(v1,0);
s_ptr[2]->display_area();
break;
}
}while(ch!=0);
}
Shared By :Dhaval M. Devani(B.H.Gardi - Rajkot) Show/Hide Program
/*
Create a base class called shape. Use this class to store two double type values that could be used to compute the area of figures.
Derive three specific classes calledtriangle, rectangle and circle from the base shape. Add to the base class,
a member function get_data( ) to initialize base class data members and another member function display_area( ) to compute and display the
area of figures.Make display_area( ) as a virtual function and redefine this function in derived classes to suit their requirements.
Using these three classes design a program that will accept dimensions of a triangle or rectangle interactively and store it in one array.
After having read all the input display the area of all the figures whose area has been read in the program. Remember the two values given
as input will be treated as lengths of two sides in the case of rectangle and as base and height in case of triangle. In case of circle
only one value should be accepted which will be taken as the radius and the default value of the next parameter should be 0.*/
#include<iostream>
#include<string>
using namespace std;
class shape
{
protected:
double area;
public:
virtual void input()
{
}
virtual void disarea()
{
}
};
class triangle:public shape
{
double b,h,c;
public:
void input()
{
cout<<endl<<"Enter the Base::-";
cin>>b;
cout<<endl<<"Enter the Height::-";
cin>>h;
c=0.5*(h*b);
// cout<<endl<<"area of triabgle"<<c;
}
void disarea()
{
cout<<endl<<"AREA OF TRIANGLE::-"<<c;
}
};
class rectangle:public shape
{
double a,b,c;
public:
void input()
{
cout<<endl<<"Enter the length::-";
cin>>a;
cout<<endl<<"Enter the breath::-";
cin>>b;
c=2*(a+b);
}
void disarea()
{
cout<<endl<<"AREA OF RECTANGLE::-"<<c;
}
};
class circle:public shape
{
double a,c;
public:
void input()
{
cout<<endl<<"Enter the radios::-";
cin>>a;
c=3.14*(a*a);
}
void disarea()
{
cout<<endl<<"AREA OF CIRCLE::-"<<c;
}
};
void main()
{
int ch;
shape *ptr;
while(1)
{
cout<<endl<<"1..triangle";
cout<<endl<<"2..rectangle";
cout<<endl<<"3..circle";
cout<<endl<<"4..EXIT";
cout<<endl<<"Enter your choice::-";
cin>>ch;
switch(ch)
{
case 1:ptr= new triangle;break;
case 2:ptr= new rectangle;break;
case 3:ptr= new circle;break;
case 4:exit(1);
}
ptr->input();
ptr->disarea();
}
cin.get();
}
(17) ABC publishing company markets both book and audio cassette...
/* ABC publishing company markets both book and audio cassette versions of its work. Create a class
called publication that stores the title (a string) and price (type float) of a publication. From
this class derive two classes: book, which adds a page count (type int); and tape, which adds playing
time in minutes (type float). Write a main program that reads both book and tape information in one
array. When the user has finished entering data for all books and tapes, displays the resulting
Data for all the books and tapes entered.Also count no of book and cassette entries in the Array
using runtime identification feature of C++.*/
#include<iostream>
#include<string>
using namespace std;
class publication
{
public:
string title;
float price;
};
class book : public publication
{
public:
int pg_cnt;
void getBookData()
{
cout<<endl<<"Enter Title :";
cin>>title;
cout<<endl<<"Enter Price :";
cin>>price;
cout<<endl<<"Enter Pages : ";
cin>>pg_cnt;
}
void putBookData()
{
cout<<endl<<" Title :"<<title;
cout<<endl<<" Price :"<<price;
cout<<endl<<" Pages : "<<pg_cnt;
}
};
class tape : public publication
{
float ply_time;
public :
void getTapeData()
{
cout<<endl<<"Enter Title :";
cin>>title;
cout<<endl<<"Enter Price :";
cin>>price;
cout<<endl<<"Enter Playing Time oF Cassette :";
cin>>ply_time;
}
void putTapeData()
{
cout<<endl<<" Title :"<<title;
cout<<endl<<" Price :"<<price;
cout<<endl<<" Playing Time oF Cassette :"<<ply_time;
}
};
void main()
{
book b[3];
tape t[3];
cout<<"Book DATA"<<endl;
for(int i=0;i<3;i++)
b[i].getBookData();
cout<<"Tape DATA"<<endl;
for(int j=0;j<3;j++)
t[j].getTapeData();
cout<<"Printing BOOK DATA"<<endl;
for(j=0;j<3;j++)
b[j].putBookData();
cout<<"Printing TAPE DATA"<<endl;
for(j=0;j<3;j++)
t[j].putTapeData();
cin.get();
}
Shared By:Mr.Adarsh Patel (Lecturer - AIIS - Anand) Show/Hide Program
/*
Written by :Adarsh Patel-This is a Sample Program..
ABC publishing company markets both book and audio cassette versions of its
work.
Create a class called publication that stores the
title( a string) and price( type float) of a publication.
From this class derive two classes :
book , which adds a
page count (type int);
and
tape, which adds playing time in minutes (type float).
Write a main program that reads both book and tape information in one array. When the
user has finished entering data for all books and tapes, displays the resulting data
for all the books and tapes entered. Also count no of book and cassette entries in the
array using runtime identification feature of C++.
*/
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
class Publication
{
protected:
char title[20];
float price;
public:
Publication(char t[],float p)
{
strcpy(title,t);
price=p;
}
};
class Book : public Publication
{
private:
int pagecount;
public:
Book(int pc,char t[20],float p) : Publication(t,p)
{
pagecount=pc;
}
void print()
{
cout << endl << endl << "-: Book Informations :-" << endl;
cout << "Title : " << title << endl;
cout << "Page : " << pagecount << endl;
cout << "Price : " << price << endl;
}
};
class Tape : public Publication
{
private:
float minutes;
public:
Tape(float m,char t[20],float p) : Publication(t,p)
{
minutes=m;
}
void print()
{
cout << endl << endl << "-: Tape Informations :-" << endl;
cout << "Title : " << title << endl;
cout << "Minutes : " << minutes << endl;
cout << "Price : " << price << endl;
}
};
void main()
{
clrscr();
int page;
char title[20];
float price,minutes;
cout << endl << ":: Enter Book Informations :: " << endl;
cout << "Title : ";
gets(title);
cout << "No of Pages : ";
cin >> page;
cout << "Price : ";
cin >> price;
Book b1(page,title,price);
b1.print();
cout << endl << ":: Enter Tape Informations :: " << endl;
cout << "Title : ";
gets(title);
cout << "Minutes : ";
cin >> minutes;
cout << "Price : ";
cin >> price;
Tape t1(minutes,title,price);
t1.print();
getch();
}
(18) There are two classes Emp and Employee. Emp is defined in the payroll
Share This Program..
send it gtumca@gmail.com
with your name - college name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
Shared By :Your Name Show/Hide Program
(19) Define a class Result which contains the result of an MCA written...
Shared By :Mr.S.K.Patel (Lecturer, AITS, Rajkot) Show/Hide Program
/* Prog.(19 and 20) Define a class Result which contains the result of an MCA written test.
It should take merit list from a file and display on the screen such that at a time only ten
candidates information is printed on the screen. The headings should be displayed
using a manipulator. All the information should be aligned with the headings.
Developed in : Microsoft Visual C++
By- Mr.S.K.Patel, Lecturer, MCA Dept., Atmiya Institute of Technology and Science, Rajkot.
*/
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<conio.h>
#define PERPAGEREC 10
using namespace std;
static int p=1;
static int counter=0;
static srno=0;
static total=0;
class result
{
int id;
char name[20];
int score;
public:
void readstudent()
{
cout << "\n Enter id-> ";
cin >> id;
cout << "\n Enter name-> ";
cin >> name;
cout << "\n Enter score-> ";
cin >> score;
cout << " -----------------------------------";
}
void writestudent()
{ counter++;
srno++;
if(counter ==1)
{
cout << "\n\n --------------------------------------------";
cout << "\n " << setw(8) << "Sr.No" << setw(6) << "ID" << setw(20) << "Name of student" << setw(7) << "Score" << endl;
cout << " --------------------------------------------\n";
}
if(counter > PERPAGEREC)
{
char e;
cout << " --------------------------------------------\n";
cout << " Page no: " << p;
cout << "\tpress 'c' to continue... -> ";
cin >> e;
counter=1;
cout << "\n\n --------------------------------------------\n";
cout << " " << setw(8) << "Sr.No" << setw(6) << "ID" << setw(20) << "Name of student" << setw(7) << "Score" << endl;
cout << " --------------------------------------------\n";
p++;
}
if(srno<=total)
cout << " " << setw(8) << srno << setw(6) << id << setw(21) << name << setw(7) << score << endl;
}
}r;
void main()
{
ofstream entry("mca",ios::binary);
char c='y';
cout << "\n Enter Result data";
cout << "\n -----------------";
do
{
r.readstudent();
entry.write((char*) &r,sizeof(r));
cout << "\n Do you want to continue? [y/n] -> ";
cin >> c;
total++;
}
while(c!='n');
entry.close();
ifstream out("mca",ios::binary);
while(out.read((char*) &r,sizeof(r)))
{
char e;
cout << setiosflags(ios::left);
r.writestudent();
}
if(srno>0)
{
cout << " --------------------------------------------\n";
cout << " Page no: " << p;
cout << " | Total records= " << srno;
cout << " | End of File." << endl;
cout << "\n By- S.K.Patel, Atmiya Insti. of Tech. & Sci. Rajkot|";
}
getch();
}
(20) Write a C++ program that displays a student object read from a file backwards.
Shared By :Mr.S.K.Patel (Lecturer, AITS, Rajkot) Show/Hide Program
/* Prog.(19 and 20) Define a class Result which contains the result of an MCA written test.
It should take merit list from a file and display on the screen such that at a time only ten
candidates information is printed on the screen. The headings should be displayed
using a manipulator. All the information should be aligned with the headings.
Developed in : Microsoft Visual C++
By- Mr.S.K.Patel, Lecturer, MCA Dept., Atmiya Institute of Technology and Science, Rajkot.
*/
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<conio.h>
#define PERPAGEREC 10
using namespace std;
static int p=1;
static int counter=0;
static srno=0;
static total=0;
class result
{
int id;
char name[20];
int score;
public:
void readstudent()
{
cout << "\n Enter id-> ";
cin >> id;
cout << "\n Enter name-> ";
cin >> name;
cout << "\n Enter score-> ";
cin >> score;
cout << " -----------------------------------";
}
void writestudent()
{ counter++;
srno++;
if(counter ==1)
{
cout << "\n\n --------------------------------------------";
cout << "\n " << setw(8) << "Sr.No" << setw(6) << "ID" << setw(20) << "Name of student" << setw(7) << "Score" << endl;
cout << " --------------------------------------------\n";
}
if(counter > PERPAGEREC)
{
char e;
cout << " --------------------------------------------\n";
cout << " Page no: " << p;
cout << "\tpress 'c' to continue... -> ";
cin >> e;
counter=1;
cout << "\n\n --------------------------------------------\n";
cout << " " << setw(8) << "Sr.No" << setw(6) << "ID" << setw(20) << "Name of student" << setw(7) << "Score" << endl;
cout << " --------------------------------------------\n";
p++;
}
if(srno<=total)
cout << " " << setw(8) << srno << setw(6) << id << setw(21) << name << setw(7) << score << endl;
}
}r;
void main()
{
ofstream entry("mca",ios::binary);
char c='y';
cout << "\n Enter Result data";
cout << "\n -----------------";
do
{
r.readstudent();
entry.write((char*) &r,sizeof(r));
cout << "\n Do you want to continue? [y/n] -> ";
cin >> c;
total++;
}
while(c!='n');
entry.close();
ifstream out("mca",ios::binary);
while(out.read((char*) &r,sizeof(r)))
{
char e;
cout << setiosflags(ios::left);
r.writestudent();
}
if(srno>0)
{
cout << " --------------------------------------------\n";
cout << " Page no: " << p;
cout << " | Total records= " << srno;
cout << " | End of File." << endl;
cout << "\n By- S.K.Patel, Atmiya Insti. of Tech. & Sci. Rajkot|";
}
getch();
}
Shared By :Hetal Modi (CTI-Gandhinagar) Show/Hide Program
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class Student
{
int Roll_no;
int subj1,subj2,total;
public:
Student()
{
}
Student(int r_n,int s1,int s2)
{
Roll_no=r_n;
subj1=s1;
subj2=s2;
total=s1+s2;
}
void display();
};
void Student::display()
{
cout<<"\n\t"<<Roll_no;
cout<<"\t"<<subj1;
cout<<"\t\t"<<subj2;
cout<<"\t\t"<<total;
}
int main()
{
int val,t_byte,t_obj,i;
Student s1(1,30,60);
Student s2(2,40,80);
Student s3(3,35,70);
Student s4;
clrscr();
fstream fs;
fs.open("One.txt",ios::in | ios::out);
fs.write((char *)&s1,sizeof(s1));
fs.write((char *)&s2,sizeof(s2));
fs.write((char *)&s3,sizeof(s3));
val=fs.tellg(); // Total byte
t_byte=sizeof(s1); // Size of one object
t_obj=val/t_byte; // Total Object
cout<<"\n Total Byte: "<<val;
cout<<"\n Size of object: "<<t_byte;
cout<<"\n Total Object: "<<t_obj;
cout<<"\n-------------------------------";
// fs.seekg(0);
cout<<"\n\tRoll No\tSubject1\tSubject2\tTotal\n";
cout<<"\t----------------------------------------------\t";
// Reverse Print
for(i=1;i<=t_obj;i++)
{
fs.seekg(val-t_byte*i,ios::beg);
fs.read((char *)&s4,sizeof(s4));
s4.display();
}
fs.close();
getch();
return 0;
}
(21) Use an Employee Class to write records of employee to a file. Include...
Shared By :Mr.S.K.Patel (Lecturer, AITS, Rajkot) Show/Hide Program
/*
Prog. (21) Use an Employee Class to write records of employee to a file.
Include a menu that will allow the user to select any of the following features
a. Add a new record.
b. Modify an existing record.
c. Retrieve and display an entire record for a given name.
d. Generate a complete list of all names, addresses and telephone numbers.
e. End of the computation.
-----------------------------------------------------------------------------------
Developed by : Mr. S.K.Patel, Lecturer, MCA Dept.
(Atmiya Institute of Technology & Science, Rajkot)
Developed in : Microsoft Visual C++ 6.0
-----------------------------------------------------------------------------------
Reference from : E.Balagurusamay, 2nd edition
Vedant.
*/
#include<iostream>
#include<fstream>
#include<string>
static int rec=1;
static int count=0;
static char nm[15];
using namespace std;
class employee
{
char name[15];
char address[20];
char teleno[15];
public:
void addRecord();
void modifyRecord();
void searchRecord();
void displaylist();
};
void employee :: addRecord()
{
cout << "\n Enter name -> ";
cin >> name;
cout << " Enter address CITY -> ";
cin >> address;
cout << " Enter telephone number -> ";
cin >> teleno;
}
void employee :: modifyRecord()
{
cout << "\n\n Enter new name -> ";
cin >> name;
cout << "\n Enter address CITY -> ";
cin >> address;
cout << "\n Enter telephone number -> ";
cin >> teleno;
}
void employee :: searchRecord()
{
if(strcmp(nm,name)==0)
{
displaylist();
count++;
}
}
void employee :: displaylist()
{
cout << "\n\n\n\ -> Record no: " << rec;
cout << "\n Name: " << name;
cout << "\n Address: " << address;
cout << "\n Tele no: " << teleno;
rec++;
}
void main()
{
employee e;
int flag=0,c, recpos,location;
int r=0, s=sizeof(e),m;
char ch;
if(flag==0)
{
ofstream myfile;
myfile.open("emp.dat", ios::app);
myfile.close();
}
do
{
cout << "\n\n ----------------------------------------------------------";
cout << "\n 1. Add a new record";
cout << "\n 2. Modify an existing record";
cout << "\n 3. Retrieve and display an entire record for a given name";
cout << "\n 4. Display all";
cout << "\n 0. Exit";
cout << "\n ----------------------------------------------------------";
cout << "\n Enter your choice -> ";
cin >> c;
cout << "\n ======================================";
fstream efile;
efile.open("emp.dat",ios::ate|ios::in|ios::out|ios::binary);
switch(c)
{
case 1:
e.addRecord();
cin.get(ch);
efile.write((char*) &e, sizeof(e))<<flush;
break;
case 2:
cout << "\n Enter the record no. to be modify -> ";
cin >> recpos;
cin.get(ch);
location = (recpos-1) * sizeof(e);
if(efile.eof())
efile.clear();
efile.seekp(location);
cout << "\n Enter new values for employee";
e.addRecord();
cin.get(ch);
efile.write((char*) &e, sizeof(e)) << flush;
efile.seekg(0);
rec=1;
while(efile.read((char*) &e,sizeof(e)))
{
e.displaylist();
}
break;
case 3:
efile.seekg(0,ios::end);
m=efile.tellg();
cout << "\n Enter employee name -> ";
cin >> nm;
r=count=0;
m=efile.tellg();
rec=1;
while(r<m)
{
efile.seekg(r,ios::beg);
efile.read((char*) &e,sizeof(e));
e.searchRecord();
r=r+s;
}
if(count==0)
cout << "\n ---> Record not found <---";
break;
case 4:
efile.seekg(0,ios::beg);
rec=1;
while(efile.read((char*) &e,sizeof(e)))
{
e.displaylist();
}
efile.clear();
break;
case 5:
efile.close();
break;
}
}while(c!=0);
}
Shared By :Dhaval M.Devani(B.H.GARDI - RAJKOT) Show/Hide Program
/*(1) Use an Employee Class to write records of employee to a file. Include a menu that will allow the user to select any of the following features
a. Add a new record.
b. Modify an existing record.
c. Retrieve and display an entire record for a given name.
d. Generate a complete list of all names, addresses and telephone numbers.
e. End of the computation.
*/
#include<iostream>
#include<fstream>
using namespace std;
void add();
void Modify();
void Displayall();
int location=0;
class tool
{
public:
int r_no;
char t_name[30];
int qty;
int cost;
public:
void input()
{
cout<<endl<<"Enter the Empid :-";
cin>>r_no;
cout<<endl<<"Enter the name :-";
cin>>t_name;
cout<<endl<<"Enter the quantity :-";
cin>>qty;
cout<<endl<<"Enter the cost :-";
cin>>cost;
}
void display()
{
cout<<endl<<"r_no :- "<<r_no;
cout<<endl<<"Name :- "<<t_name;
cout<<endl<<"Quantity:- "<<qty;
cout<<endl<<"Cost :- "<<cost;
}
};
void main()
{
int ch;
do
{
cout<<endl<<"1..Add record :-";
cout<<endl<<"2..Modify record. :-";
cout<<endl<<"3..Display record :-";
cout<<endl<<"4..EXIT::-";
cout<<endl<<"Enter your choice :-";
cin>>ch;
switch(ch)
{
case 1:
add();
break;
case 2:
Modify();
break;
case 3:
Displayall();
break;
case 4:
exit(1);
}
}while(ch!=5);
cin.get();
}
void add()
{
tool e1;
ofstream out("tool.txt",ios::out|ios::binary|ios::app);
e1.input();
out.write((char *)&e1,sizeof(tool));
out.close();
}
void Modify()
{
tool e1;
int flg=0;
fstream in("tool.txt",ios::in |ios::out|ios::ate|ios::binary);
char t_name[30];
cout<<endl<<"Enter the t_name to Update:-";
flushall();
cin>>t_name;
int i=0;
while(!in.eof())
{
in.read((char *)&e1,sizeof(tool));
if(strcmp(e1.t_name ,t_name)==0)
{
e1.input();
in.seekp(i*(sizeof(tool)),ios::beg);
in.write((char *)&e1,sizeof(tool));
flg=1;
break;
}
i++;
}
if(flg==0)
{
cout<<endl<<"NOT FOUND";
}
else
{
cout<<endl<<"RECORD HAS BEEN MODIFIED SUCCESSFULLY";
}
in.close();
}
void Displayall()
{
ifstream in("tool.txt",ios::in|ios::binary);
int count=0;
in.seekg(0,ios::beg);
while(!in.eof())
{
tool e1;
in.read((char *)&e1,sizeof(tool));
e1.display();
}
in.close();
}
(22) Write a program that returns the size in bytes of a file entered on the command line.
Shared By :Milan K. Vachhani(Lecturer - B.H.GARDI - RAJKOT) Show/Hide Program
#include<iostream>
#include<fstream>
using namespace std;
void main(int argc,char *argv[])
{
int cnt=0;
char ch;
if(argc!=2)
{
cout<<"Please Enter 2 Arguments";
exit(0);
}
ifstream in(argv[1]);
while(!in.eof())
{
in.get(ch);
cnt += in.gcount();
}
cout<<"Total No of bytes in File = "<<cnt;
cin.get();
}
Shared By :Your Name Show/Hide Program
Share This Program..
send it gtumca@gmail.com
with your name - college name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
(23) You are the owner of a hardware store and need to keep an...
Shared By :Mr.S.K.Patel (Lecturer, AITS, Rajkot) Show/Hide Program
#include<iostream>
#include<fstream>
#include<iomanip>
#include<conio.h>
static int recno=0;
static int srno=0;
static int rec=1;
static int reset=0;
using namespace std;
class inventory
{
int record_no;
char tool_name[20];
int quantity;
float cost;
public:
void input();
void displayall();
void modify(int);
};
void inventory::input()
{
record_no=recno+1;
cout << " \n---------------------------";
cout << "\n Record no -> " << record_no;
cout << "\n Enter tool name -> ";
cin >> tool_name;
cout << " Enter quantity -> ";
cin >> quantity;
cout << " Enter cost -> ";
cin >> cost;
cout << "---------------------------" << endl;
recno++;
}
void inventory::modify(int r)
{
record_no=r;
cout << " \n----------------------------";
cout << "\n Record no -> " << r;
cout << "\n Enter tool name -> ";
cin >> tool_name;
cout << " Enter quantity -> ";
cin >> quantity;
cout << " Enter cost -> ";
cin >> cost;
cout << " --------------------------" << endl;
}
void inventory::displayall()
{
srno++;
reset++;
if(reset==1)
{
cout << "\n\n---------------------------------------------" << endl;
cout << setiosflags(ios::left) << setw(10) << "Record no" << setw(15) << "| Tool name" << setw(12) << "| Quantity" << setw(5) << "| Cost";
cout << "\n---------------------------------------------" << endl;
cout << setiosflags(ios::left) << setw(10) << record_no << "| " << setw(13) << tool_name << "| " << setw(10) << quantity << "| " << setw(5) << cost << endl;
}
else
cout << setiosflags(ios::left) << setw(10) << record_no << "| " << setw(13) << tool_name << "| " << setw(10) << quantity << "| " << setw(5) << cost << endl;
}
void main()
{
int ch;
int recpos,location;
inventory tool;
ofstream outtool;
outtool.open("hardware.dat");
fstream mytool;
mytool.open("hardware.dat", ios::ate|ios::in|ios::out|ios::binary);
do
{
cout << "\n-------------------------------";
cout << "\n 1. Input data in file";
cout << "\n 2. Display all data from file";
cout << "\n 3. Update a record in a file";
cout << "\n 0. Exit";
cout << "\n-------------------------------";
cout << "\n Enter you choice -> ";
cin >> ch;
switch(ch)
{
case 1:
tool.input();
mytool.write((char*) &tool, sizeof(tool))<<flush;
break;
case 2:
//srno=0;
reset=0;
mytool.seekg(0);
while(mytool.read((char*) &tool, sizeof(tool)))
tool.displayall();
if(srno==0)
{
cout << "\n\n---------------------------------------------" << endl;
cout << setiosflags(ios::left) << setw(10) << "Record no" << setw(15) << "| Tool name" << setw(12) << "| Quantity" << setw(5) << "| Cost";
cout << "\n---------------------------------------------" << endl;
cout << setiosflags(ios::right) << setw(30) << "| File is empty |" << endl;
cout << resetiosflags(ios::right);
}
cout << "---------------------------------------------" << endl<<endl;
mytool.clear();
break;
case 3:
char ch;
cout << "\n\n Enter the record no. to be modify -> ";
cin >> recpos;
cin.get(ch);
location = (recpos-1) * sizeof(tool);
if(mytool.eof())
mytool.clear();
mytool.seekp(location);
cout << "\n Enter new values for a tool";
tool.modify(recpos);
cin.get(ch);
mytool.write((char*) &tool, sizeof(tool)) << flush;
mytool.seekg(0);
rec=1;
srno=0;
reset=0;
mytool.seekg(0);
while(mytool.read((char*) &tool, sizeof(tool)))
tool.displayall();
cout << "---------------------------------------------" << endl<<endl;
mytool.clear();
break;
case 0:
cout << "\n\n ======================================================";
cout << "\n| By: S.K.Patel |" << endl;
cout << "| Lecturer, MCA Department |" <<endl;
cout << "| Atmiya Institute of Technology & Science, Rajkot.|" << endl;
cout << "| E-mail: patelshaileshk@gmail.com |" << endl;
cout << " ======================================================\n";
getch();
exit(0);
}
}while(ch!=0);
}
Shared By :Dhaval M. Devani(B.H.Gardi - Rajkot) Show/Hide Program
#include<iostream>
#include<fstream>
using namespace std;
void add();
void Modify();
void Displayall();
int location=0;
class tool
{
public:
int r_no;
char t_name[30];
int qty;
int cost;
public:
void input()
{
cout<<endl<<"Enter the Empid :-";
cin>>r_no;
cout<<endl<<"Enter the name :-";
cin>>t_name;
cout<<endl<<"Enter the quantity :-";
cin>>qty;
cout<<endl<<"Enter the cost :-";
cin>>cost;
}
void display()
{
cout<<endl<<"r_no :- "<<r_no;
cout<<endl<<"Name :- "<<t_name;
cout<<endl<<"Quantity:- "<<qty;
cout<<endl<<"Cost :- "<<cost;
}
};
void main()
{
int ch;
do
{
cout<<endl<<"1..Add record :-";
cout<<endl<<"2..Modify record. :-";
cout<<endl<<"3..Display record :-";
cout<<endl<<"4..EXIT::-";
cout<<endl<<"Enter your choice :-";
cin>>ch;
switch(ch)
{
case 1:
add();
break;
case 2:
Modify();
break;
case 3:
Displayall();
break;
case 4:
exit(1);
}
}while(ch!=5);
cin.get();
}
void add()
{
tool e1;
ofstream out("tool.txt",ios::out|ios::binary|ios::app);
e1.input();
out.write((char *)&e1,sizeof(tool));
out.close();
}
void Modify()
{
tool e1;
int flg=0;
fstream in("tool.txt",ios::in |ios::out|ios::ate|ios::binary);
char t_name[30];
cout<<endl<<"Enter the t_name to Update:-";
flushall();
cin>>t_name;
int i=0;
while(!in.eof())
{
in.read((char *)&e1,sizeof(tool));
if(strcmp(e1.t_name ,t_name)==0)
{
e1.input();
in.seekp(i*(sizeof(tool)),ios::beg);
in.write((char *)&e1,sizeof(tool));
flg=1;
break;
}
i++;
}
if(flg==0)
{
cout<<endl<<"NOT FOUND";
}
else
{
cout<<endl<<"RECORD HAS BEEN MODIFIED SUCCESSFULLY";
}
in.close();
}
void Displayall()
{
ifstream in("tool.txt",ios::in|ios::binary);
int count=0;
in.seekg(0,ios::beg);
while(!in.eof())
{
tool e1;
in.read((char *)&e1,sizeof(tool));
e1.display();
}
in.close();
}
Shared By :Your Name Show/Hide Program
Share This Program..
send it gtumca@gmail.com
with your name - college name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
Shared By :Mr.S.K.Patel (Lecturer, AITS, Rajkot) Show/Hide Program
/*Prog.(24) A file contains name and phone numbers. Name contains only one word and names
and telephone numbers are seperated by white spaces. Write a program which will
include a menu driven functionality for the above file
(a) Display 2 column output with names left justified and
phone numbers right justified.
Name Phone
Ram 56789125
Shyam 34565432
Rahim 23456789
Joseph 67213148
(b) Determine the telephone number of a person.
(c) Determine the name if the telephone number is known.
(d) Update the telephone number.
(e) Prefix all the telephone numbers by 2.
------------------------------------------------------
By- S.K.Patel, Lecturer, MCA Dept.
Atmiya Institute of Technolgy and Science, Rajkot.
E-mail: patelshaileshk@gmail.com
Program developed in- Microsoft Visual C++ 6.0
------------------------------------------------------ */
#include<iostream>
#include<fstream>
#include<iomanip>
#include<conio.h>
static int recno=0;
static int srno=0;
static int count=0;
static char teleno[15];
static char nm[15];
static int r=0;
static int s;
static int i=0;
using namespace std;
class directory
{
char name[15];
char phoneno[15];
char tempno[15];
public:
void addcontact(int);
void displayall();
void searchcontact(int);
void updatecontact(int);
void displaydata();
void prefix(int);
};
void directory::addcontact(int p)
{
cout << " \n-----------------------------";
if(p==1)
{
recno=recno+1;
cout << "\n Record no -> " << recno;
}
cout << "\n Enter name -> ";
cin >> name;
cout << " Enter phone -> ";
cin >> phoneno;
cout << "-----------------------------" << endl;
}
void directory::displayall()
{
srno++;
if(srno==1)
{
cout << "\n\n------------------------------------------" << endl;
cout << setiosflags(ios::left) << setw(11) << "| Record no " << setw(15) << "| Name" << "| " << setiosflags(ios::right) << setw(13) << "Phone|" << resetiosflags(ios::right);
cout << "\n------------------------------------------" << endl;
cout << "| " << setiosflags(ios::left) << setw(6) << srno << "| " << setw(13) << name << "| " << setiosflags(ios::right) << setw(12) << phoneno << resetiosflags(ios::right) << "|" << endl;
}
else
cout << "| " << setiosflags(ios::left) << setw(6) << srno << "| " << setw(13) << name << "| " << setiosflags(ios::right) << setw(12) << phoneno << resetiosflags(ios::right) << "|" << endl;
}
void directory :: searchcontact(int a)
{
if(a==3)
{
if(strcmp(teleno,phoneno)==0)
{
displaydata();
count++;
}
}
else
{
if(strcmp(name,nm)==0)
{
displaydata();
count++;
}
}
}
void directory :: displaydata()
{
cout << " \n\n----------------------";
cout << "\n Serial no: " << srno;
cout << "\n Name : " << name;
cout << "\n Tele no : " << phoneno;
cout << " \n----------------------";
srno++;
}
void directory::updatecontact(int x)
{
if(strcmp(name,nm)==0)
{
r=(i)*s;
srno++;
}
i++;
}
void directory::prefix(int x)
{
int k=0;
while(phoneno[k] !='\0')
{
if(k==0)
strcpy(tempno,"2");
tempno[k+1]=phoneno[k];
k++;
}
tempno[k+1]='\0';
k=0;
strcpy(phoneno,"\0");
while(tempno[k] !='\0')
{
phoneno[k]=tempno[k];
k++;
}
phoneno[k]='\0';
}
void main()
{
directory d;
int m,ch;
r=0, s=sizeof(d);
ofstream dfile;
dfile.open("tele_dir.dat",ios::app);
fstream mydir;
mydir.open("tele_dir.dat", ios::ate|ios::in|ios::out|ios::binary);
ifstream mydirup;
mydirup.open("tele_dir.dat", ios::ate|ios::in|ios::binary);
do
{
cout << "\n\n--------------------------------------------------------";
cout << "\n 1. Add contact in directory";
cout << "\n 2. Display 2 column output";
cout << "\n 3. Determine the telephone number of a person";
cout << "\n 4. Determine the name if the telephone number is known";
cout << "\n 5. Update the telephone number";
cout << "\n 6. Prefix all the telephone numbers by 2";
cout << "\n 0. Exit";
cout << "\n--------------------------------------------------------";
cout << "\n Enter you choice -> ";
cin >> ch;
switch(ch)
{
case 1:
d.addcontact(ch);
mydir.write((char*) &d, sizeof(d))<<flush;
break;
case 2:
srno=0;
mydir.seekg(0);
while(mydir.read((char*) &d, sizeof(d)))
d.displayall();
if(srno==0)
{
cout << "\n\n------------------------------------------" << endl;
cout << setiosflags(ios::left) << setw(11) << "| Record no " << setw(15) << "| Name" << "| " << setiosflags(ios::right) << setw(13) << "Phone|" << resetiosflags(ios::right);
cout << "\n------------------------------------------" << endl;
cout << "|" << setiosflags(ios::right) << setw(25) << "File is empty" << setw(16) << "|" << resetiosflags(ios::right) << endl;
}
cout << "------------------------------------------" << endl<<endl;
mydir.clear();
break;
case 3:
case 4:
mydir.seekg(0,ios::end);
m=mydir.tellg();
if(ch==3)
{
cout << "\n Enter telephone number -> ";
cin >> teleno;
}
else
{
cout << "\n Enter name -> ";
cin >> nm;
}
r=count=0;
m=mydir.tellg();
srno=1;
while(r<m)
{
mydir.seekg(r,ios::beg);
mydir.read((char*) &d,sizeof(d));
d.searchcontact(ch);
r=r+s;
}
if(count==0)
cout << "\n ---> Record not found <---";
mydir.clear();
break;
case 5:
cout << "\n Enter NAME to update telephone number -> ";
cin >> nm;
srno=0;
mydir.seekg(0);
mydirup.seekg(0);
r=i=0;
while(mydirup.read((char*) &d, sizeof(d)))
d.updatecontact(i);
if(srno==0)
{
cout << "\n\n------------------------------------------" << endl;
cout << setiosflags(ios::left) << setw(11) << "| Record no " << setw(15) << "| Name" << "| " << setiosflags(ios::right) << setw(13) << "Phone|" << resetiosflags(ios::right);
cout << "\n------------------------------------------" << endl;
cout << "|" << setiosflags(ios::right) << setw(29) << "No Record exist in file" << setw(12) << "|" << resetiosflags(ios::right) << endl;
cout << "------------------------------------------" << endl<<endl;
}
else
{
mydir.seekg(r,ios::beg);
d.addcontact(ch);
mydir.write((char*) &d, sizeof(d));
}
mydir.clear();
mydirup.clear();
break;
case 6:
srno=0;
mydir.seekg(0);
mydirup.seekg(0);
r=i=0;
while(mydirup.read((char*) &d, sizeof(d)))
{
r=(i)*s;
srno++;
i++;
mydir.seekg(r,ios::beg);
d.prefix(ch);
mydir.write((char*) &d, sizeof(d));
}
if(srno==0)
{
cout << "\n\n------------------------------------------" << endl;
cout << setiosflags(ios::left) << setw(11) << "| Record no " << setw(15) << "| Name" << "| " << setiosflags(ios::right) << setw(13) << "Phone|" << resetiosflags(ios::right);
cout << "\n------------------------------------------" << endl;
cout << "|" << setiosflags(ios::right) << setw(29) << "No Record exist in file" << setw(12) << "|" << resetiosflags(ios::right) << endl;
cout << "------------------------------------------" << endl<<endl;
}
mydir.clear();
mydirup.clear();
break;
case 0:
cout << "\n\n ======================================================";
cout << "\n| By: S.K.Patel |" << endl;
cout << "| Lecturer, MCA Department |" <<endl;
cout << "| Atmiya Institute of Technology & Science, Rajkot.|" << endl;
cout << "| E-mail: patelshaileshk@gmail.com |" << endl;
cout << " ======================================================\n";
getch();
exit(0);
}
}while(ch!=0);
}
Shared By :Jimeet Shah(C.U.Shah - Surendranagar) Show/Hide Program
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
#include<iomanip.h>
#include<conio.h>
#include<stdio.h>
class telephone
{
char name[50];
int pn;
public:
void Get_Data()
{
cout<<"Enter Name:-";
cin>>name;
cout<<"Enter Phone No.:-";
cin>>pn;
}
void display()
{
cout<<"\t"<<name<<"\t"<<setw(9)<<pn<<"\n";
}
int Search_No(char na[])
{
if(strcmp(na,name)==0)
{
return 1;
}
else
{
return 0;
}
}
int Search_Name(int na)
{
if(na==pn)
{
return 1;
}
else
{
return 0;
}
}
void update(char na[])
{
cout<<"Enter New Telephone No.:-";
cin>>pn;
strcpy(name,na);
}
void prefix()
{
int nn=pn,temp[8],k=0,j=0,temp1[8];
double x=1;
while(nn!=0)
{
temp[k]=nn%10;
nn/=10;
k++;
}
cout<<"\n";
temp[k]=2;
j=k;
k=0;
for(int p=j;p>=0;p--)
{
temp1[p]=temp[k];
k++;
x=x*10;
}
x=x/10;
int ans=0;
for(p=0;p<=j;p++)
{
ans+=(int)x*temp1[p];
x=x/10;
}
pn=ans;
}
};
void main()
{
telephone t,t1;
ifstream ifile;
ofstream ofile;
clrscr();
int ch,bool;
char n[50],i=1;
ofile.open("telephone.txt",ios::app|ios::out);
ofile.close();
while(1)
{
cout<<"\t\nTelephone Details";
cout<<"\t\n1.Enter Details";
cout<<"\t\n2.Display Details";
cout<<"\t\n3.Find Name";
cout<<"\t\n4.Find No.";
cout<<"\t\n5.Update Phone No.";
cout<<"\t\n6.Prefix 2 In All The No.";
cout<<"\t\n7.Exit";
cout<<"\t\nEnter Choice:-";
cin>>ch;
switch(ch)
{
case 1:
ofile.open("telephone.txt",ios::app|ios::out);
t.Get_Data();
ofile.write((char *)&t,sizeof(t));
ofile.close();
break;
case 2:
ifile.open("telephone.txt",ios::in);
cout<<"\t"<<"Name"<<"\t"<<"Phone No."<<"\n\n";
ifile.seekg(0);
while(ifile.read((char *)&t1,sizeof(t1)))
{
t1.display();
}
ifile.close();
break;
case 3:
ifile.open("telephone.txt",ios::in);
cout<<"\n\tEnter Name to Search Telephone No.:-";
cin>>n;
ifile.seekg(0);
bool=0;
while(ifile.read((char *)&t1,sizeof(t1)))
{
bool=t1.Search_No(n);
if(bool==1)
{
cout<<"\t"<<"Name"<<"\t"<<"Phone No."<<"\n\n";
t1.display();
break;
}
}
if(bool==0)
{
cout<<"\n\tNo Match Found";
}
ifile.close();
break;
case 4:
ifile.open("telephone.txt",ios::in);
int num;
cout<<"\n\tEnter No. to Search Name.:-";
cin>>num;
ifile.seekg(0);
bool=0;
while(ifile.read((char *)&t1,sizeof(t1)))
{
bool=t1.Search_Name(num);
if(bool==1)
{
cout<<"\t"<<"Name"<<"\t"<<"Phone No."<<"\n\n";
t1.display();
break;
}
}
if(bool==0)
{
cout<<"\n\tNo Match Found";
}
ifile.close();
break;
case 5:
ofile.open("telephone.txt",ios::ate);
ifile.open("telephone.txt",ios::in);
cout<<"\n\tEnter Name to Update Telephone no.:-";
cin>>n;
ifile.seekg(0);
bool=0;
i=1;
while(ifile.read((char *)&t1,sizeof(t1)))
{
bool=t1.Search_No(n);
if(bool==1)
{
ofile.seekp((sizeof(t1))*(i-1),ios::beg);
t1.update(n);
ofile.write((char *)&t1,sizeof(t1));
cout<<"\n\tRecord Updated";
break;
}
i++;
}
if(bool==0)
{
cout<<"No Match Found";
}
ifile.close();
ofile.close();
break;
case 6:
ofile.open("telephone.txt",ios::ate);
ifile.open("telephone.txt",ios::in);
i=1;
while(ifile.read((char *)&t1,sizeof(t1)))
{
t1.prefix();
ofile.seekp((sizeof(t1))*(i-1),ios::beg);
ofile.write((char *)&t1,sizeof(t1));
cout<<"\n\t2 Prefixed Successfully";
i++;
}
ofile.close();
ifile.close();
break;
case 7:
exit(0);
default:
cout<<"invalid";
break;
}
}
}
Shared By :Your Name Show/Hide Program
Share This Program..
send it gtumca@gmail.com
with your name - college name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
(25) Write a program that stores and displays the records of the customer...
Shared By :Mr.S.K.Patel (Lecturer, AITS, Rajkot) Show/Hide Program
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
static int ac_no=1;
static int r=0;
static int s;
static int i=0;
static int a_no;
static int tmp_a_no;
static char tmp_a_type[2];
static char tmp_name[20];
static float tmp_old_balance;
static float tmp_new_balance;
static float tmp_last_payment;
static char tmp_date[11];
static int flag=0;
class balance_info
{
int acc_no;
char acc_type[2];
char name[20];
float old_balance;
float new_balance;
float last_payment;
char date[11];
char sign[2];
public:
void open_acc();
void read_balance();
void search4credit(int);
void credit(int);
void withdraw(int);
};
void balance_info::open_acc()
{
cout << "\n\n ---------------------------------------------";
cout << "\n Your Account number is -> " << ac_no;
cout << "\n Enter Accout type [C-current / S-Saving] -> ";
cin >> acc_type;
cout << " Enter Account holder name -> ";
cin >> name;
cout << " Enter Initial balance -> ";
cin >> last_payment;
cout << " Enter date [24/03/2011] -> ";
cin >> date;
cout << " ---------------------------------------------";
acc_no=ac_no;
strcpy(sign,"+");
old_balance=0;
new_balance=last_payment;
ac_no++;
}
void balance_info::read_balance()
{
cout << endl << "| " << setw(4) << acc_no << "| " << setw(6) << acc_type << "| " << setw(14) << name << "|" << setiosflags(ios::right) << setw(8) << old_balance << "|" << setw(8) << new_balance << "|" << setiosflags(ios::right) <<setw(10) << last_payment << "(" << sign << ")|" << setw(10) << date << "|" << resetiosflags(ios::right);
flag=1;
}
void balance_info::search4credit(int x)
{
if(acc_no == a_no)
{
r=(i)*s;
tmp_a_no=acc_no;
strcpy(tmp_a_type,acc_type);
strcpy(tmp_name,name);
tmp_old_balance=old_balance;
tmp_new_balance=new_balance;
tmp_last_payment=last_payment;
strcpy(tmp_date,date);
flag=1;
}
i++;
}
void balance_info::credit(int p)
{
acc_no=tmp_a_no;
strcpy(acc_type,tmp_a_type);
strcpy(name,tmp_name);
old_balance=tmp_new_balance;
new_balance=tmp_new_balance;
strcpy(date,tmp_date);
cout << " Enter amount -> ";
cin >> last_payment;
cout << " Enter date-> ";
cin >> date;
strcpy(sign,"+");
new_balance = new_balance+last_payment;
}
void balance_info::withdraw(int p)
{
acc_no=tmp_a_no;
strcpy(acc_type,tmp_a_type);
strcpy(name,tmp_name);
old_balance=tmp_new_balance;
new_balance=tmp_new_balance;
strcpy(date,tmp_date);
cout << " Enter amount -> ";
cin >> last_payment;
cout << " Enter date-> ";
cin >> date;
strcpy(sign,"-");
new_balance = new_balance-last_payment;
}
void main()
{
balance_info b;
int ch=0;
r=0,s=sizeof(b);
int choice;
ofstream dfile;
dfile.open("balance_info");
fstream inoutfile;
inoutfile.open("balance_info",ios::in|ios::out|ios::binary);
ifstream tempfile;
tempfile.open("balance_info", ios::in|ios::binary);
do
{
cout << "\n *************************************";
cout << "\n 1. Open and initialize the account";
cout << "\n 2. Credit an account";
cout << "\n 3. Withdraw an account";
cout << "\n 4. View all customer account details";
cout << "\n 0. Exit";
cout << "\n *************************************";
cout << "\n Enter you choice -> ";
cin >> choice;
switch(choice)
{
case 1:
b.open_acc();
inoutfile.write((char*) &b, sizeof(b)) << flush;
break;
case 2:
flag=0;
cout << "\n Enter Account number -> ";
cin >> a_no;
inoutfile.clear();
inoutfile.seekg(0);
tempfile.seekg(0);
r=i=0;
while(tempfile.read((char*) &b, sizeof(b)))
b.search4credit(i);
if(flag==1)
{
inoutfile.seekg(r,ios::beg);
b.credit(ch);
inoutfile.write((char*) &b, sizeof(b)) << flush;
cout << " 'Amount credited successfully' - View your account\n";
}
else
{
cout << "\n------------------------------------------------------------------------------\n";
cout << setiosflags(ios::left) << setw(6) << "| A/c No" << setw(10) << "| A/c Type" << setw(16) << "| Name" << "| Old bln" << "| New bln" << "| Last payment" << "| Date |";
cout << "\n------------------------------------------------------------------------------";
cout << endl << "|" << setiosflags(ios::right) << setw(45) << "*** Record not found *** " << setw(32) << "|";
cout << "\n------------------------------------------------------------------------------\n";
}
inoutfile.clear();
tempfile.clear();
break;
case 3:
flag=0;
cout << "\n Enter Account number -> ";
cin >> a_no;
inoutfile.clear();
inoutfile.seekg(0);
tempfile.seekg(0);
r=i=0;
while(tempfile.read((char*) &b, sizeof(b)))
b.search4credit(i);
if(flag==1)
{
inoutfile.seekg(r,ios::beg);
b.withdraw(ch);
inoutfile.write((char*) &b, sizeof(b)) << flush;
cout << " 'Amount debited successfully' - View your account\n";
}
else
{
cout << "\n-----------------------------------------------------------------------------\n";
cout << setiosflags(ios::left) << setw(6) << "| A/c No" << setw(10) << "| A/c Type" << setw(16) << "| Name" << "| Old bln" << "| New bln" << "| Last payment" << "| Date |";
cout << "\n-----------------------------------------------------------------------------";
cout << endl << "|" << setiosflags(ios::right) << setw(45) << "*** Record not found *** " << setw(31) << "|";
cout << "\n-----------------------------------------------------------------------------\n";
}
inoutfile.clear();
tempfile.clear();
break;
case 4:
flag=0;
inoutfile.seekg(0);
cout << "\n------------------------------------------------------------------------------\n";
cout << setiosflags(ios::left) << setw(6) << "| A/c No" << setw(10) << "| A/c Type" << setw(16) << "| Name" << "| Old bln" << "| New bln" << "| Last payment" << "| Date |";
cout << "\n------------------------------------------------------------------------------";
while(inoutfile.read((char*) &b, sizeof(b)))
b.read_balance();
if(flag==0)
{
cout << endl << "|" << setiosflags(ios::right) << setw(45) << "*** File is empty *** " << setw(32) << "|";
}
cout << "\n------------------------------------------------------------------------------\n";
inoutfile.clear();
break;
case 0:
break;
}
}while(choice!=0);
cout << "\n\n ========================================================";
cout << "\n | By: Mr. S.K.Patel |" << endl;
cout << " | Lecturer, MCA Department |" <<endl;
cout << " | Atmiya Institute of Technology & Science, Rajkot.|" << endl;
cout << " | E-mail: patelshaileshk@gmail.com |" << endl;
cout << " ========================================================\n";
}
(26) Write a program that swaps each character pair in a text file...
Shared By :Mr.S.K.Patel (Lecturer, AITS, Rajkot) Show/Hide Program
/*
(26) Write a program that swaps each character pair in a text file. For example, if the
file contains "1234", then after the program in run, the file will contain "2143".
*/
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<conio.h>
using namespace std;
class swapping
{
public:
char string[20];
char temp[20];
void insert(int j)
{
cout << "\n Enter string" << j+1 << " -> " ;
cin >> string;
}
};
void main()
{
int n;
ofstream outfile;
outfile.open("25_swap",ios::binary | ios::app);
swapping obj;
cout << "\n How many words you want to add? -> ";
cin >> n;
for(int j=0; j<n; j++)
{
obj.insert(j);
outfile.write((char*)&obj, sizeof(obj)) << flush;
}
fstream inoutfile;
inoutfile.open("25_swap",ios::binary | ios::in | ios::out | ios::ate);
fstream ofile;
ofile.open("25_swap",ios::binary | ios::in | ios::out | ios::ate);
inoutfile.seekg(0,ios::beg);
int len=0;
int s=sizeof(obj);
int p=0;
cout << "\n\n ----------------------------------------";
cout << "\n Before reading |" << setw(16) << " After reading";
cout << "\n ----------------------------------------" << endl;
while(inoutfile.read((char *) &obj, sizeof(obj)))
{
int len=0;
while(obj.string[len] != '\0')
len=len+1;
int index=0;
while(obj.string[index] != '\0')
{
if(index % 2==0)
{
obj.temp[index+1] = obj.string[index];
if(index==len-1)
obj.temp[len-1]=obj.string[len-1];
}
else
{
obj.temp[index-1] = obj.string[index];
}
index++;
}
obj.temp[index]='\0';
int r=(p)*s;
ofile.seekp(r,ios::beg);
ofile.write((char*) &obj.temp, sizeof(obj.temp))<<flush;
p=p+1;
cout << setw(5) << "" << setiosflags(ios::left) << setw(13) << obj.string << setw(10) << " ->" << setiosflags(ios::left) << setw(8)<< obj.temp << endl;
}
cout << " ---------------------------------------\n\n\n";
cout << "\n\n ======================================================";
cout << "\n| By: S.K.Patel |" << endl;
cout << "| Lecturer, MCA Department |" <<endl;
cout << "| Atmiya Institute of Technology & Science, Rajkot.|" << endl;
cout << "| E-mail: patelshaileshk@gmail.com |" << endl;
cout << " ======================================================\n";
getch();
}
Shared By :Milan K. Vachhani(Lecturer - B.H.GARDI - RAJKOT) Show/Hide Program
//Write a program that swaps each character pair in a text file...
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
void main()
{
char ch1,ch2;
fstream fptr("test.txt",ios::in|ios::out);
int r=0;
while(!fptr.eof())
{
fptr.get(ch1);
fptr.get(ch2);
if(fptr.fail())
break;
r=fptr.tellg();
fptr.seekp(r-2,ios::beg);
fptr.put(ch2);
fptr.put(ch1);
fptr.seekg(r,ios::beg);
}
fptr.close();
}
Shared By :Dhaval M.Devani(B.H.GARDI - RAJKOT) Show/Hide Program
/* Write a program that swaps each character pair in a text file. For example, if the file contains "1234",
then after the program in run, the file will contain "2143".*/
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
#include<iomanip>
int main()
{
char ch,c[80];
char ch1,ch2;
ofstream entryfile("fewline.dat",ios::ate);
while (true)
{
cin.get(ch);
if (ch == '$') break;
entryfile << ch;
}
entryfile.close();
ifstream displayfile("fewline.dat");
while (!displayfile.eof())
{
displayfile.getline(c,80,'\n');
for(int i=0;i<strlen(c);i++)
{
if(i%2==0 && i<strlen(c))
cout<<c[i+1];
else
cout<<c[i-1];
}
cout<<endl;
}
displayfile.close();
flushall();
cin.get();
return 0;
}