STOCK with public and private members? Ans. #include<iostream.h> #include<conio.h> #include<stdio.h> class STOCK { 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() { cout<<"Enter item code:"; cin>>Icode; cout<<"Enter item name:"; gets(Item); cout<<"Enter price of each item:"; cin>>price; cout<<"Enter quantity of item in stock:"; cin>>qty; finddisc(); } void showall() { cout<<"Code:"<<Icode<<"\n Name:"<<Item<<"\n Price:"<<price<<"\n Quantity:"<<qty<<"\n Discount percentage:"<<discount; } }; void main() { STOCK s1; cout<<"Enter the following details:"; s1.buy(); cout<<"All the details are:"; s1.showall(); getch(); } Q2. Write a program to perform search on the basis of the roll no. entered by the user in the binary file using structures? Ans. #include<iostream.h> #include<conio.h> #include<fstream.h> struct info { char name[10]; int rollno; int Class; }s[10]; void main() { clrscr();int n,ch; ofstream filout; filout.open("Marks.dat",ios::out|ios::binary); cout<<"Enter the no. of details to be entered(<10):"; cin>>n; for(int i=0;i<n;i++) { cout<<"\nEnter students name:"; cin>>s[n].name;cout<<endl; cout<<"\nEnter students class:"; cin>>s[n].Class;cout<<endl; cout<<"\nEnter students roll no.:"; cin>>s[n].rollno;cout<<endl; } filout.close(); ifstream fin; fin.open("Marks.dat",ios::in); cout<<"Enter the roll no. to be searched:"; cin>>ch; while(!fin.eof()) { for(int i=0;i<n;i++) { fin.read((char*)&s[n],sizeof(s[n])); if(s[n].rollno==ch) { cout<<"Name of the student is:"<<s[n].name<<endl; cout<<"Class of the student is:"<<s[n].Class<<endl; } else cout<<"Not found!!"; } } getch(); } Q3. Write a program in file handling to append the information entered by the user in the binary file using either class or structure? Ans. #include<iostream.h> #include<conio.h> #include<fstream.h> class Stu { char name[20]; int rollno; int Class; public: void read() { cout<<"Enter name:"; cin>>name; cout<<"Enter roll no:"; cin>>rollno; cout<<"Enter class:"; cin>>Class; } void display() { cout<<"Name of the student is "<<name<<" of Class"<<Class<<"having roll no:"<<rollno<<"\n"; } int roll() { return rollno; } } s[10]; void main() { clrscr(); int n; char ans; fstream fin; fin.open("stu.dat",ios::out|ios::binary); cout<<"Enter the no of details to be entered(<10):"; cin>>n; for(int i=0;i<n;i++) { s[i].read(); fin.write((char*)&s[i],sizeof(s[i])); s[i].display(); } fin.close(); ofstream fo; fo.open("stu.dat",ios::app|ios::binary); cout<<"Want to enter more details:\t"; cin>>ans; while(ans=='y') { n+=1; s[n].read(); fo.write((char*)&s[n],sizeof(s[n])); cout<<"Record added..\n"; cout<<"want to enter more" ; cin>>ans; } fo.close(); getch(); } Q4. Write a program to perform insertion in a binary file? Ans. #include<iostream.h> #include<conio.h> #include<fstream.h> #include<stdio.h> class stu { char name[20]; int Class; int rollno; public: void getdata() { cout<<"Enter name:"; cin>>name; cout<<"Enter roll no:"; cin>>rollno; cout<<"Enter class:"; cin>>Class; } void display() { cout<<"Name of the student is "<<name<<" is of class"<<Class<<" and roll no "<<rollno; } int roll() { return rollno; } }s1,s2,s3; void main() { clrscr(); fstream fin; fin.open("marks.dat",ios::out|ios::binary); cout<<"Enter the details of student:\n"; s1.getdata(); fin.write((char*)&s1,sizeof(s1)); s1.display(); fin.close(); char last='y'; fin.open("marks.dat",ios::in|ios::binary); ofstream fo("temp.dat",ios::out|ios::binary); cout<<"\nEnter details of student to be entered:\n"; s2.getdata(); while(fin.eof()) { fin.read((char*)&s1,sizeof(s1)); if(s2.roll()<=s1.roll()) { fo.write((char*)&s3,sizeof(s3)); last='n'; break; } else fo.write((char*)&s1,sizeof(s1)); } if(last=='y') fo.write((char*)&s2,sizeof(s2)); else { while(fin.eof()) { fin.read((char*)&s1,sizeof(s1)); fo.write((char*)&s1,sizeof(s1)); } } fin.close(); fo.close(); remove("stu.dat"); rename("temp.dat","stu.dat"); fin.open("stu.dat",ios::in|ios::binary); cout<<"File now contains:\n"; while(!fin.eof()) { fin.read((char*)&s1,sizeof(s1)); if(fin.eof()) {break;} s1.display(); } fin.close(); getch(); } Q5. Write a program to perform deletion in file? Ans. #include<iostream.h> #include<conio.h> #include<fstream.h> #include<stdio.h> class stu { char name[20]; int Class; int rollno; public: void getdata() { cout<<"Enter name:"; cin>>name; cout<<"Enter roll no:"; cin>>rollno; cout<<"Enter class:"; cin>>Class; } void display() { cout<<"Name of the student is "<<name<<" is of class"<<Class<<" and roll no "<<rollno<<"\n"; } int roll() { return rollno; } }s[10],s11; void main() { clrscr(); int n,rno; char found='f',confirm='n'; fstream fin; fin.open("stu.dat",ios::out|ios::binary); cout<<"Enter the no. of student whose entry is to be done:\n"; cin>>n; for(int i=0;i<n;i++) { s[i].getdata(); fin.write((char*)&s[i],sizeof(s[i])); s[i].display(); } fin.close(); fin.open("stu.dat",ios::in|ios::binary); ofstream fo("temp.dat",ios::out|ios::binary); cout<<"\n Enter roll no. to be deleted:"; cin>>rno; while(!fin.eof()) { for(int j=0;j<n;j++) { fin.read((char*)&s[j],sizeof(s[j])); if(s[j].roll()==rno) { s[j].display(); found='t'; cout<<"Are you sure,you wanna delete.."; cin>>confirm; if(confirm=='n') fo.write((char*)&s[j],sizeof(s[j])); } else fo.write((char*)&s[j],sizeof(s[j])); } } if(found=='f') cout<<"Record not found:\n"; fin.close(); fo.close(); remove("stu.dat"); rename("temp.dat","stu.dat"); fin.open("stu.dat",ios::in|ios::binary); cout<<"now the file contains:\n"; while(!fin.eof()) { fin.read((char*)&s11,sizeof(s11)); if(fin.eof()) {break;} s11.display(); } fin.close(); getch(); }