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..

I think your problem is that you don't read the records from the file when you're making changes to them.

For example if the first thing I did after starting the program was to check-out or check-in a book, I select menu choice 2 and this is the code:

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

You seek to the position but don't read the data.
Then you update whatever the B class had in memory.
Then you write out the contents of the B class.

It would probably be obvious if the check-out/check-in process displayed the name of book and the number of copies you thought it had.

Unless you're running with a debugger (and you'd probably not be asking the same questions if you were) add more printing (at least for debugging, you can comment it out later if you don't want it in the final version). Knowing what your program is doing and understanding why it is doing what it is are the keys to making it all work.

Here's a personal style comment, feel free to ignore it:
Which is easier to read?

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: ";

or (this works because the compiler merges adjacent strings)

cout<<"\n"
          "1.Add new book.\n"
          "2.Issue/return book.\n"
          "3.Edit book info.\n"
          "4.Show book info.\n"
          "5.Add new user.\n"
          "6.Update user info.\n"
          "7.View user info.\n"
          "8.Delete data base .\n"
          "9.Aboout\n"
          "10.quit.\n"
          "Enter your choice: ";

or even (though this might run a nanosecond or two slower)

cout << "\n"
          << "1.Add new book.\n"
          << "2.Issue/return book.\n"
          << "3.Edit book info.\n"
          << "4.Show book info.\n"
          << "5.Add new user.\n"
          << "6.Update user info.\n"
          << "7.View user info.\n"
          << "8.Delete data base .\n"
          << "9.Aboout\n"
          << "10.quit.\n"
          << "Enter your choice: ";

Hello,
The problem reguarding printing the name of the book was solved,But now I have another problem..When I issue a book or return a book, Each of these transactions become different entries in the file.As a result,When Book info is viewed, the info on same book is repeated with different no. of copies.

Eg:I entered book detail of only Hamlet.Then,one copy of this book was issued and another copy was returned.Now, the database has info on three books called hamlet!!
OUTPUT:

Slno | Book code | Name | Author | no.of copies

1 | ES118 | Hamlet | Shakesphere | 5
2 | ES118 | Hamlet | Shakesphere | 4
3 | ES118 | Hamlet | Shakesphere | 6

See what I mean?
I need only the final updated data in the database.Is there any way to overwrite the data stored at a particular position in a file?Here,data is inserted.But,I want to overwrite(Update)

btw,here is the modified code segment pertraining to the context:

//In outside class defenition:
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";
}
//Insde main:
     else if(choice==2)
     {
       cout<<"\nEnter the sl.no. of the book :";
       cin>>bno;
       int loc=(bno-1)*(sizeof(books));
       file.seekp(loc);file.seekg(loc);
       file.read((char*)&B,sizeof(books));
       B.update();
       file.write((char*)&B,sizeof(books));
       file.seekp(0,ios::end);
     }

//and 

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

	};
	}

Thanks for the formatting tip,ill ramember that..

You need to seek before you read (saw you have that), but you need to seek to the same location before writing (by default, the act of reading moved the file pointer).

//Insde main:
     else if(choice==2)
     {
       cout<<"\nEnter the sl.no. of the book :";
       cin>>bno;
       int loc=(bno-1)*(sizeof(books));
       // seek for reading
       file.seekg(loc);
       file.read((char*)&B,sizeof(books));
       B.update();
       // seek for writing
       file.seekp(loc); 
       file.write((char*)&B,sizeof(books));
       file.seekp(0,ios::end);
     }
commented: Thank you again! +1

You need to seek before you read (saw you have that), but you need to seek to the same location before writing (by default, the act of reading moved the file pointer).

//Insde main:
     else if(choice==2)
     {
       cout<<"\nEnter the sl.no. of the book :";
       cin>>bno;
       int loc=(bno-1)*(sizeof(books));
       // seek for reading
       file.seekg(loc);
       file.read((char*)&B,sizeof(books));
       B.update();
       // seek for writing
       file.seekp(loc); 
       file.write((char*)&B,sizeof(books));
       file.seekp(0,ios::end);
     }

Thank you sir,That solved the problem and, As far as I know,All the problems.I will now mark this thread as solved.As this is a big software,I need to test it completely before launching it.

Thank you again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.