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
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