I am writing a Database for a book store in a DOS console program, and I am having trouble with the Delete book function. I can successfully search the file for the book I am trying to remove and pass the location in the file to the Delete function. but when it tries to delete it, my program bugs and the screen starts scrolling and it corrupts the .dat file, and I have to delete it.
The code I am working with is as follows (this will not compile, I have copied and pasted the code of interest) :
struct items
{
char isbn[30];
char title[30];
char author[30];
char publisher[30];
char date[20];
int onHand;
double retail;
double wholesale;
};
void deletebook()
{
char title[50];
int choice=0;
char response ='n';
//book entry prompt
cin.ignore();
cout<<"\n Enter the title as it appears in file: ";
cin.getline(title, 50);
iFile.open("database.dat",ios::in);
if(iFile.fail())
{
iFile.open("database.dat", ios::out);
iFile.close();
iFile.clear();
}
else
{
while(!iFile.eof())
{
for(int fromFile=0; fromFile<50; fromFile++)
{
int IfromFile=fromFile;
iFile.seekg(IfromFile*sizeof(book),ios...
iFile.read(reinterpret_cast<char*>(&bo... sizeof(book));
book[fromFile];
}
}
iFile.close();
iFile.clear();
}
for (int row=0; row <50; row++)
{
if (strstr(book[row].title, title))
{
cout<<"\nIs......"
<<book[row].title
<<"\n\n";
cout<<"This your book?\n";
cout<<"1. yes\n";
cout<<"2. no\n\n";
cout<<"Which is it?";
cin>> choice;
if (choice ==1)
{
cout<<"Delete this Book(Y/N): ";
cin>>response;
while(response !='y'&&response !='Y'&&response != 'n'&& response !='N')
{
cout<<"\nPlease enter \'Y\' for Yes or \'N\' for No.\n\n";
cout<<"would you like to delete this entire record?(Y/N): ";
cin>> response;
}
if (response=='Y'||response=='y')
{
removeBook(row);
}
//Removal function
void removeBook(int subscript)
{
int Isubscript=subscript;
iFile.open("database.dat",ios::out);
iFile.seekp(Isubscript*sizeof(book),io...
book[Isubscript].title[0]='\0';
iFile.write(reinterpret_cast<char*>(&b...
iFile.close();
iFile.clear();
}
Tags: