Search code examples
c++structdynamicheap-memorydynamic-memory-allocation

Problem pointing to a variable in a structure in dynamic memory


I cannot seem to be able to input values into the structure I already declared. I am not sure if it's a syntax or logical error.

I've already tried to change syntax but always end up with the same error.

struct Book{

    string title;
    Book* next;
};

Book bookName;
Book author;


Book* add_node(Book* in_root){

    cout <<"Enter Book name \n";
    cin >> bookName.title;

    cout << "Enter author name \n";
    cin >> author;
    author = new Book();
    Book -> Book.next = author;
}

The error is encountered in this portion of the code:

    cout << "Enter author name \n";
    cin >> author;
    author = new Book();
    Book -> Book.next = author;

Solution

  • First of all, there are a couple of logical errors inside the code.

    • There is absolutely no need to have 2 books named bookName and author unless I'm misinterpreting their purpose.
    • Book -> and Book.next is invalid logic, since you're telling it to operate on the data type Book, not an object of type Book.

    The code of what you potentially wanted should look something like this:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct Book{
        string title;
        string author_name; // You potentially wanted this?
    
        Book* next;
    };
    
    // This function assumes that `current_node->next` is `nullptr`
    // The reasons for this is that making it handle such cases might be too difficult for you yet.
    Book* add_node(Book* current_book){
        if(current_book == nullptr){
            cout << "Cannot link a new book to an non-existant book!\n";
            return nullptr;
        }
    
        Book* new_book = new Book();
    
        cout <<"Enter the book name\n";
        cin >> new_book->title;
    
        cout << "Enter the author name\n";
        cin >> new_book->author_name;
    
        new_book->next = nullptr;
    
        current_book->next = new_book;
        return new_book;
    }
    
    int main(){
        Book* book = new Book();
        book->next = nullptr;
    
        cout <<"Enter the name of the first book\n";
        cin >> book->title;
    
        cout << "Enter the name of the first book's author\n";
        cin >> book->author_name;
    
        add_node(add_node(book));
    
        return 0;
    }
    

    The reason why I didn't make the function handle cases when current_book->next != nullptr is because it would then require the usage of pointers to pointers. If you're interested in it, here it is:

    Book* add_node_v2(Book* current_book){
        if(current_book == nullptr){
            cout << "Cannot link a new book to an non-existant book!\n";
            return nullptr;
        }
    
        Book* new_book = new Book();
    
        cout <<"Enter the book name\n";
        cin >> new_book->title;
    
        cout << "Enter the author name\n";
        cin >> new_book->author_name;
    
        new_book->next = nullptr;
    
        // Move to the last book in the chain
        Book** ptr_to_next = &current_book->next;
        while(*ptr_to_next != nullptr){
            ptr_to_next = &(*ptr_to_next)->next; 
        }
    
        *ptr_to_next = new_book;
        return new_book;
    }
    

    Keep in mind that you will eventually have to delete all the books in the chain.