Hi,
I have this annoying problem in my project program.It simulates a Library.From the data stored in a binary file,the information about books are to be displated as:
Sl.no Book code Book Name Author no. of books
1 ES101 Sherlock.. A.C Doyle 10
2. SF200 Harry Potter J K Rowling 2
....
The list continues.
But, The output I get Does not contain Book names!Well, the last book name is displayed, but others are not.See..
Sl.no Book code Book Name Author no. of books
1 ES101 A.C Doyle 10
2. SF200 Harry Potter J K Rowling 2
The first n-1 names of n books are missing!Here is the source code:
#include<fstream.h>
#include<process.h>
#include<conio.h>
#include<stdio.h>
class books
{
int bcopies,choice;
char bcode[5],bname[20],bauth[15];
public:
void init();
void update();//issue or return book
void bshow();//Display book info
};
void books::init()
{
clrscr();
cout<<"Enter book name: ";
gets(bname);
cout<<"Enter the book code:";
gets(bcode);
cout<<"Enter the name of the author:";
gets(bauth);
cout<<"Enter the number of copies present:";
cin>>bcopies;
}
void books::update()
{
clrscr();
cout<<"Would you like to:\n1.Issue Book?\n2.Return book?\nEnter your choice:";
cin>>choice;
if(choice==1)
{
if(bcopies>=1)
bcopies--;
else
cout<<"No book left.\n";
}
else if(choice==2)
bcopies++;
else
cout<<"Invalid choice";
}
void books::bshow()
{
cout.width(6);cout<<bcode;
cout.width(25);cout<<bname;
cout.width(20);cout<<bauth;
cout.width(20);cout<<bcopies;
cout<<"\n";
}
class user
{
int nbooks; //no of book with user
char uname[15];
public:
void inituser()
{
clrscr();
cout<<"Enter name :"; gets(uname);
cout<<"Enter the no of books with user :";
cin>>nbooks;
}
void uupdate()
{
cout<<"Enter the current no of books with user :";
cin>>nbooks;
cout<<"Press any key to countinue :";
getch();
}
void udisp()
{
cout.width(18);
cout<<uname;cout.width(18);
cout<<nbooks<<"\n";
}
};
int choice,cho;
void main()
{
do
{
clrscr();
fstream file,ufile;
file.open("library.dat",ios::in|ios::ate|ios::out|ios::binary);
ufile.open("user.dat",ios::in|ios::out|ios::ate|ios::binary);
int choice,bno,usno;
books B;
user U;
cout<<"\n\t\t\t\tWelcome to AKSHARA";
cout<<"\nWhat would you like to do?";
cout<<"\n1.Add new book.\n2.Issue/return book.\n3.Edit book info.\n4.Show book info.\n5.Add new user.\n6.Update user info.\n7.View user info.\n8.Delete data base .\n9.Aboout\n10.quit.\nEnter your choice: ";
cin>>choice;
if(choice==1)
{
char ch;
ch='y';
while(ch=='y'||ch=='Y')
{
clrscr();
B.init();
file.write((char*)&B,sizeof(B));
cout<<"Continue?(y/n):";
cin>>ch;
if(ch=='n'||ch=='N')
break;
};
}
else if(choice==2)
{
cout<<"\nEnter the sl.no. of the book :";
cin>>bno;
int loc=(bno-1)*(sizeof(books));
file.seekp(loc);
B.update();
file.write((char*)&B,sizeof(B));
file.seekp(0,ios::end);
}
else if(choice==3)
{
cout<<"Enter the serial no. of the book:";
cin>>bno;
int loc=(bno-1)*sizeof(books);
if(file.eof())
file.clear();
file.seekp(loc);
B.init();
file.write((char*)&B,sizeof(B));
}
else if(choice==4)
{
file.seekg(0);
clrscr();
int i=1;
cout<<"\n Books information:\n";
cout.width(6);
cout<<"Sl.No";
cout.width(6);
cout<<"Code";
cout.width(25);
cout<<"Book name";
cout.width(20);
cout<<"Author";
cout.width(20);
cout<<"Copies";
cout<<"\n";
while(file.read((char*)&B,sizeof(books)))
{
cout.width(6);
cout<<i;i++;
B.bshow();
};
}
else if(choice==5)
{
char ch='y';
while(ch=='y'||ch=='Y')
{
clrscr();
U.inituser();
ufile.write((char*)&U,sizeof(user));
cout<<"Continue?(y/n):";
cin>>ch;
if(ch=='n'||ch=='N')
break;
};
}
else if(choice==6)
{
cout<<"Enter the serial number to be edited:";
cin>>usno;
int loc=(usno-1)*sizeof(user);
if(ufile.eof())
ufile.clear();
ufile.seekp(loc);
U.uupdate();
ufile.write((char*)&U,sizeof(user));
ufile.seekp(0,ios::end);
}
else if(choice==7)
{
clrscr();
int i=1;
cout.width(6);cout<<"Sl.no";
cout.width(18);
cout<<"User name";
cout.width(18);
cout<<"No. of books\n";
ufile.seekg(0);
while(ufile.read((char*)&U,sizeof(user)))
{
cout.width(6);
cout<<i;
U.udisp();i++;
};
}
else if(choice==8)
{
clrscr();
cout<<"\nWould you like to :\n1.Delete user database?\n2.Delete Books Database?\n";
cout<<"\n\t\t\t\t WARNING!\t\t\t\t\n\t\t\tALL PREVIOUS DATA WILL BE LOST.\n";
cout<<"\nEnter your choice:";
int ch;
cin>>ch;
if(ch==1)
{
ufile.close();
ufile.open("user.dat",ios::out);
}
else if(ch==2)
{
file.close();
file.open("library.dat",ios::out);
}
else
cout<<"Invalid choice\n";
}
else if(choice==9)
{
clrscr();
cout<<"\n\t\t\t\tAKSHARA\n\n\n\tVersion 1.0\n\n\n\tCreated by group 5,+2 Computer Science";
cout<<"\n\n\n\tLibrary information system software.";
cout<<"\n\n\n\tPress any key to continue.";
}
else if(choice==10)
{
file.close();
ufile.close();
exit(0);
}
else
cout<<"Invalid choice.";
cout<<"Quit?(yes=1,no=2)";
cin>>cho;
if(cho==1)
exit(0);
}while(cho==2);
getch();
}
I know code is a bit lengthy, but I think problem is in a small region..
I hope you guys can help me..