#include <iostream>
#include <string>
#include <iomanip>
#include "mainmenu.h"
using namespace std;

string bookTitle[20] = {""};
string isbn[20] = {""};
string author[20] = {""};
string publisher[20] = {""};
string dateAdded[20] = {""};
int qtyOnHand[20] = {0};
double wholesale[20] = {0.0};
double retail[20] = {0.0};
string searchTitle;

int main()
{
    // Declarations for function
    int choice;

    // Constants for menu choices
    const int Cashier_Module = 1,
              Inventory_Database_Module = 2,
              Report_Module = 3,
              Exit = 4; 
    do
    {
    // Read input
    cout << "        Serendipity Booksellers\n";
    cout << "              Main Menu\n\n";
    cout << "       1. Cashier Module\n"; 
    cout << "       2. Inventory Database Module\n";
    cout << "       3. Book Information Module\n";
    cout << "       4. Report Module\n";
    cout << "       5. Exit\n\n";
    cout << "       Enter Your Choice: ";
    cin >> choice; 
    cout << "\n"; 

    // Choice validation
    switch (choice)
    {
    case 1: cashier();
            break;
    case 2: invMenu();
            break;
    case 3: bookInfo(isbn, bookTitle, author, publisher, dateAdded, qtyOnHand, wholesale, retail);
            break; 
    case 4: reports();
            break;
    case 5: cout << "   You have chosen to end the program.\n";
            break; 
    default: cout << " Please enter a number in the range of 1-5.\n";
    } 
        cout << endl;

    } while (choice != 5);

        cout << "\n";

        return 0;
} 

void cashier()
{ 
    // Declarations for function
    string date;
    string isbn;
    string title;  
    int quantity, answer;
    float price_of_book, subtotal, tax, total, percent_of_tax;


    do
    { 
    // Read input
    cout << " Please fill in the information below:\n\n";
    cout << " Please enter the date in the form (MM/DD/YYYY): ";
    cin >> date;
    cout << " Enter the ISBN of the book in the form (X-XXX-XXXXX-X): ";
    cin >> isbn;
    cin.ignore(); // Skips to the next character so I can enter the title
                  // *Need to use this before a getline so i can enter the next statement*
    cout << " Enter the title of the book: ";
    getline(cin, title); // Allows me to enter a name with spaces
    cout << " Enter the number of books: ";
    cin >> quantity;
    cout << " Enter the price of the book: ";
    cin >> price_of_book;
    cout << " Enter the percent of tax: ";
    cin >> percent_of_tax;
    cout << "\n\n\n";

    // Calculations
    subtotal = quantity * price_of_book;
    tax = subtotal * percent_of_tax;
    total = subtotal + tax;

    // Display Results
    cout << setprecision(2) << fixed << showpoint;
    cout << " Serendipity Book Sellers\n\n\n";
    cout << " Date: " << date << endl;
    cout << "\n";
    cout << " Qty    ISBN            Title                Price        Total\n";
    cout << " ________________________________________________________________\n";
    cout << setw(3) << quantity << setw(15) << isbn << setw(13) << title << setw(15) << "$ " << price_of_book <<
            setw(9) << "$ " << subtotal << endl; 
    cout << "\n\n\n";
    cout << "             Subtotal                                    $ " << subtotal << endl;
    cout << "             Tax                                         $ " << tax << endl;
    cout << "             Total                                       $ " << total << endl;
    cout << "\n\n";
    cout << "Would you like to make another transaction?" << endl;
    cout << "(1 = Yes & 2 = No): ";
    cin >> answer;
    cout << "\n";
    } while(answer == 1);

    while (answer == 2)
    { 
        cout << "Thank You for Shopping at Serendipity!\n\n";
        break;
    }
} 

void invMenu()
{
    // Declarations for function
    int choice;

    // Constants for menu choices
    const int Look_up_a_book = 1,
              Add_a_book = 2,
              Edit_a_book_record = 3,
              Delete_a_book = 4,
              Return_to_main_menu = 5;
    do
    {
    // Read input
    cout << "                       Serendipity Booksellers\n";
    cout << "                         Inventory Database\n\n";
    cout << "                      1. Look Up a Book\n";
    cout << "                      2. Add a Book\n";
    cout << "                      3. Edit a Book's Record\n";
    cout << "                      4. Delete a Book\n";
    cout << "                      5. Return to the Main Menu\n\n";
    cout << "                      Enter Your Choice: ";
    cin >> choice;
    cout << "\n";

    // Choice validation
    switch (choice)
    {
    case 1: lookUpBook();
            break;
    case 2: addBook();
            break;
    case 3: editBook();
            break;
    case 4: deleteBook();
            break;
    case 5: cout << "            You have chosen to return to the Main Menu.\n";
            break;
    default: cout << "              Please enter a number in the range 1-5.\n";
    } 
        cout << endl; 

    } while (choice != 5);

        cout << "\n";
} 

void bookInfo(string bookTitle[], string isbn[], string author[], string publisher[], string dateAdded[], int qtyOnHand[], double wholesale[], double retail[])
{
    for (int i = 0; i < 20; i++)
    {

        // Read input
        cout << "                   Serendipity Booksellers\n";
        cout << "                       Book Information\n\n\n";
        cout << "        Title: " << bookTitle[i] << endl;
        cout << "        ISBN: " << isbn[i] << endl; 
        cout << "        Author: " << author[i] << endl;
        cout << "        Publisher: " << publisher[i] << endl;
        cout << "        Date Added: " << dateAdded[i] << endl;
        cout << "        Quantity-On-Hand: " << qtyOnHand[i] << endl;
        cout << "        Wholesale Cost: " << wholesale[i] << endl;
        cout << "        Retail Price: " << retail[i] << endl;

    }
} 

void reports()
{
    // Declarations for function
    int choice;

    // Constants for program
    const int Inventory_listing = 1,
              Inventory_wholesale = 2,
              Inventory_retail = 3,
              Listing_quantity = 4,
              Listing_cost = 5,
              Listing_age = 6,
              Main_menu = 7;

    do
    {
    // Read input
    cout << "                   Serendipity Booksellers\n";
    cout << "                           Reports\n\n";
    cout << "                 1. Inventory Listing\n";
    cout << "                 2. Inventory Wholesale Value\n";
    cout << "                 3. Inventory Retail Value\n";
    cout << "                 4. Listing by Quantity\n";
    cout << "                 5. Listing by Cost\n";
    cout << "                 6. Listing by Age\n";
    cout << "                 7. Return to Main Menu\n\n";
    cout << "                 Enter Your Choice: ";
    cin >> choice;
    cout << "\n";


    switch (choice)
    {
    case 1: repListing();
            break;
    case 2: repWholesale();
            break;
    case 3: repRetail();
            break;
    case 4: repQty();
            break;
    case 5: repCost();
            break;
    case 6: repAge();
            break;
    case 7: cout << "          You have chosen to return to the Main Menu.\n";
            break;
    default: cout << "            Please enter a number in the range 1-7.\n";
    } 
        cout << endl;

    } while (choice != 7); 

        cout << "\n";
}

// *******************************
// Inventory Database Functions  *
// *******************************

void lookUpBook()
{
    cin.ignore();
    cout << "                      Please enter the title of the book: ";
    getline(cin, searchTitle);
    cout << "\n";

    for (int i = 0; i < 20; i++)
    {
        if (searchTitle == bookTitle[i])
        {
            bookInfo(bookTitle, isbn, author, publisher, dateAdded, qtyOnHand, wholesale, retail); 
            break;
        }
        else 
        {
            cout << "                      This book is not in the inventory.\n";
            break;
        }
    }
    cout << "\n"; 

}

void addBook()
{
    for (int i = 0; i < 20; i++)
    {
        if (bookTitle[i] == "")
        {
            cout << "        Please enter the information for the following:\n\n";
            cin.ignore(); 
            cout << "        Enter the title of the book: ";
            getline(cin, bookTitle[i]);
            cout << "        Enter the ISBN of the book in the form (X-XXX-XXXXX-X): ";
            cin >> isbn[i];
            cin.ignore();
            cout << "        Enter the name of the author: ";
            getline(cin, author[i]);
            cout << "        Enter the name of the publisher: ";
            getline(cin, publisher[i]);
            cout << "        Enter the date added, in the form (MM/DD/YYYY): ";
            cin >> dateAdded[i];
            cout << "        Enter the quantity of the book being added: ";
            cin >> qtyOnHand[i];
            cout << "        Enter the wholesale cost of the book: ";
            cin >> wholesale[i];
            cout << "        Enter the retail price of the book: ";
            cin >> retail[i];
            cout << "\n";
            cout << "        The book information has been successfully entered.\n\n";
            break;
        }

        else if (bookTitle[i] == bookTitle[20])
        {
            cout << "        No more books may be added to the inventory!\n\n";
            break;
        }
    }
}

void editBook()
{
    cout << "                    You selected Edit Book.\n";
}

void deleteBook()
{
    cout << "                    You selected Delete Book.\n";
}

// ************************
// Report Stub Functions  *
// ************************

void repListing()
{
    cout << "             You selected Inventory Listing.\n";
}

void repWholesale()
{
    cout << "             You selected Inventory Wholesale Value.\n";
}

void repRetail()
{
    cout << "             You selected Inventory Retail Value.\n";
}

void repQty()
{
    cout << "             You selected Listing By Quantity.\n";
}

void repCost()
{
    cout << "             You selected Listing By Cost.\n";
}

void repAge()
{
    cout << "             You selected Listing By Age.\n";
}

---------------------------------------------------------------------------------------------------------------

Hello,

Instructions:

It should ask the user for the title of a book. This is the book that is to be looked up in the
inventory database.

The function should search the bookTitle array for a title that matches the one
entered by the user. If no match is found, the function should display a message
indicating that the book is not in inventory, and terminate. If the book is found, the

function should call bookInfo, passing the correct data as arguments.

The problems I am having:

I have done this, but I'm having trouble with my for loops.

In the addBook() function there is a break statement at the bottom because "I only need to enter the information once", not 20 times, but "I used a counter so I could fit the information in each subscript".

