Constructive Criticism Appreciated

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 Vukki 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
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
	 }




You can rework you code with use of simple structure
1
2
3
4
5
6
7
8
struct BookInfo
{
string ISBN;
string Title;
string Author;
int Cr_date;
char checked_in;
};

So you will need only one vector, and overall code would be more readable.

Again, you can encapsulate inside this structure function which test copyright date and author, instead of writing it two times.
Also you can put the code which search the vectors in another function.
@vukki,

Thank you for your suggestion. The chapter I am reading only touches on structs so I need to research that topic more. I don't yet know it well enough to use in a program.
A struct is exactly the same as a class, except that it has public as it's standard access modifier (instead of private in classes).
Topic archived. No new replies allowed.