Hi
I have been given a assignment of making a "inventory control program of a book store in c++" i have done all the parts except for two
1. I have to delete a record from the file (i am using fstream and saving the file i need the piece of code to delete the particular record).
2 . i have to sell a book but when ever i subtract something from the record a new record gets created and the problem becomes harder.
this the program
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class inventory
{
char b_name[15];
char p_name[15];
float isbn;
int pur;
float p_cost;
float s_cost;
int hand;
public:
void getdata()
{
cout<<"ENTER ISBN :";cin>>isbn;
cout<<"ENTER BOOK :";cin>>b_name;
cout<<"ENTER PUBLSHER :";cin>>p_name;
cout<<"ENTER PURCHASE COST :";cin>>p_cost;
cout<<"ENTER SALE COST :";cin>>s_cost;
}
void inhand()
{
cout<<"ENTER QUANTITY PURCHASED :";cin>>pur;
hand=pur;
}
void sale()
{
hand=hand-1;
}
void show()
{
cout<<"\nISBN :"<<isbn<<"\nBOOK :"<<b_name<<"\nPUBLISHER :"<<p_name;
cout<<"\nQUANTITY PURCHASE :"<<pur<<"\nQUANTITY IN HAND :"<<hand;
cout<<"\nPURCHASE COST :"<<p_cost<<"\nSALE COST :"<<s_cost;
}
};
void main()
{
clrscr();
char c;
inventory inv;
fstream file;
file.open("inv_dat.txt",ios::ate);
cout<<"\n\n\n\t\t\"MENU\"";
cout<<"\nPRESS \"1\" to print all the records.";
cout<<"\nPRESS \"2\" to delete a record.";
cout<<"\nPRESS \"3\" to update a record.";
cout<<"\nPRESS \"4\" to enter a new record.";
cout<<"\nPRESS \"5\" to sell a book.";
cout<<"\nPRESS \"5\" to calculate profit in this month.";
cout<<"\nPRESS \"0\" for EXIT";
char choice='0';
cout<<"\n\n\"ENTER CHOICE\" :";cin>>choice;
switch (choice)
{
case'1':
{
file.open("inv_dat.txt",ios::in);
file.seekg(0);
file.read((char*)&inv,sizeof(inv));
do
{
inv.show();
cout<<endl;
file.read((char*)&inv,sizeof(inv));
}
while(!file.eof());
getch();
};break;
case '2':
{
file.open("inv_dat.txt",ios::in|ios::out|ios::ate);
file.seekg(0,ios::end);
int n,pos;
cout<<"The records present are :"<<file.tellg()/sizeof(inv);
cout<<"\nEnter ISBN no. of the record you want to delete :";
cin>>n;
pos=(n-1) *sizeof(inv);
file.seekp(pos);
file.write((char *)&inv,sizeof(inv));
getch();
};break;
case '3':
{
file.open("inv_dat.txt",ios::in|ios::out|ios::ate);
int n,pos;
cout<<"Enter ISBN no. of the record you want to update";
cin>>n;
pos=(n-1) *sizeof(inv);
file.seekp(pos);
cout<<"\n\" ENTER NEW DATA \"\n";
inv.getdata();
inv.inhand();
file.write((char *)&inv,sizeof(inv));
file<<flush;
getch();
};break;
case '4':
{
file.open("inv_dat.txt",ios::app);
do
{
inv.getdata();
inv.inhand();
file.write((char*)&inv,sizeof(inv));
cout<<"\n\nWnat to enter another object(y/n):" ;cin>>c;
}
while (c=='y');
};break;
case '5':
{
file.open("inv_dat.txt",ios::in|ios::out);
int n,pos;
cout<<"\n Enter the ISBN no of the Book you want to sell :";
cin>>n;
pos=(n-1) *sizeof(inv);
file.seekp(pos);
file.read((char*)&inv,sizeof(inv));
inv.sale();
file.write((char*)&inv,sizeof(inv));
file<<flush;
getch();
};break;
default:getch();
}
}