(I found out that because of the break statement, when i come around again to add a book, the information won't be found because the memory is already taken up for that subscript, due to it resetting everytime it runs through.)

In the bookInfo(....) function it is displaying the data 20 times, but once again "I only need the data shown for the title entered". I'm just confused because without the counter, it won't show the data for each matching subscript.

I need the lookUpBook() function to display the information, only if it matches the title searched.

Help if you can, please, thank you for your time.

kimika
Deleted Member

I'm reading the function name. I see a function called addBook, not addBoooks. Hence I am expecting to one book, not many. I am hoping this is the intent. One book, not twenty. I am confused by your description of what is occurring. You are entering something 20 times, not once? What precisely is happening?

Here should be the general attack, I would imagine. The user wants to add a book. We have twenty slots open. A blank book title denotes a vacancy. We loop through the 20 slots. If a slot is taken, we gop to the next. If a slot is vacant, we ask the name of the book, ISBN, etc., then fill in the slot with that info, then the function ends. If ALL slots are taken, we cannot add the book.

Now let's look at your code.

void addBook()
{
    for (int i = 0; i < 20; i++)
    {
        if (bookTitle[i] == "")
        {
            cout << " Please enter the information for the following:\n\n";
            cin.ignore();
            out << " Enter the title of the book: ";
            getline(cin, bookTitle[i]);
            cout << " Enter the ISBN of the book in the form (X-XXX-XXXXX-X): ";
            cin >> isbn[i];
            cin.ignore();
            cout << " Enter the name of the author: ";
            getline(cin, author[i]);
            cout << " Enter the name of the publisher: ";
            getline(cin, publisher[i]);
            cout << " Enter the date added, in the form (MM/DD/YYYY): ";
            cin >> dateAdded[i];
            cout << " Enter the quantity of the book being added: ";
            cin >> qtyOnHand[i];
            cout << " Enter the wholesale cost of the book: ";
            cin >> wholesale[i];
            cout << " Enter the retail price of the book: ";
            cin >> retail[i];
            cout << "\n";
            cout << " The book information has been successfully entered.\n\n";
            break;
        }
        else if (bookTitle[i] == bookTitle[20])
        {
            cout << " No more books may be added to the inventory!\n\n";
            break;
        }
    }
}

This is a segmentation fault.

else if (bookTitle[i] == bookTitle[20])

There is no bookTitle[20]. 20 is an invalid loop index. Besides, you can simply test whether i is a certain value here. No need to compare book titles. Check indexes. If i is 19, then you have tried every single slot and all are full, so test whether i is 19.

That's one way of doing it. It's the one that requires the fewest adjustments to your code. There are some more elegant ways of doing it. One way is to replace the first "break" with a "return" and get rid of the "else if" statement. Place the cout << " No more books may be added to the inventory!\n\n"; line AFTER the end of the loop.

Also, this is an excellent candidate for a structure called "book" that contains the book name, ISBN, etc. You could then have a single array of size 20 rather than a whole bunch of them. However, if you are not familiar with structures or are not allowed to use them, don't use a structure. You will / should learn them in the near future and when you do, you'll see how much cleaner the code will look.

Edited 3 Years Ago by VernonDozier

"Here should be the general attack, I would imagine. The user wants to add a book. We have twenty slots open. A blank book title denotes a vacancy. We loop through the 20 slots. If a slot is taken, we gop to the next. If a slot is vacant, we ask the name of the book, ISBN, etc., then fill in the slot with that info, then the function ends. If ALL slots are taken, we cannot add the book."

This is my intent, sorry for the mixup!
How can i properly increment this, so that it goes to the next book?

  1. I need to increment "i" so that i can enter the information for the next book.

  2. I need to increment "i" so that the lookUpBook() can find the next book.

  3. I need to increment "i" so that the bookInfo() can display the next book.

This is me attempting to increment but i don't think it's right:

void addBook()
{
    for (int i = 0; i < 20; i++)
    {
        if (bookTitle[i] == "")
        {
            cout << "        Please enter the information for the following:\n\n";
            cin.ignore(); 
            cout << "        Enter the title of the book: ";
            getline(cin, bookTitle[i]);
            cout << "        Enter the ISBN of the book in the form (X-XXX-XXXXX-X): ";
            cin >> isbn[i];
            cin.ignore();
            cout << "        Enter the name of the author: ";
            getline(cin, author[i]);
            cout << "        Enter the name of the publisher: ";
            getline(cin, publisher[i]);
            cout << "        Enter the date added, in the form (MM/DD/YYYY): ";
            cin >> dateAdded[i];
            cout << "        Enter the quantity of the book being added: ";
            cin >> qtyOnHand[i];
            cout << "        Enter the wholesale cost of the book: ";
            cin >> wholesale[i];
            cout << "        Enter the retail price of the book: ";
            cin >> retail[i];
            cout << "\n";
            cout << "        The book information has been successfully entered.\n\n";
            i += 1; 
            break;  
        }

        if (bookTitle[i] == bookTitle[19])
        {
            cout << "           No more books may be added to the inventory!\n\n";
            break;
        }
    }   
}

Hince the i += 1; in line 28.

Line 28 does nothing. i is a local variable to the function addBook(). It goes out of scope before you return from the addBook() function. Replace lines 28 and 29 with the following line...

return;

This change will make no difference in the code. Hence you are confused about something. I am not entirely sure what you are trying to accomplish with line 28.

I generally ask the following questions, and they apply to this case.

  1. What is the input?
  2. What is the expected output?
  3. What is the actual output?
  4. How do the two differ?
This article has been dead for over six months. Start a new discussion instead.