Thread: Can't display item from an array

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    16

    Can't display item from an array

    Dear all,
    All I'm trying to do is to enter 3 books from the keyboard to an array and then traverse the array to display the elements and also prompt the user to enter the book names to delete them from the array(if the book exists into the array).

    The code that adds to the array is:

    template<class Object>
    void List<Object>::addElement(char* type)
    {
    if (this->num_elements == MAX_ELEMENTS)
    {
    cout<<"No more room in the "<<type<<" array.\n";
    cout<<"The maximum number of "<<type<<" is "<<MAX_ELEMENTS<<"."<<endl;
    }
    else
    {
    this->element_list[num_elements]=new Object;
    (this->num_elements)++;
    }
    }

    The code that displays the elements is:

    template<class Object>
    void List<Object>::displayElements(char* type)
    {
    if (num_elements == 0)
    cout<<"No "<<type<<" is found in the "<<type<<" array.\n";
    else
    for(int element=0; element<this->num_elements; element++)
    {
    cout<<'\n';
    this->element_list[element]->display(association_list.get_data(element_list[element]));
    }
    }

    The code for deleting is:

    template<class Object>
    void List<Object>::removeElement(char* type)
    {
    char* item;
    if (this->num_elements == 0)
    cout<<"There are no "<<type<<"s to delete from the "<<type<<" array."<<endl;
    else
    item=get_string_ver2("Input element to delete: ");
    for(int element=0; element<this->num_elements; element++)
    if (strcmp(item,element_list[element]->get_data()) == 0)
    //cout<<"Element found\n";
    element_list[element]='\0';
    else
    cout<<"Element not found\n";
    }

    But there is a problem after deleting a book from the array.
    I can't display the elements of the array, after a book has been deleted. For example if I enter a, a as the first book and b, b as the second book and then after deleting b, b, try to display the books into the array, I will only see the details of the first book and after that the program will be terminated due to an error. The error occurs at the file:
    Book.cpp
    Book::Book()
    {
    cout<<"Book constructor called called\n";
    this->bookDetails=get_string_ver2("input book title and author separated by a comma and a space character: ");
    }

    Book::~Book()
    {
    if (this->bookDetails)
    delete [] this->bookDetails;
    }


    void Book::display(Member* borrower)
    {
    cout<<"Book title and author are: "<<this->bookDetails<<"."<<endl;
    if (borrower)
    cout<<"The member's name is: "<<borrower->get_data()/*get_name()*/<<"."<<endl;
    else
    cout<<"No member has borrowed a book."<<endl;
    }
    in the first line of code of the display function.(cout<<"Book title ....)



    What is wrong?
    If you need more details or anything more about the program, please let me know.

    Regards,
    grscot

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "All I'm trying to do is to enter 3 books from the keyboard to an array."

    Wow! I hope they aren't computer books.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109

  4. #4
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    strange things in your code:

    1)I would stop using: this -> some_member.

    2)At the delete method:

    element_list[element]='\0';

    '\0' is a char, the correct would be NULL isnīt? I understood that element_list is a pointer array.
    And use code tags!!!
    Nothing more to tell about me...
    Happy day =)

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    1) yes the this -> is rather un needed....what else is going to be setting with the class if its not the object that called it ??

    2) '\0' is null as well

  6. #6
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    are you using

    num_elements-- ; (num_elements - 1 , I mean )

    somewhere in the delete code? or this isnīt needed?

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    16

    I still can't display book and boorower's details together

    Dear all,
    I can't borrow a book even though the code does not have any error. When I try to display the book details along with the member's name (in case the member has borrowed the book), I can't display the member's name, but only the book details and a message: "no member has borrowed a book".


    Though the code works fine when I have one book and one member, but now that I'm using an array of books and members it doesn't work.


    The code that displays the book and the member's name when I have 1 book and 1 member is:
    Code:
    void Book::display(Member* borrower)
    {
    	cout<<"Book title and author are: "<<this->bookDetails<<"."<<endl;
    	if (borrower)
    		cout<<"The member's name is: "<<borrower->get_Data()/*get_name()*/<<"."<<endl;
    	else
    		cout<<"No member has borrowed a book."<<endl;
    }

    Code:
    char* get_Data(){return this->name;}
    returns the member's full name, entered in the constructor


    I have an associationlist that holds pointers to associations. Each association has two pointers, one to a book object and the other to the member object.

    Code:
    class AssociationList
    {
    public:
    	//It initialises all slots in the associationlist array to zero.
    	AssociationList();
    
    	/* It searches the associationlist, if book is found then returns 
    	   the member that is connected with.*/
    	   //Member* get_member(Book* book);
    	   Member* get_data(Book* book);
    	
    	/* It searches the associationlist, if member is found then returns
    	   the book that is connected with.*/ 
    	   //Book* get_book(Member* member);
    	   Book* get_data(Member* member);
    
    	/* Checks that book/member not already linked
    	   creates association if objects are free to link
    	   returns whether or not link was valid */
    	bool link(Book* book,Member* member);
    
    	/* Checks that book and member are linked 
    	   deletes association if they are linked
    	   returns whether or not unlinking was valid */
    	bool unlink(Book* book,Member* member);
    private:
    	Association<Book,Member>* association_list[LIST_SIZE];
    };


    The code that performs the linking between 1 book object and 1 member object is the following:

    Code:
    template<class Book,class Member>
    bool AssociationList<Book,Member>::link(Book* book,Member* member)
    {
    	bool searching=true;
    	bool success=true;
    	int index=0;
    	int free_slot;
    
    	while(searching)
    	{
    		if(this->association_list[index])
    			if((this->association_list[index]->linked_book()==book ||
    				(this->association_list[index]->linked_member()==member)))
    		{
    			searching=false;
    			success=false;
    		}
    			else
    				index++;
    		else
    		{
    			free_slot=index;
    			index++;
    		}
    		if(searching&&(index == LIST_SIZE))
    			searching=false;
    	}
    	if (success)
    		this->association_list[free_slot]=new Association(book,member);
    	return success;
    }


    Code that is used in the link function

    Code:
    template<class Book,class Member>
    class Association
    {
    	public:
    		//Sets up book and member with parameters
    		Association(Book* book, Member* member);
    		
    		//Returns Book
    		Book* linked_book(){return this->book;}
    		
    		//Returns Member
    		Member* linked_member(){return this->member;}
    		
    	private:
    		Book* book;
    		Member* member;
    };
    and

    Code:
    template<class Book,class Member>
    Association<Book,Member>::Association(Book* book, Member* member)
    {
    	cout<<"Association constructor called\n";
    	this->book=book;
    	this->member=member;
    }
    What needs to be done so that each member to be able to borrow one book?
    (10-members)
    (10-books)
    (1 member can only borrow one book at a time)

    Regards,
    grscot

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    if(this->association_list[index])
      if((this->association_list[index]->linked_book()==book ||
         (this->association_list[index]->linked_member()==member)))
      {
          searching=false;
          success=false;
      }
      else
          index++;
    else
    needs another set of {} to start. Whether it solves the problem is unclear.

    Code:
    if(this->association_list[index])
    {
       if((this->association_list[index]->linked_book()==book ||
          (this->association_list[index]->linked_member()==member)))
       {
           searching=false;
           success=false;
        }
        else
           index++;
    }
    else
    Last edited by elad; 05-02-2003 at 03:03 PM.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    16

    I can't display the book and the borrower's details together

    Sorry,
    I can't understand your answer

    Could you please send it again, with some comments?

    regards,
    grscot

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    if an if statement is more than 1 line of code or contains a loop or other if statement it doesnt need brackets.....however, your first if statement has an if and an else, therefore, you need to have a bracket surrounding it all like he mentioned.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    16

    I can't display book and borrower's details together

    Ok,

    Now I understand.

    But any suggestion about my problem?

    thanks,
    grscot

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    16

    I can't display book and borrower's details together

    Dear all,

    I'm still struggling to display the book and the borrower's details together.

    Could someone help me?

    regards,
    grscot

  13. #13
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Need to know details

    I want to help you, but I need to know what errors you are getting. Are compiler errors? Run time errors?
    Couldnīt you attach all your code? So I would examine better.
    Nothing more to tell about me...
    Happy day =)

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    16
    Here is the code,

    Thanks for helping me,

    regards,
    grscot

  15. #15
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Donīt yout forgot something?

    What is the problem? Running your program I noticed that the borrow function donīt works properly. Is that the problem?
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Retail Outlet Managment System - the 4th
    By Presidentofusa in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 10:44 PM
  2. Char array display error...
    By Karpaty in forum C Programming
    Replies: 23
    Last Post: 10-31-2007, 07:17 AM
  3. help to display an array
    By jujubeats in forum C++ Programming
    Replies: 9
    Last Post: 10-29-2006, 08:59 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM