Code:
struct book
{
long ID;
int numAvailable;
};
struct Library
{
book books[2];
//A Library "has a" group, here an array, of books. Simplistic use of composition, but illustrates use of a group of user defined class/struct in another class/struct
};
int main()
{
Library myLibrary; //declare a Library
//populate the Library
myLibrary.books[0].ID = 4365;
myLibrary.books[0].numAvailable = 1;
myLibrary.books[1].ID = 1111;
myLibrary.books[1].numAvailable = 2;
//have user request a book
cout << "which book number do you want?" << endl;
long inputID;
cin >> inputID;
int i;
bool found = false;
//check the Library to see if the book is there
for(i = 0; i < 2; ++i)
{
if(inputID == myLibrary.books[i].ID)
{
//the book is in the Library
found = true;
//tell how many copies are available
cout << "there are " << myLibrary.books[i].numAvailable << " copies of book # " << myLibrary.books[i].ID << " available." << endl;
//stop search
i = 3;
}
}
//if book not found in Library let user know
if(!found)
cout << "sorry, we don't have a book with ID number " << inputID << " available. Please check with your local university book store." << endl;
}