Sunteți pe pagina 1din 21

PROGRAM – 1

Define a class stock in C++ with the following description:

Private members:

 ICode of type integer


 Item of type string
 Price of float type
 Qty of type integer
 Discount of type float
 A member function FindDisc() to calculate discount

Public members:

 A function buy() to allow user to enter values for ICode, item, price, qty and call function
finddisc() to calculate discount.
 A function show all() to allow user to view the content of all the data members.

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#include<stdio.h>

class stock {public:

int Icode;

char Item[20];

float Price;

int Qty;

float discount;

void FindDisc()

{ if(Qty<=50)

discount=0;

else if(Qty<=100)

discount=5;

else

discount=10 }

public: void buy();


void showall();

}x;

void stock::buy()

{cout<<"Enter item code: ";

cin>>x.Icode;

cout<<"Enter Item Name: ";

gets(x.Item);

cout<<"Enter Item Price: ";

cin>>x.Price;

cout<<"Enter Quantity: ";

cin>>x.Qty;

FindDisc(); n }

void stock::showall()

{cout<<" Code "<<x.Icode<<" NAME "<<x.Item<<" Price "<<x.Price<<" Quantity "<<x.Qty<<" Discount
"<<x.discount; }

void main()

{clrscr();

stock y;

y.buy();

y.showall();

getch(); }
PROGRAM - 2

Write a program to perform search on the basis of the roll_no entered by the user in the
binary file using structures.
#include<iostream.h>

#include<fstream.h>

#include<conio.h>

struct stu

{ int rollno;

char name[20];

int clas;

} s1;

void main()

clrscr();

int rn; char found='n';

ifstream fin("stu.dat", ios::in);

cout<<"Enter roll no. to be searched for:";

cin>>rn;

while(!fin.eof())

fin.read((char*)&s1,sizeof(s1));

if (s1.rollno==rn)

{ cout<<"roll_no:"<<s1.rollno<<"\n";

cout<<"name:"<<s1.name<<"\n";

cout<<"class:"<<s1.clas;

found=='y';

break;

if(found=='n')
cout<<"Rollno not found in file!!"<<endl;

fin.close();

getch();

}
PROGRAM - 3

Write a program in file handling to append the information entered by the user in the binary
file using either classes of structures.
#include<iostream.h>

#include<fstream.h>

#include<conio.h>

class stu{

int rollno;

char name[20];

int clas;

public:

void getdata()

cout<<"Rollno:";

cin>>rollno;

cout<<"Name:";

cin>>name;

cout<<"Class:";

cin>>clas;

void putdata()

cout<<"roll_no:"<<rollno<<"\n";

cout<<"name:"<<name<<"\n";

cout<<"class:"<<clas;

int getrno()

{ return rollno;

}s1;
void main()

{ clrscr();

ofstream fo("stu.dat",ios::app|ios::binary);

char ans='y';

while(ans=='y')

{ s1.getdata();

fo.write((char*)&s1,sizeof(s1));

cout<"Record added to file.\n";

cout<<"Want to enter more records?(y/n)..";

cin>>ans;

fo.close();

getch();

}
PROGRAM – 4

Write a program to insert a new record in binary file.


#include<iostream.h>

#include<fstream.h>

#include<stdio.h>

#include<conio.h>

class stu{

int rollno;

char name[20];

int clas;

public:

void getdata()

{ cout<<"rollno:";

cin>>rollno;

cout<<"name:";

cin>>name;

cout<<"class:";

cin>>clas;

void putdata()

{ cout<<"Rollno:"<<rollno<<"\tName:"<<name<<"\tclass:"<<clas<<"\n";

int getrno()

{ return rollno;

}s1,stud;

void main()

{ clrscr();

ifstream fi("stu.dat",ios::in|ios::binary);

ofstream fo("temp.dat",ios::out|ios::binary);
char last='y';

cout<<"Enter details of student whose record is to be inserted\n";

s1.getdata();

while(!fi.eof() )

{ fi.read((char*) &stud, sizeof(stud));

if(s1.getrno()<=stud.getrno() )

{ fo.write((char*)&s1, sizeof(s1));

last='n';

break;

else

fo.write((char*)&stud, sizeof(stud));

if (last=='y')

fo.write( (char*)&s1, sizeof(s1));

else if(!fi.eof())

{ while(!fi.eof())

{ fi.read((char*)&stud, sizeof(stud));

fo.write((char*)&stud, sizeof(stud));

fi.close();

fo.close();

remove("stu.dat");

rename("temp.dat","stu.dat");

fi.open("stu.dat",ios::in);

cout<<"File now contains\n";

while(!fi.eof())

{ fi.read( (char*) &stud, sizeof(stud));

if(fi.eof() ) break;
stud.putdata();

fi.close();

getch();

}
PROGRAM – 5

Write a program to perform deletion in binary file.


#include<iostream.h>

#include<fstream.h>

#include<stdio.h>

#include<conio.h>

class stu{

int rollno;

char name[20];

int clas;

public:

void getdata()

cout<<"Rollno:";

cin>>rollno;

cout<<"Name:";

cin>>name;

cout<<"Class:";

cin>>clas;

void putdata()

cout<<"Rollno:"<<rollno<<"\tName:"<<name<<endl;

int getrno()

return rollno;

s1,stud;
void main()

{clrscr();

ifstream fio("stu.dat",ios::in);

ofstream file("temp.dat",ios::out);

int rno;

char found='f',confirm='n';

cout<<"Enter rollno of student whose record is to be deleted\n";

cin>>rno;

while(!fio.eof())

fio.read((char*)&s1,sizeof(s1));

if(s1.getrno()==rno)

s1.putdata();

found='t';

cout<<"Are you sure, you want to delete this record?(y/n)..";

cin>>confirm;

if(confirm=='n')

file.write((char*)&s1,sizeof(s1));

else

file.write((char*)&s1,sizeof(s1));

if(found=='f')

cout<<"Record not found!!\n";

fio.close();

file.close();

remove("stu.dat");

rename("temp.dat","stu.dat");

fio.open("stu.dat",ios::in);
cout<<"Now the file contains\n";

while(!fio.eof())

fio.read( (char*)&stud,sizeof(stud));

if(fio.eof() )break;

stud.putdata();

fio.close();

getch();

}
\
PROGRAM - 6

Write a program to modify data in binary file.


#include<iostream.h>

#include<conio.h>

#include<fstream.h>

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

class Stu

{ int rollno;

char name[20];

int clas;

public:

void read()

{cout<<"Enter roll no, name , ClasS ";

cin>>rollno>>name>>clas;

void display()

{cout<<rollno<<" "<<name<<" "<<clas;

int getrollno()

return (rollno);

void modify();

}S1,stud;

void Stu::modify()

char nm[20];

int clas1;
cout<<"Enter new name:"<<"\n";

cin>>nm;

cout<<"Enter new class:"<<"\n";

cin>>clas1;

strcpy(name,nm);

clas=clas1;

void main()

clrscr();

int rno,pos,found='f';

fstream fio("stu.dat",ios::in|ios::out|ios::binary);

cout<<"Enter the roll no. whose record is to be modified ";

cin>>rno;

while(!fio.eof())

{pos=fio.tellg();

fio.read((char*)&S1,sizeof(S1));

if(S1.getrollno()==rno)

S1.modify();

fio.seekg(pos);

fio.write((char*)&S1,sizeof(S1));

found='t';

break;

}}

if(found=='f')

cout<<"record not found!!!";

else

fio.seekg(0);

cout<<"Now the file contains:\n";


while(!fio.eof())

{fio.read((char*)&stud,sizeof(stud));

S1.display();

fio.close();

getch();

S-ar putea să vă placă și