Me Again! This program is for exercise 5) of chapter 9 in Stroustrup's book.
It was a challenge for me. I gave up on trying to use

getline()

because it required substantial parsing of the line before I could use the input. I will add it when I am more proficient in that tool. I thank another poster again for his/her suggestions about the Book constructor. So here is the code. It is fairly long compared to most other examples I have seen here. Is that unusual? I snipped code to make it faster to read and follow.

/*
This program solves an exercise related to a writing software program for a library. The first requirement is to create a class 
holding vectors of book characteristics; ISBN, title, author, copyright date, and an indicator of whether a book is checked 
out.  The class should also hold functions that return a book characteristic when queried. Additional functions should 
perform input validation.
*/
      
#include "../../std_lib_facilities.h"
       
class TitleExcept: public exception    // Class for handling errors about the book's title.
{
virtual const char* what() const throw()
{
return "Please enter the book's title using only letters.\n";
}
} errortitle;

SNIP

class Book {
      private:
            vector<string>ISBNs;
            vector<string>Titles;
            vector<string>Authors;
            vector<int>CR_Dates;
            vector<char>Checked_In;

     public:
     Book()
  	  :Titles(3),
	   Authors(3),
	   CR_Dates(3),
	   ISBNs(3),
	   Checked_In(3)

	   {
	   Titles[0] = "CatintheHat";
	   Titles[1] = "CharlottesWeb";
           Titles[2] = "NineteenEightyfour";

	   Authors[0] = "DrSeuss";
	   Authors[1] = "EBWhite";
           Authors[2] = "GeorgeOrwell";

	   CR_Dates[0] = 1957;
	   CR_Dates[1] = 1952;
           CR_Dates[2] = 1949;

     	   ISBNs[0] = "0-2-6-8";
	   ISBNs[1] = "1-8-3-r";
           ISBNs[2] = "4-7-0-l";

	   Checked_In[0] = 'y';
	   Checked_In[1] = 'y';
           Checked_In[2] = 'n';
 	   }

     void Get_ISBN() 
     {
     string author = " ";
     int crdate = 1900;
     int flag = 0;
     int index = -1;
     cout <<"Please enter the author's name and the book's copyright date, separated by a space.\n";
     cin >> author >> crdate ;
		 
     if (crdate < 1900 || crdate > 2010) throw errorcrdate;  // Copyright dates of books are after 1900 up to the current year.
        for (int j = 0; j < author.length(); j++) {             // Verifies that the characters of the author's name are letters.
           if (isalpha(author[j])) {
	      continue;
	   } else {
	      throw errorauthor;
	   }
	}

	for (int i = 0; i < ISBNs.size(); i++) {
	   if (author == Authors[i] && crdate == CR_Dates[i]) {
	      flag = 1;
	      index = i;
	   }
	}
	if (flag == 1) {
	   cout << "The ISBN of the requested book is " << ISBNs[index] << ".\n";
	}
	if (flag == 0) {
	   cout << "There is no ISBN in our records corresponding to your author name and copyright date.\n";
	}
    }		     
	

    void Get_title() 
    {
    string author = " ";
    int crdate = 1900;
    int flag = 0;
    int index = -1;
    cout <<"Please enter the author's name and the book's copyright date, separated by a space.\n";
    cin >> author >> crdate ;
		 
    if (crdate < 1900 || crdate > 2010) throw errorcrdate;  // Copyright dates of books are after 1900 up to the current year.
       for (int j = 0; j < author.length(); j++) {             // Verifies that the characters of the author's name are letters.
          if (isalpha(author[j])) {
	     continue;
          } else {
	     throw errorauthor;
          }
       }

       for (int i = 0; i < Titles.size(); i++) {
          if (author == Authors[i] && crdate == CR_Dates[i]) {
             flag = 1;
	     index = i;
          }
       }
       if (flag == 1) {
          cout << "The title of the requested book is " << Titles[index] << ".\n";
       }
       if (flag == 0) {
          cout << "There is no title in our records corresponding to your author name and copyright date.\n";
       }
    }
SNIP
 };
       

     int main ()
 try {
	
	 Book Test;
	 char choice = ' ';
     
	 cout <<"Please select the task you wish to perform.  Enter the task's number below.\n";
	 cout <<" 1) Find a book title.\n 2) Find an author.\n 3) Find an ISBN.\n 4) Find a copyright date.\n 5) Find a check-out status.\n";
	 cin >> choice;

	 switch(choice) {
	 case '1':
		 Test.Get_title();
		 break;
	 case '2':
		 Test.Get_author();
		 break;
	 case '3':
		 Test.Get_ISBN();
		 break;
	 case '4':
		 Test.Get_crdate();
		 break;
	 case '5':
		 Test.Get_checked_in();
		 break;
	 default:
		 cout <<"Your choice could not be processed.  Please select again.\n";
	 }
		       
     keep_window_open();
     return 0;
	 }   
SNIP
	 }

Recommended Answers

All 3 Replies

First this code :

class TitleExcept: public exception    // Class for handling errors about the book's title.
{
virtual const char* what() const throw()
{
return "Please enter the book's title using only letters.\n";
}
} errortitle;

there are a few problems with it. The function what() should be public. Also, there is no need for the virtual unless you think someone would inherit from TitleExcept. And I think TitleExcept is a bad name. Its name is not descriptive enough. Perhaps something like InvalidTitleException or BadTitleFormatException would be better. One las thing about this code. There is no need for errortitle right now. Let the user use statements like : throw BadTitleFormatException rather than errortitle. As a general rule, try to scope everything tight as possible.

holding vectors of book characteristics; ISBN, title, author, copyright date, and an indicator of whether a book is checked

I think that means this :

struct BookCharacteristic{
 string Isbn;
 string title;
 string author;
 //...and so on
};

class Book{
 std::vector<BookCharacteristic> info;
 //...
};

instead of a vector of each characteristic. That way its easier to maintain, change,
and use, not to mention less error prone.

As for the rest of your code, I'll let someone else inspect that. I'm getting tired.

@firstperson,

Thank you for your suggestions. They were a little advanced for me. I realize I need to review the chapter on error and exception handling. The chapter I am reading only touches on structs, so I need to research that topic more deeply.

>>The chapter I am reading only touches on structs, so I need to research that topic more deeply

Let me clarify that for you. If you know classes then you know structs. classes are no
different than struct except for their default modifier. For example :

struct F{
 int x;
};
class G{
 int x;
};
//...
F f;
G g;

f.x = 10; //valid, x is public by default
g.x = 10; //error, x is private by default
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.