Alright, now this is what i have. I have used the approach of bubble sorting then binary searching. what i want to do now is to search a particular BOOK from someone without using the bubble sorting or binary searching method and instead search all the records one at a time, from start till the end. i think this will make the coding a lot shorter than it is now
#include<fstream>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
#include<iomanip>
#include<iostream>
using namespace std;
class BOOK
{
public:
char ID[6];
char Content[50];
char AUTHOR[20];
char Book[20];
void create_book() {
cout<<"\nNEW BOOK ENTRY...\n";
cout<<"\nEnter The BOOK ID.";
gets(ID);
cout<<"\n\nEnter BOOK NAME ";
gets(Content);
cout<<"\n\nEnter The AUTHOR's Name ";
gets(AUTHOR);
cout<<"\n\nEnter The Source Book ";
gets(Book);
cout<<"\n\n\nBOOK Created..";
getch();
}
void report()
{cout<<ID<<setw(10)<<Content<<setw(30)<<AUTHOR<<setw(20)<<Book<<endl;}
void Copy(BOOK hd){ strcpy(ID,(hd.ID)); strcpy(AUTHOR,(hd.AUTHOR)); strcpy(Book,(hd.Book));
strcpy(Content,(hd.Content));
}
};
void SwapBOOK(BOOK &hd1, BOOK &hd2){
BOOK Temp;
Temp.Copy(hd1); hd1.Copy(hd2); hd2.Copy(Temp); }
fstream fp; ofstream ofp; BOOK hd[1000]; int countBOOK=0;
void ReportAll(){ system("cls");
for(int i=0;i<countbook;i++){>
hd[i].report();
}
getche();
}
void writeAllBOOK()
{
char ch;
ofp.open("BOOK.dat",ios::trunc); system("cls");
for(int i=0; i < countBOOK;i++){
ofp.write((char*)&(hd[i]),sizeof(BOOK));
}
ofp.flush(); ofp.close();}
BOOK temp;
void LoadAllBOOK()
{
system("cls");
fp.open("BOOK.dat",ios::in); if(!fp) {
cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
getch(); countBOOK =0;
return; }
int i=0;
while(fp.read((char*)&temp,sizeof(BOOK))) {
hd[i++].Copy( temp); }
countBOOK=i; fp.close(); getch();
}
void SortByAUTHOR() {
for(int i=0; i< countBOOK-1; i++){
for(int j=0; j <countbook-1;> if(strcmp(hd[j].AUTHOR,hd[j+1].AUTHOR)>0)
SwapBOOK(hd[j],hd[j+1]); }
}
}
void SearchBetweenIndex(int IndexA, int IndexB, char* AUTHOR)
{
if(IndexA > IndexB){
int temp;
temp = IndexA;
IndexA = IndexB;
IndexB= temp;
}
for(int i = IndexA; i <= IndexB;i++){
if(strcmp(hd[i].AUTHOR,AUTHOR)==0)
hd[i].report();
}
getch();
}
void ListByAUTHOR(char* AUTHOR){ int PreviousIndex = 0;
int StartIndex =0 , EndIndex = countBOOK-1;
int i=2;
while(1==1){
int CurrentIndex=(EndIndex+StartIndex)/2; if(strcmp(hd[CurrentIndex].AUTHOR, AUTHOR) > 0){
PreviousIndex = EndIndex;
EndIndex = CurrentIndex;
}else if(strcmp(hd[CurrentIndex].AUTHOR, AUTHOR) < 0){
PreviousIndex = StartIndex;
StartIndex = CurrentIndex;
}else
{
SearchBetweenIndex(StartIndex, EndIndex, AUTHOR);
break;
}
if(CurrentIndex == PreviousIndex)
break;
}
}
void SortAndSearchByAUTHOR(){ system("cls");
char str[50];
cout << "Enter the Search Criteria for AUTHOR ";
gets(str);
SortByAUTHOR(); ListByAUTHOR(str);
}
void main()
{
char ch;
LoadAllBOOK();
do
{
system("cls");
cout<<"\n\n\n\tMAIN MENU";
cout<<"\n\n\t01. Create BOOK";
cout<<"\n\n\t02. SORT And Search By AUTHOR";
cout<<"\n\n\t03. lIST ALL ABOOK";
cout<<"\n\n\t04. EXIT";
cout<<"\n\n\tPlease Select Your Option (1-4) ";
ch=getche();
switch(ch)
{
case '1':system("cls");
hd[countBOOK].create_book();
countBOOK++;
break;
case '2':
SortAndSearchByAUTHOR();
break;
case '3':
ReportAll();
break;
case '4':
writeAllBOOK();
exit(0);
default :cout<<"\a";
}
}while(ch!='4');
}
</iostream></iomanip></string.h></process.h></stdio.h></conio.h></fstream>