I am a student in C++.We undertook a school project to develop a library information system.We are only allowed to use Turbo C++ version 3 compiler.We cannot use ANSI/ISO C++(Not in our syllabus.)The following is my problem:

In my project, I have a class books.When the program is first run, it would create n number of object of books class.The data members would then be given values.It is then saved in a binary file.When the software is run again,the program asks whether the books are to be initialized?(i.e, clear the binary file and make another set of objects and write them to the disk.)If no is selected,the program will display the information of the books.i.e, it will retreive the information from memmory.In my program,Even though I create many objects,only the values of one object is outputed.

A simplified version of the code is given below:

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class books
{
		int bcopies;
	public:
		void init()
		{
			cout<<"Enter the no.of copies:";
			cin>>bcopies;
		}
		void putvalue()
		{
			cout<<"Copies:"<<bcopies<<"\n";
		}
};
int choice,n,i;
int main()
{
	clrscr();
	fstream file;
	cout<<"Initialize?(1/2)";
	cin>>choice;
	if(choice==1)
	{
		cout<<"Enter the no. of books:";
		cin>>n;
		--n;
		file.open("library.dat",ios::out|ios::binary|ios::trunc);
		fstream f;
		f.open("n.dat",ios::out|ios::binary);
		f.write((char*)&n,sizeof(int));
		books * B=new books[n];
		for(i=0;i<=n;i++)
		{
			clrscr();
			B[i].init();
			file.write((char*)&B[i],sizeof(books));
		}
		file.close();
		f.close();

	}
	fstream f;
	file.open("library.dat",ios::in|ios::binary);
	f.open("n.dat",ios::in|ios::binary);
	books * B=new books[n];
	file.seekg(0,ios::beg);
	file.read((char*)&B[i],sizeof(books));
	for(i=0;i<=n;i++)
	{


		B[i].putvalue();
	}
	getch();

	return 0;
}

OUTPUT:
__________________________
Initialize?(1/2) 1
Enter the number of books: 3
__________________________
Enter the number of copies:2
__________________________
Enter the number of copies:1
__________________________
Enter the number of copies:3
Copies:2
Copies:1
Copies:3
__________________________
<Program is closed,and excecuted again:>
__________________________
Initialize(1/2) 2
Copies:2
__________________________
why the data from the other objects are not displayed?

line 48: it is not reading the value of n from the data file. You need to put f >> n; between lines 47 and 48.


delete line 49 because that's the default when files are opened for reading.

line 50 should be this: file.read((char*)B,sizeof(books)); . The value of i is probably outside the bounds of the array anyway at that point.


line 51 is wrong. use < operator, not the <= operator.

The only one right decidion is to overload the operator<< () and operator>> ()

Sorry sir, the data entry phase of the output remains unchanged and if option 2 is given in the beginning, Nothing happens..btw,isnt there (char*)&B?
The code I changed to:

Only the part after if:

fstream f;
	file.open("library.dat",ios::in|ios::binary);
	f.open("n.dat",ios::in|ios::binary);
	f>>n;
	books * B=new books[n];
	file.read((char*)B,sizeof(books));
	for(i=0;i<n;i++)
	{
	       B[i].putvalue();
	}

	getch();
	return 0;

>>.btw,isnt there (char*)&B?
NO. B is already a pointer, so what you would create is a pointer to a pointer.

You didn't post the entire program so I can't help you more. I have no clue what Option 2 (or any other option) is.

The only one right decidion is to overload the operator<< () and operator>> ()

Yes, that is another way to do it, but not the only way. Overloading the operators would take more processing time because the program would have to read/write the structures/classes one at a time. The way he is doing it the entire array is read/written all in one shot.

Thank you all for your help.However,I am extremely happy to say that I found an Alternative logic.I stopped using the dynamic initialization and object array.My new code:

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class books
{
		int bcopies;
	public:
		void getdata()
		{
			cout<<"Enter the no.of copies:";
			cin>>bcopies;
		}
		void dispdata()
		{
			cout<<"Copies:"<<bcopies<<"\n";
		}
};
int main()
{
	clrscr();
	char ch;books B;
	fstream file;
	file.open("library.dat",ios::out|ios::ate|ios::binary|ios::in);
	cout<<"Would you like to add a book?";
	cin>>ch;
	while(ch=='y'||ch=='Y')
	{
		B.getdata();
		file.write((char*)&B,sizeof(books));
		cout<<"Continue?(y/n)";
		cin>>ch;
	}
	cout<<"data:\n";
	file.seekg(0,ios::beg);
	while(file.read((char*)&B,sizeof(B)))
	{
		B.dispdata();
	}
	getch();
	return 0;
}

Once again, thank you!

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.