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.
/*
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.
{
virtualconstchar* what() constthrow()
{
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
}
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.
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.