Note that there are some explanatory texts on larger screens.

plurals
  1. POuser defined variables on the heap getting destroyed in while loop
    primarykey
    data
    text
    <p>I an odd problem that i cant understand if someone could point me in the right direction it would be greatly appreciated.I create my own linked list variables on the heap but when i go to add another variable to it they all get destroyed and i dont know why. in my main i set up my variables like this<br> <strong>main.cpp</strong> </p> <pre><code> Book* temp; temp = bookSetUp(); </code></pre> <p>this goes to a different cpp called functions which sets up the objects like this:<br> <strong>functions.cpp</strong> </p> <pre><code> Book* bookSetUp() { //The items that populate the list Book* a= new Book("A Tale of Two Cities", "Charles Dickens", "1", true); Book* b= new Book("Lord of the rings", "J.R.R Tolkein", "2", true); Book* c= new Book("Le Petit Prince", "Antoine de Saint-Exupéry", "3", true); Book* d= new Book("And Then There Were None", "Agatha Christie", "4", true); Book* e= new Book("Dream of the Red Chamber","Cao Xueqin","5", true); Book* f= new Book("The Hobbit","J.R.R Tolkein","6", true); //sets up the pointers between books a-&gt;setPrev(NULL); a-&gt;setNext(b); b-&gt;setPrev(a); b-&gt;setNext(c); c-&gt;setPrev(b); c-&gt;setNext(d); d-&gt;setPrev(c); d-&gt;setNext(e); e-&gt;setPrev(d); e-&gt;setNext(f); f-&gt;setPrev(e); f-&gt;setNext(NULL); //sets up a temp pointer to a Book* temp = a; //returns the temp pointer to a return temp; } </code></pre> <p>this works perfectly but later on when i go to add to the list again in the main using:<br> <strong>main.cpp</strong> </p> <pre><code>else if(checkRegUser(username, password, regUserList) == true) { int choice = 99; cout &lt;&lt; "Welcome Registered user: "&lt;&lt; username &lt;&lt; endl; while(choice != 0) { //this is so the print will start everytime as if you run it once print will be at NULL thereafter Book* print = temp; choice = options(); if(choice == 1) { while(print!=NULL) { cout&lt;&lt;"Name: "&lt;&lt;print-&gt;getName()&lt;&lt;endl&lt;&lt;"Author: "&lt;&lt;print-&gt;getAuthor()&lt;&lt;endl&lt;&lt;"ISBN: "&lt;&lt;print-&gt;getISBN()&lt;&lt;endl&lt;&lt;"Availability: "&lt;&lt;print-&gt;getAvail()&lt;&lt;endl; cout&lt;&lt;endl; print = print-&gt;getNext(); } print = temp; } if(choice == 2) { search(temp); } if(choice == 3) { takeOut(temp); } if(choice == 4) { returnBack(temp); } if(choice == 5) { append(temp); } if(choice == 6) { cout&lt;&lt;"Sorry you have the privilege needed to use this function."&lt;&lt;endl; } if(choice == 7) { choice = 0; } } } </code></pre> <p>My user defined variables get destroyed. I debugged and they just disappeared i am not sure why!<br> Just in-case its needed here is my add() function because I feel It could be me missing something small or just making a disastrous mistake. My add function is in the functions.cpp and I know all the links are working as I have everything else running apart from this<br> <strong>functions.cpp</strong> </p> <pre><code>Book* append(Book* tempParam) { string title; string author; string isbn; bool avail; cout&lt;&lt;"What is the book called?"&lt;&lt;endl; cin.clear(); cin.ignore(); getline(cin, title); cout&lt;&lt;"Who is the author?"&lt;&lt;endl; cin.clear(); cin.ignore(); getline(cin, author); cout&lt;&lt;"What is the ISBN to be?"&lt;&lt;endl; cin&gt;&gt;isbn; Book* temp = new Book(title, author, isbn, true); Book* list = tempParam;int count; while(list!=NULL) { if(list-&gt;getNext()==NULL&amp;&amp;list-&gt;getName()!=title) { list-&gt;setNext(temp); temp-&gt;setNext(NULL); temp-&gt;setPrev(list); cout&lt;&lt;"Your book has been added"&lt;&lt;endl; cout&lt;&lt;temp-&gt;getName()&lt;&lt;temp-&gt;getAuthor()&lt;&lt;endl; } list = list-&gt;getNext(); } tempParam = list; return tempParam; } </code></pre> <p>My user defined classes are working perfectly its just when I go to add that my list gets destroyed any ideas??</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload