0

I've written out the code for an assignment for school, but it isn't working as it should. Everything compiles and runs, but I run into a couple of issues. Neither of the first couple of menu options return anything on screen. It almost makes me thing I've done something wrong when loading my object array from the file.

I've looked this over for the last couple of hours, and can't seem to find out what I'm doing wrong, so I'm turning to people with far more experience that I have.

*Edited to cut out some of the code that may be unnecessary for you to help with.

Here are the functions that seem to be giving me problems. They are called in a switch in the main program.

Here is main()

int main()
{
    //Arrays
    BookClass books[MAX_BOOKS];

    //Variables
    int choice;     //For menu selection from getMenuChoice.
    int bookNum;    //For holding array location of book for the findBook function.
    int numBooks;   //Holds the number of books in the array
    string searchTitle;     //Hold value of user's search for findBook function.
    BookClass newBook;
    fstream file;   //For input of file to load array

    getBooks(books, numBooks);

    do
    {
        choice = getMenuChoice();

        if (choice != 6)
        {
            switch (choice)
            {
            case 1:
            {
                showBooks(books, numBooks);
                break;
            }
            case 2:
            {
                showTitles(books, numBooks);
                break;
            }
            case 3:
            {
                cout << "\nEnter the title you wish to find: ";
                getline(cin, searchTitle);
                cin.ignore();
                findBook(searchTitle, books, numBooks);
                bookNum = findBook(searchTitle, books, numBooks);
                break;
            }
            case 4:
            {
                cout << "\nEnter the title you wish to check out: ";
                getline(cin, searchTitle);
                cin.ignore();
                findBook(searchTitle, books, numBooks);
                bookNum = findBook(searchTitle, books, numBooks);
                books[bookNum].returnBook();
                break;
            }
            case 5:
            {
                cout << "\nEnter the title you wish to return: ";
                getline(cin, searchTitle);
                cin.ignore();
                findBook(searchTitle, books, numBooks);
                bookNum = findBook(searchTitle, books, numBooks);
                books[bookNum].checkOutBook();
                break;
            }
            }
        }
        if (choice == 6)
        {
            cout << "\nThank you for using the library program!! This program will now end!" << endl;
        }
    } while (choice != 6);

    system("pause");
    return(0);
}

And here are the functions that seem to be giving me issues...

//Function definition for getBooks. Loads the 'books' array using data from input file 'books.txt'
void getBooks(BookClass books[], int &numBooks)
{
    string bookTitle, authorName, bookPublisher, bookISBN;
    double bookPrice;
    int bookYear, bookInStock;
    int total = 0;
    ifstream file;
    file.open("books.txt", ios::in);


    if (file.fail())
    {
        cout << "\nThe file failed to open. Please restart the program and try again!" << endl;
        system("pause");
        return;
    }

    for (int i = 0; i < numBooks; i++)
    {
        getline(file, bookTitle);
        getline(file, authorName);
        getline(file, bookPublisher);
        getline(file, bookISBN);
        file >> bookPrice;
        file >> bookYear;
        file >> bookInStock;

        books[i].storeBook(bookTitle, authorName, bookPublisher, bookISBN, bookPrice, bookYear, bookInStock);

        books[i].displayBookInfo();
        numBooks++;
    }
    file.close();

}



//Function definition for showBooks. Loops through the 'books' array and displays info for all books to user.
void showBooks(BookClass books[], int numBooks)
{
    for (int i = 0; i < numBooks; i++)
    {
        books[i].displayBookInfo();
        cout << endl;
    }

}



//Function definition for showTitles. Similar to showBooks. Only displays the titles of the books to the user.
void showTitles(BookClass books[], int numBooks)
{
    for (int i = 0; i < numBooks; i++)
    {
        books[i].getTitle();
        cout << endl;
    }

}

Any help is appreciated. Sorry if any formatting or anything of that nature isn't done quite correctly. I'm very new around here. So any critique for the future is also welcome and appreciated!

  • 1
    Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please take the [tour](http://stackoverflow.com/tour) and read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Ahsan Apr 02 '17 at 18:37
  • Could you perhaps boil that down to a [SSCCE](http://sscce.org)? – Jesper Juhl Apr 02 '17 at 18:40
  • Removed most of the stuff that probably isn't need for you. Thanks for the quick reply. – thecoljohnrambo Apr 02 '17 at 18:50
  • `books[i].getTitle();` gets the title of the book and returns it as a `string`. Absolutely nothing in the code prints out the returned `string`. Perhaps try `cout << books[i].getTitle() << endl;` – user4581301 Apr 02 '17 at 18:55
  • added this (can't believe I missed that) but it still doesn't show anything on screen. Just displays the menu again. Is there an issue with the data from the file being loaded into the array? – thecoljohnrambo Apr 02 '17 at 18:58
  • When folk ask for SSCCE or MCVE, one of the Cs stands for Complete. What they want you to do is reduce your code to the minimal compiling example that demonstrates your problem. Why this is asked for is often not for us, but for you. The act of finding a minimal example forces you to examine your code and remove all that isn't the bug. It almost always helps you find the bug yourself. – user4581301 Apr 02 '17 at 18:59
  • `newBook.storeBook` looks a lot like you are storing the book in itself, and not placing the book into the array. Another important programming trick is to write a small amount of code, then test that the small amount of code works correctly. If you do not do this, then you have A) code that was based on the code that doesn't work and now probably has to be changed as well and B) a much larger amount of code that needs to be inspected to find the cause of the bug when you do find one. – user4581301 Apr 02 '17 at 19:03
  • @user4581301 Thank you very much for the info. That will definitely be remembered for next time (I feel I've butchered this post already). I'll try cutting things down to the minimum and see if I can find anything. Greatly appreciated! – thecoljohnrambo Apr 02 '17 at 19:05
  • Missed C) bugs feed on and hide one another. Fixing one bug may reveal more bugs leaving you uncertain whether or not you fixed the bug, leading to [shotgun debugging](https://en.wikipedia.org/wiki/Shotgun_debugging). – user4581301 Apr 02 '17 at 19:07
  • 1
    It's becoming very clear to me that I need to find a new way to plan out and write these programs. The method I was using in my first semester just isn't cutting it anymore as the programs become more complex. I understand that I'm still very new to this, but wow... I feel like a complete newbie all over again. – thecoljohnrambo Apr 02 '17 at 19:11
  • If you used a `std::vector` you wouldn't have to set a max array size, or pass `numBooks` around. A vector knows its own size. For the `getBooks` function you should also consider [Why does std::getline skip input after a formatted extraction?](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – Bo Persson Apr 02 '17 at 21:12

0 Answers0