Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't display items from an array

Status
Not open for further replies.

grscot

Programmer
Apr 25, 2003
16
GR
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<<&quot;No more room in the &quot;<<type<<&quot; array.\n&quot;;
cout<<&quot;The maximum number of &quot;<<type<<&quot; is &quot;<<MAX_ELEMENTS<<&quot;.&quot;<<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<<&quot;No &quot;<<type<<&quot; is found in the &quot;<<type<<&quot; array.\n&quot;;
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<<&quot;There are no &quot;<<type<<&quot;s to delete from the &quot;<<type<<&quot; array.&quot;<<endl;
else
item=get_string_ver2(&quot;Input element to delete: &quot;);
for(int element=0; element<this->num_elements; element++)
if (strcmp(item,element_list[element]->get_data()) == 0)
//cout<<&quot;Element found\n&quot;;
element_list[element]='\0';
else
cout<<&quot;Element not found\n&quot;;
}

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<<&quot;Book constructor called called\n&quot;;
this->bookDetails=get_string_ver2(&quot;input book title and author separated by a comma and a space character: &quot;);
}

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


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



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

Regards,
grscot





 
You delete an element from your list by assigning a 'null'
to it:

void List<Object>::removeElement(char* type)
{
...
element_list[element]='\0';
...
}

So, when you try to display the list you also have to check for element with null pointer in it, do something like:

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

 
Are you familiar with the STL data structures?

You want to use a &quot;list&quot; (a doubly-linked list) rather than an array if you're going to make deletions like that.

Tell me if you'd like to know more.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top