Thread: SBBBS::Software for Big Bang Book Store

  1. #1
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    Software for Big Bang Book Store

    Hi,



    Problem Statement:

    A book store maintains the inventory of books that are being sold at the shop. The list includes details such as author, title, price, and stock position. Whenever a customer wants a book, the sales person inputs the title and author and the system searches the list and displays whether its available or not.

    Design a system using a class called books with suitable member functions and constructors. Use new operator in constructors to allocate memory space required.

    The salesman should be able to add new books to the database.

    Code:


    Code:
    #include <iostream>
    
    using namespace std;
    
    int bookcount;
    books *pointer;
    void menu(void);
    
    class books
    {
    	private:
    	
    	string title;
    	string author;
    	float price;
    	int stock;
    	
    	
    	public:
    	
    	books(string ltitle,string lauthor,float lprice,int lstock)
    	{
    		title=ltitle;
    		author=lauthor;
    		price=lprice;
    		stock=lstock;
    	}
    	
    	search(string ltitle)
    	{
    		
    		//Search Algorithm goes here
    	    if(found)
    	    {
    	    	sell(ltitle);
    	    }
    	    else
    	    {
    	    	cout<<"Book not found";
    	    }  
    	
    	}
    	
    	sell(string ltitle)
    	{
    		cout<<"\nBook name:"<<ltitle;
    		cout<<"\nPrice:"<<price;
    		cout<<"\nStock:"<<stock;
    		cout<<"\nEnter no. of copies to buy";
    		int num;
    		cin>>num;
    		if(num<=stock)
    		{
    			stock-=num;
    			cout<<"\nSold!";
    		}
    		else
    		{
    			cout<<"\nnot available";
    		}
    	}
    	
    
    };
    
    int main()
    {
    	
    	int choice=1;
    	string ltitle;
    	string lauthor;
    	float lprice;
    	int lstock;
    	
    	menu();
    	while(choice!=3)
    	{
    			    
    	    cout<<"\nEnter Your choice:";
    	    cin>>choice;
    	    if(choice==1)
    	      {
    	      	cout<<"\nEnter name of the book:";
    	      	cin>>ltitle;
    	      	search(ltitle);
    	      }
    	    
    	    if(choice==2)
    	    {
    	    	cout<<"\nEnter name of book:";
    	    	cin>>ltitle;
    	    	cout<<"\nAuthor";
    	    	cin>>lauthor;
    	    	cout<<"\nPrice:";
    	    	cin>>lprice;
    	    	cout<<"\nStock:";
    	    	cin>>lstock;
    	    	pointer=new books(string ltitle,string lauthor,float lprice,int lstock);
    	    }
    		menu();
    	}
    }
    
    
    void menu(void)
    {
    	cout<<endl<<"\t\t\tWelcome to Big Bang Book Store"<<endl;
    	cout<<"1) Buy a Book"<<endl;
    	cout<<"2)Add a book to database"<<endl;
    	cout<<"3)Exit"<<endl;
    }


    I am having trouble with making the search algorithm........and perhaps you all will find 50 other flaws in it.

    Thanks for the help.
    Last edited by abh!shek; 04-09-2008 at 06:14 AM.

  2. #2
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    I don't want you to write the search algorithm for me...if it sounds like that in the previous post. I just want some directions from here.

  3. #3
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Google!

  4. #4
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Google what keyword?

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    First of all Google for "C++".
    You don't know even the basics!

  6. #6
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Ok I forgot to return 0, but is the class definition correct?

  7. #7
    Banned
    Join Date
    Nov 2007
    Posts
    678
    You forgot to specify the return types of the functions!

  8. #8
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    OK I will come back with new code after some time.

  9. #9
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    I am getting many errors highlighted in red. Hope you can help me get rid of them.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int bookcount;
    books *pointer;
    void menu(void);
    
    class books
    {
    	private:
    	
    	string title;
    	string author;
    	float price;
    	int stock;
    	
    	
    	public:
    	
    	books(string ltitle,string lauthor,float lprice,int lstock)
    	{
    		title=ltitle;
    		author=lauthor;
    		price=lprice;
    		stock=lstock;
    	}
    	
    	void search(string ltitle)
    	{
    		books *lpointer;
    		int lnumbook;
    		bool found=0;
    		lpointer=pointer; /*error 'pointer' was not declared in this scope
    
    ...but I declared 'pointer' as global variable */
    
    
    		for(lnumbook=1;lnumbook<=bookcount;lnumbook++)
    		{
    			if(!strcmp((*lpointer).title,ltitle)) //cannot convert string* to const char*
    				found=1;
    		}
    		
    	    if(found)
    	    {
    	    	sell(ltitle);
    	    }
    	    else
    	    {
    	    	cout<<"Book not found";
    	    }  
    	
    	}
    	
    	void sell(string ltitle)
    	{
    		cout<<"\nBook name:"<<ltitle;
    		cout<<"\nPrice:"<<price;
    		cout<<"\nStock:"<<stock;
    		cout<<"\nEnter no. of copies to buy";
    		int num;
    		cin>>num;
    		if(num<=stock)
    		{
    			stock-=num;
    			cout<<"\nSold!";
    		}
    		else
    		{
    			cout<<"\nnot available";
    		}
    	}
    	
    
    };
    
    
    
    int main()
    {
    	
    	int choice=1;
    	string ltitle;
    	string lauthor;
    	float lprice;
    	int lstock;
    	
    	menu();
    	while(choice!=3)
    	{
    			    
    	    cout<<"\nEnter Your choice:";
    	    cin>>choice;
    	    if(choice==1)
    	      {
    	      	cout<<"\nEnter name of the book:";
    	      	cin>>ltitle;
    	      	search(ltitle); //no matching function call
    	      }
    	    
    	    if(choice==2)
    	    {
    	    	cout<<"\nEnter name of book:";
    	    	cin>>ltitle;
    	    	cout<<"\nAuthor";
    	    	cin>>lauthor;
    	    	cout<<"\nPrice:";
    	    	cin>>lprice;
    	    	cout<<"\nStock:";
    	    	cin>>lstock;
    	    	pointer=new books(string ltitle,string lauthor,float lprice,int lstock);
                      /*pointer not in this scope. expected-primary expression before ltitle, lauthor, lprice,lstock*/
    
    
    	    }
    		menu();
    	}
    	return 0;
    }
    
    
    void menu(void)
    {
    	cout<<endl<<"\t\t\tWelcome to Big Bang Book Store"<<endl;
    	cout<<"1) Buy a Book"<<endl;
    	cout<<"2)Add a book to database"<<endl;
    	cout<<"3)Exit"<<endl;
    }

  10. #10
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Please get a C++ book first and learn something from it.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    books *pointer;
    At this point the compiler doesn't know what books is. Hence all the following errors concerning pointer. This should come after the definition of class books or books needs to be forward declared.

    Code:
    if(!strcmp((*lpointer).title,ltitle)) //cannot convert string* to const char*
    string is a C++ standard class and not a C-style char array. Therefore you don't use standard C style string manipulation functions (things starting with str...) to work with it. To compare string instances, use comparison operators (==, !=, < etc).

    Code:
    search(ltitle); //no matching function call
    search is a member function of books, not a free function. Therefore you need an instance of books to call it with.

    Even if you fix the compiler errors, I wouldn't expect this program to work very well.

    It seems that you have not really understood what the class books should represent in the first place. Does it represent a single book (because it has one title, one author, one price) or does it represent a collection of books (because it should provide means to search for a book)? I doubt it could be both at a time.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Quote Originally Posted by abk View Post
    Code:
    pointer=new books(string ltitle,string lauthor,float lprice,int lstock);
    and how did you miss this line anon? he does not even know the basics yet.


    abk please get a good book on C++. I can suggest an online free book also if you don't have a book.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I didn't miss it. I forgot to comment on it.

    All the code is pretty much useless as it is anyway.

    I would suggest reading the problem description again:

    A book store maintains the inventory of books that are being sold at the shop. The list includes details such as author, title, price, and stock position. Whenever a customer wants a book, the sales person inputs the title and author and the system searches the list and displays whether its available or not.
    So you are supposed to write an inventory class and pretty much the only required member function is something like bool is_available(string title, string author).

    (The requirements are a bit vague, though, if that is the full assignment description.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Language REFERENCE book recommendations?
    By DougDbug in forum C++ Programming
    Replies: 5
    Last Post: 03-10-2011, 02:26 AM
  2. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 04:18 PM
  3. Newbie - MFC code from a book in VC++.Net
    By Guardian in forum Windows Programming
    Replies: 2
    Last Post: 04-27-2002, 07:17 PM
  4. C++: Reference Book, GUI, Networking & Beyond
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2001, 08:03 PM