Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Can't display items from an array.

Can't display items from an array.

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++data-structurestutoriallearning
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    grscot
    wrote on last edited by
    #1

    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 void List::displayElements(char* type) { if (num_elements == 0) cout<<"No "<num_elements; element++) { cout<<'\n'; this->element_list[element]->display(association_list.get_data(element_list[element])); } } The code for deleting is: `` template void List::removeElement(char* type) { char* item; if (this->num_elements == 0) cout<<"There are no "<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: "<bookDetails<<"."<` ``

    N T 2 Replies Last reply
    0
    • G grscot

      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 void List::displayElements(char* type) { if (num_elements == 0) cout<<"No "<num_elements; element++) { cout<<'\n'; this->element_list[element]->display(association_list.get_data(element_list[element])); } } The code for deleting is: `` template void List::removeElement(char* type) { char* item; if (this->num_elements == 0) cout<<"There are no "<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: "<bookDetails<<"."<` ``

      N Offline
      N Offline
      Neville Franks
      wrote on last edited by
      #2
      1. the pre and code tags wrap around the code, not your explanatory comments. 2) You need to use the Formatting bar lt and gt to replace all lt and gt characters. Your for loops aren't displaying these operators. 3) In removeElement() you have: element_list[element]='\0'; you aren't removing the item from the array, nor are you deleting it, resulting in memory leaks. 4) In displayElements() this->element_list[element]->display(association_list.get_data(element_list[element])); will raise an exception after your delete because: this->element_list[element]-> is a NULL pointer for the deleted item. You should always test for NULL in cases like this, before attempting to use a pointer. 5) Why are you using this-> everywhere? Neville Franks, Author of ED for Windows. www.getsoft.com Make money with our new Affilate program
      1 Reply Last reply
      0
      • G grscot

        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 void List::displayElements(char* type) { if (num_elements == 0) cout<<"No "<num_elements; element++) { cout<<'\n'; this->element_list[element]->display(association_list.get_data(element_list[element])); } } The code for deleting is: `` template void List::removeElement(char* type) { char* item; if (this->num_elements == 0) cout<<"There are no "<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: "<bookDetails<<"."<` ``

        T Offline
        T Offline
        Toni78
        wrote on last edited by
        #3

        grscot wrote: element_list[element]='\0'; You are setting this element to NULL, so somewhere in the function display which you call it like this: this->element_list[element]->display(association_list.get_data(element_list[element])); you are trying to access this NULL pointer. It would be a good idea to post the code for the display function and also for association_list::get_data( class Object ); // Afterall I realized that even my comment lines have bugs

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups