Click here to Skip to main content
15,887,945 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Here is my code for library management system. Now i have tried to add a shelves function in this code which is supposed to work in this manner: Each shelf can have 3 book entries and after that, books are entered into the next shelf.
The problem i'm having is that all the books are entered into the 1st shelf even after the 3rd entry. The code used for this function is in bold!
C++
//***************************************************************
//                   CLASS USED IN PROJECT
//***************************************************************



class book
{
	char bno[6];
	char bname[50];
	char aname[20];
	int count1;
	int shelf;
    
    
  public:
	
    
    
	book()
	{
		count1=0;
        	shelf=1;
        }
    
    
	void create_book()
	{
		cout<<"\nNEW BOOK ENTRY...\n";
		cout<<"\nEnter The book no.";
cin.ignore();
		cin.getline(bno,6);
		cout<<"\n\nEnter The Name of The Book ";
		cin.getline(bname,50);
		cout<<"\n\nEnter The Author's Name ";
		cin.getline(aname,20);
		cout<<"\n\n\nBook Created..";
		count1++;
		
		if (count1 == 3 )
		{
    			shelf++;
			count1=0;
    		}
	}

	void show_book()
	{
		cout<<"\nBook no. : "<<bno;
		cout<<"\nBook Name : "<<bname;
		cout<<"\nAuthor Name : "<<aname;
		cout<<"\nShelf number : "<<shelf;
	}

	char* retbno()
	{
		return bno;
	}

	void report()
	{cout<<bno<<setw(30)<<bname<<setw(30)<<shelf<<setw(30)<<aname<<endl;}


};         //class ends here
Posted
Updated 8-May-12 12:16pm
v2

1 solution

You need to make a static class member that holds the next count and shelf, and initialize them statically, rather than initializing them in the constructor...

class book
 {
 char bno[6];
 char bname[50];
 char aname[20];
 
 int my_count;
 int my_shelf;
 static int total_books_count;
 static int current_shelf;

 book () 
 {
    my_count = total_books_count;
    my_shelf = current_shelf;

    total_books_count++;
    if (total_books_count == 3) 
    {
        total_books_count = 0;
        current_shelf++;
    }
 }

 void create_book()
 {
    cout<<"\nNEW BOOK ENTRY...\n";
    cout<<"\nEnter The book no.";
    cin.ignore();
    cin.getline(bno,6);
    cout<<"\n\nEnter The Name of The Book ";
    cin.getline(bname,50);
    cout<<"\n\nEnter The Author's Name ";
    cin.getline(aname,20);
    cout<<"\n\n\nBook Created..";
 }

...

}

int book::total_books_count = 0;
int book::current_shelf= 0;


The way you have the code written, every book starts out with it's own count1 = 0 and shelf = 1 and each book thinks it's the only book...
 
Share this answer
 
v3
Comments
ShayanTanwir 8-May-12 19:09pm    
with this code, every book ends up with a shelf no=0 for all entries.
TRK3 8-May-12 19:51pm    
Fixed.
Sandeep Mewara 9-May-12 7:23am    
5 for the effort.
TRK3 9-May-12 13:07pm    
Thanks. I was just going to tell him what was wrong with the code, but I wasn't sure of OP's skill/training and was afraid the explanation wouldn't be as easy to understand as if I showed him.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900