CPP Program for mantaining BOOK Details

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
class book
{
      int bno;
      char bname[30];
      int price;
   public:
   void input()
   {  cout<<"\nEnter Book Number : ";
      cin>>bno;
      cout<<"\nEnter Book Name   : ";
      gets(bname);
      cout<<"\nEnter the Price   : ";
      cin>>price;
   }
   void display()
   {
      cout<<"\nBook Number : "<<bno;
      cout<<"\nBook Name   : "<<bname;
      cout<<"\nBook Price  : Rs"<<price;
   }
   int getbno()
   {
      return bno;
   }
};
void main()
{
   book b;
   int n, i, j, choice;
   fstream file;
   char ch;
   do
   {
      clrscr();
      cout<<"\n----MENU----";
      cout<<"\n1.Enter Details of Book\n2.Display the Details"
   <<"\n3.Search for Book\n4.Exit";
      cout<<"\nEnter your choice : ";
      cin>>choice;
      switch(choice)
      {
  case 1: file.open("bk.dat",ios::out|ios::binary);
   cout<<"\nEnter Number of Records to be Stored : ";
   cin>>n;
   for(i=0;i<n;i++)
   {
       b.input();
       file.write((char *)&b,sizeof(b));
   }
   file.close();
   break;
  case 2: file.open("bk.dat",ios::in|ios::binary);
   cout<<"\nThe Details are ";
   cout<<"\n---------------";
   do
   {
      file.read((char *)&b,sizeof(b));
      if(!file.eof()) b.display();
      cout<<"\n";
   }while(file);
   file.close();
   break;

  case 3: int bn,flag=0;
   cout<<"\nEnter the Book Number to be Searched : ";
   cin>>bn;
   file.open("bk.dat",ios::in|ios::binary);
   while(file)
   {
      file.read((char *)&b,sizeof(b));
      if(bn==b.getbno())
      {
         b.display();
         flag=1;
         break;
      }
   }
   if(flag==0)
      cout<<"\nNo Recors Found";
   file.close();
   break;
  case 4: exit(0);
 default: cout<<"\nWrong choice!!!";
 }
 cout<<"\nPress any key to continue";
 getch();
       }while(1);
}

1 comment: