I want to make a book system

I'm making a book system, although I'm stuck.

My goal is to let a user input another book if he/she wants to.

also I want to let the user return to main menu if he/wishes so.

but I can't seem to figure it out.

P.S I'm only allowed to use <iostream>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  void book::inputbook()
{
    book book_number;

    int choice;

    cout << "\n\nEnter Book Number: "; cin >> booknumber;
    cout << "Enter Book Title: "; cin.ignore(); getline (cin, booktitle);
    cout << "Enter Book Author: "; getline (cin, bookauthor);
    cout << "Enter Date of Publicaiton: "; cin >> bookpublish;

    cout << "Do you want to input again";
    cout << "[Y] or [N] = ";
    cin >> choice;

    if(choice == 'Y' || choice == 'y')
    {
        book_number.inputbook();
    }
    else(exit);
}


and also this, I can't seem to connect it into void book::inputbook().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int menu()
{
    int choice;

    book book_number;

    cout << "Welcome to DLC Library System\n";
    cout << "Final Project in Advance Programming in C++\n\n";
    
    cout << "Programmer\n";
    cout << "ME\n\n";
    
    cout << "====================\n";
    cout << "[1] --- Input Book\n";
    cout << "[2] --- Search Book\n";
    cout << "[3] --- Borrow Book\n";
    cout << "[4] --- Exit\n\n";
    cout << "====================\n";
    cout << "Input your choice: ";
    cin >> choice;

    switch (choice)
    {
    case 1: system("cls");
            cout << "INPUT BOOK";
            book_number.inputbook();
            break;
    }
}
Last edited on
The problem is that your inputbook() function is using recursion to fill data into a locally scoped copy of the object. As soon as each iteration of the recursion returns, that book "book_number" is going to go out of scope.

You are much better off using a while loop and an array in this situation (or create a second class called library which contains the list of books). Have you gone over dynamic allocation of arrays or linked lists at this point?

If the answer is no, then just make sure that your array is large enough for most use-cases.
Last edited on
No, how do I do that though? my requirement is to use objects.
OK, let's not get into dynamic memory just yet (it will be desirable later). How comfortable are you with loops and arrays?

Here's a thought of what things might look like, inputBook() should only deal with loading information of a single book, it doesn't need to know that there are any other books in existence in order to exist.
1
2
3
4
5
6
7
8
void book::inputbook()
{
    // I'm not a fan of this syntax, but I understand the thought-organization you are using
    cout << "\n\nEnter Book Number: "; cin >> this->booknumber;
    cout << "Enter Book Title: "; cin.ignore(); getline (cin, this->booktitle);
    cout << "Enter Book Author: "; getline (cin, this->bookauthor);
    cout << "Enter Date of Publicaiton: "; cin >> this->bookpublish;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{       
        book library[1024]; // until you get into dynamic memory, just allocate plenty of books
        int cursor = 0;
        int choice;
// output your menu
        cin >> choice;
        while(choice != 4 && cursor < 1024)
        {
                switch(choice)
                {
                        case 1:
                                // if choice is to add a book:
                                library[cursor].inputBook();
                                cursor ++;
                                break;
                }
// Output your menu
                cin >> choice;
        }
} 
Last edited on
Topic archived. No new replies allowed.