0

I wanna make a library program where book titles are stored in a linked list. But I don't know why it won't display the linked list.

#include <bits/stdc++.h>
using namespace std;

struct node
{
    string bookname;
    node* next;
};

node* head;
node* tail;

void initialize()
{
    head=NULL;
    tail=NULL;
}

void browse()
{   
    node* second;
    node* third;
    node* display;
    cout<<"Here's our books:"<<endl;
    head->bookname = "Book1";
    head->next = second;

    second->bookname = "Book2";
    second->next = third;

    third->bookname = "Book3";
    third->next = NULL;

    display = head;
    for (int i=1; i<=3; i+=1)
    {
        cout<<display->bookname<<endl;
        display = display->next;
    }

}

void menu()
{
    int choice;
    char repeat;

    cout<<"Welcome to the library! What do you want to do?"<<endl;
    cout<<"1. Browse books"<<endl;
    cout<<"2. Borrow books"<<endl;
    cout<<"3. See borrowed books"<<endl;
    cout<<"4. Donate books"<<endl;
    cout<<"5. Nothing"<<endl;
    cout<<"Pick a number, please: ";
    cin>>choice;

    if (choice==1)
    {
        browse();
    } else 
    {
        cout<<"Input not valid. Try again."<<endl;
    }
}

int main()
{
    initialize();
    menu();
}

I just made the browse() first coz I wanna try it out. The sentence "Here's our books:" is printing just fine so I know it calls the function, but why is it not printing the linked list? Did I do something wrong? I'm new to this.

  • 2
    You use `head` but it is initialized with `NULL`. You use `second` and `third` but they are uninitialized. A pointer stores an address only but you have to provide storage for data what you don't. One option could be to allocate storage with `new node` but I couldn't find any in your code. – Scheff's Cat Apr 16 '20 at 04:16
  • Recommendation: try to keep the linked list and the application logic using the linked list separate. You will find it much easier to test and debug your code if you only have to test and debug one concern at a time. – user4581301 Apr 16 '20 at 04:18
  • Recommendation: Don't mix `#include ` and `using namespace std;`. You should avoid them separately ([why](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [why](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)), but they are an extremely dangerous combination. There are tens of thousands of identifiers in the C++ Standard library and they turn the global namespace into a minefield of identifiers you now have to avoid accidentally reusing. – user4581301 Apr 16 '20 at 04:21
  • Recommendation: Do not ignore compiler warnings. They are the compiler telling you that even though your program compiles, it probably doesn't do what you want it to do. – user4581301 Apr 16 '20 at 04:23
  • 1
    @Scheff I added the `new node` and it works now, thank you! I thought that using `new node` is only for when I use class, but apparently not. Thanks again! –  Apr 16 '20 at 04:34
  • @user4581301 Oh, I don't really know about not using `#include ` and `using namespace std;` together. There's no warnings in my compiler and I haven't had any problems with it before. Do you have a link where I can read about this? –  Apr 16 '20 at 04:36
  • The only difference between `struct` and `class`: In `struct`s everything is `public` by default but in `class`es, it's `private`. And, you may not merge them for a single `struct`/`class`. E.g. after forward declaration of `struct node;` using `class node` is not accepted by compiler. Other differences I'm not aware of. [The _real_ difference between struct and class](https://www.fluentcpp.com/2017/06/13/the-real-difference-between-struct-class/). – Scheff's Cat Apr 16 '20 at 04:37
  • the bits folders are GCC implementation folders. They only exist in GCC. Here's what happens when your program is compiled in Visual Studio: https://godbolt.org/z/xYmzwh . stdc++.h is a god header that includes the entire C++ Standard library. That's thousands of files, and you maybe need ten of them. Loading and compiling all these files slows down the build process quite a bit. Usually a factor of around ten. The time you saved typing in only one include statement is eaten up after waiting for the second or third recompile. – user4581301 Apr 16 '20 at 04:43
  • Here's a simple example of the nastiness that can happen because of `#include ` and `using namespace std;` together: https://godbolt.org/z/jfZh2P . The function `std::size` was added to the C++ standard library in C++17. In any earlier revision this program complies and prints 4. In C++17 the header for `std::size` is included thanks to stdc++.h and `using namespace std;` eliminates the need to specify the `std` namespace. The compiler no longer knows what to do with `std::cout << size;` All you did was upgrade the compiler and poof. Broken code with a hard-to-read error. – user4581301 Apr 16 '20 at 04:50
  • Links to detailed explanations of their individual problems: https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h and https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – user4581301 Apr 16 '20 at 04:54
  • @user4581301 -- You can also add [std::data](https://en.cppreference.com/w/cpp/iterator/data) to the mix of functions that have been added to C++ lately. There is a lot of code out there that will break when using the newer C++ compilers, and that code has `using namespace std;` along with variables named `data`. – PaulMcKenzie Apr 16 '20 at 04:57
  • Didn't even know `std::data` existed. I could have been nailed by that one, but I'm safe because I let the `std` namespace do its job. – user4581301 Apr 16 '20 at 05:02

0 Answers0