//Example 2.2
//This program stores book details in a string array.
//The user can enter book details, display details or exit the program
//A number of book details can be entered up to MAX_ELEMENTS
// The / character is stored in the array Books after each book's details
// The // string indicates that there are no more books details in the array.

#include <iostream>
#include <string>

const int MAX_ELEMENTS = 200;

int main()
{

string Books[MAX_ELEMENTS];
string choice;                 // use a string to store the users choice
int j=0;                          // integer to array index the array

int numbooks=0;            // This variable is optional and is used only to output the number of books.
 

// print a menu of options for the user
cout << "choose one of the following options: "<<endl;
cout << "type I to input book details"<<endl;
cout << "type D to display book details"<<endl;
cout << "type q to quit"<<endl;

// read in the users choice
getline(cin,choice);
while(choice != "q" && choice != "Q") {          // while choice is not quit
    if(choice == "i" || choice == "I")                     // if input details is chosen
    {
            if( (j !=0) && ((MAX_ELEMENTS-j) < 5))          //if there is no space in the array for more details.
                                                          // 5 places are needed in the array for the last book detail.
                                                         // this includes the 3 details for one book, the "/" and the final "//".
                    cout<<"Maximum capacity of books: "<<endl;
            else{
                    cout << "Enter Book Details"<<endl;
                    cout << "Enter title"<<endl;
                    getline(cin,Books[j]);j++;                 //place book detail in array position books[j] 
                    cout << "Enter author"<<endl;         // and increment index j
                    getline(cin,Books[j]);j++;
                    cout << "Enter publisher"<<endl;
                    getline(cin,Books[j]);j++;
                    Books[j] = "/";j++;                         //place the string "/" in the array to indicate the end of
                                                                       // one book's details
                    numbooks++;                               // increment number of books
                   }         // end else
    }         // end if
    else
        if(choice == "d" || choice == "D")     // if choice is to display details
        {
            cout<<"The number of Books is: "<<numbooks<<endl;
            Books[j]="//";                 // place "//" in the array to indicate end of book details.
                                            // This is overwritten if more details are input
            j=0;                           // initialise the index to the first element
            while(Books[j] != "//"){         // while there are more books
                while(Books[j] != "/"){     // while not the end of one book detail
                       cout<<"Title: "<<Books[j]<<endl;j++;
                       cout<<"Author: "<<Books[j]<<endl;j++;
                       cout<<"Publisher: "<<Books[j]<<endl;j++;
                        }         // end while
                        cout<<endl;
                        j++;         // increment the array index
                    }         // end while
           }         // end if

        cout<<"Enter 'i' another detail or 'd' to display or 'q' to quit"<<endl;
        getline(cin,choice);
 } /*end while*/
}