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();
}
}

please don't name your threads like this anymore.

Urgent::need help is just plain rude. It implies your thread is more important than others, when this forum prioritizes help requests on a first come, first-served basis.

On the 'new record' issue, everywhere else you use the ISBN as an index into the file. Yet when you write a new record, you have opened the file in 'append' mode so the write happens at the end of the file. Unless you add records in ISBN order, this won't work.

Speaking of ISBN, that's some number assigned by the publisher, isn't it? For file positions you probably want a number that is generally in the range 0..n so you don't have file waste. If ISBN's are like '17750294' you are going to have a massive file with a ton of dead space in it if you use the ISBN as a record number.

Alternatives would be to read the entire file to find the entered ISBN, or to maintain an array in RAM of the ISBN's in the file with their corresponding file position or record index. You would read the file once at startup to build this in-ram list and then maintain it as you go. OR you could have a second file with ISBN's in it that might be faster to read than the entire book file. Getting more sophisticated, you could investigate an indexing method like BTrees (look it up in Google) and build a 'sorted' index file.

Since the records are in this specific order, though, a 'delete' could be as simple as setting the quantity on hand to 0 or you could write nulls/spaces into the record, or if the record needs to be removed entirely, you might have to COPY the file over to a new file, minus this record, and then delete the original and rename the copy.

you might have to COPY the file over to a new file, minus this record, and then delete the original and rename the copy.

extremly wise words. i cant remember how many times i have told people the same advice! this is better than setting zeros and its VERY EASY to implement. got the reconrd number you want to delete and use a loop to copy records with an IF statement to trap the record number to delete (which doesnt get copied)

bit of sample code:

for(int i = 0; i < number_of_records; i++)
{
    if(i == record_number_i_want_to_delete)
        continue; // this skips the rest of the loop and effectively goes back to the top
    // copy record[i] or whatever...
}
This article has been dead for over six months. Start a new discussion instead.