Program to store the details of mobile | C++ Programming

Tuesday, March 03, 2020

//program to store the details of mobile
#include<fstream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
class MOBILE
{ private:
  int ModelNo;
  char Brand[20];
  int Price;
 public:
 int get_modelno()
  { return(ModelNo);
  }
  void enter();
  void show();
};
void MOBILE::enter()
{ cout<<"\n Enter the details";
  cout<<"\n\n Enter the model number:";
  cin>>ModelNo;
  cout<<"\n Enter the brand:";
  gets(Brand);
  cout<<"\n Enter the price:";
  cin>>Price;
}
void MOBILE::show()
{ cout<<"\n Model No.: "<<ModelNo;
  cout<<"\n Brand: "<<Brand;
  cout<<"\n Price: "<<Price;
}
void main()
{ clrscr();
  MOBILE mob;
  char p;
  ofstream ofile;
  ofile.open("MOBILE.DAT",ios::app|ios::binary);
  if(!ofile)
  { cout<<"\n File can not open";
exit(1);
  }
  do
  {
   mob.enter();
   ofile.write((char*)&mob,sizeof(mob));
   cout<<"\n Do you want to continue:";
   cin>>p;
  }while(p=='y'||p=='Y');
  ofile.close();
  ofile.open("TEMP.DAT",ios::out|ios::binary);
  ifstream ifile;
  ifile.open("MOBILE.DAT",ios::in|ios::binary);
  if(!ifile || !ofile)
  { cout<<"\n File can not read";
    exit(1);
  }
  int flag=0,r;
  cout<<"\n Enter the model number you want to delete:";
  cin>>r;
  while(ifile.read((char*)&mob,sizeof(mob)))
  { if(mob.get_modelno()==r)
    { flag=1;
    }
    else
     ofile.write((char *)&mob,sizeof(mob));
  }

if(flag==0)
cout<<"\n Unsuccessful search";
ofile.close();
ifile.close();
remove("MOBILE.DAT");
rename("TEMP.DAT","MOBILE.DAT");
ifile.open("MOBILE.DAT",ios::in|ios::binary);
cout<<"\n Records After Deletion: ";
  while(ifile.read((char*)&mob,sizeof(mob)))
  { mob.show();
    getch();
  }
 ifile.close();
 getch();
}

You Might Also Like

0 Comments

Like us on Facebook

Flickr Images

Flickr Short Films