Okay. Thanks for that.
For now, I've commented out the stringstream code to see first make sure I get everything else working first. I need to need to know how to make the functions is_string() and is_digit() and also what header files are required.
This is how the code is right now:
Code:
#include "../custom_std_lib_facilities.h"
#include <string>
#include <sstream>
using namespace std;
class Book
{
private:
string ISBN;
string title;
string author;
int copyright_date;
bool checked_in;
public:
Book(string ISBN, string title, string author, int copyright_date);
string isbn() const { return ISBN; }
string book_title() const { return title; }
string author_name() const { return author; }
int copy_date() const { return copyright_date; }
bool check() const { return checked_in; }
void check_book_in(bool checked_in);
void check_book_out(bool checked_in);
};
void printBook(Book& book);
int main()
{
Book book1{"0-15-204560-0", "Heir Apparent", "Vivian Vande Velde", 2002};
book1.check_book_in(book1.check());
Book book2{"0-375-82668-8", "Eragon", "Christopher Paolini", 2003};
book2.check_book_out(book2.check());
Book book3{"0-590-55356-9", "Sandry's Book", "Tamora Pierce", 1997};
book3.check_book_out(book3.check());
}
Book::Book(string isbn_number, string title_of_book, string name_of_author, int date)
{
if (!is_string(title))
{
error("error: invalid book title");
}
if (!is_string(author))
{
error("error: invalid author name");
}
if (!is_digit(copyright_date))
{
error("error: date must be a number (four digit year)");
}
/*stringstream ss(ISBN);
vector<string> isbn_v;
isbn_v.pushback(ISBN);
char valid = '-';
char invalid = ' ';
int printable = 33;
for (size_t i = 0; i < isbn_v.size(); i++)
{
}*/
ISBN = isbn_number;
title = title_of_book;
author = name_of_author;
copyright_date = date;
}
void Book::check_book_in(bool checked_in)
{
checked_in = true;
}
void Book::check_book_out(bool checked_in)
{
checked_in = false;
}
void printBook(Book& book)
{
cout << book.isbn() << "\n" << book.book_title() << "\n" << book.author_name() << "\n" << book.copy_date();
}
By the way, when Stroustrup asks to use an operator << to print the ISBN, title, author and copyright date on a line by themselves, does he meant to do it like how I did or does he mean to say I have to overload the operator and print them in a way that wouldn't work without overloading the operator? [It asks for that in the next question, though:
Add operators for the Book class. Have the == operator check whether the ISBN numbers are the same for two books. Have != also compare the ISBN numbers. Have a << print out the title, author, and ISBN on separate lines.
I'm going to need help with the boolean operators == and != either way, though. I still don't really get how to overload those correctly.]
For reference, when I try to compile the code, I get this:
||=== Build: Debug in book (compiler: GNU GCC Compiler) ===|
C:\Users\Osman\programming\stroustrup_programming_ using_c++\book\book.cpp||In constructor 'Book::Book(std::string, std::string, std::string, int)':|
C:\Users\Osman\programming\stroustrup_programming_ using_c++\book\book.cpp|40|error: 'is_string' was not declared in this scope|
C:\Users\Osman\programming\stroustrup_programming_ using_c++\book\book.cpp|44|error: 'is_string' was not declared in this scope|
C:\Users\Osman\programming\stroustrup_programming_ using_c++\book\book.cpp|48|error: 'is_digit' was not declared in this scope|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
And by the way, I'm using this code I found on another forum as reference for my ISBN-checking code and I need to see if I can change it a bit to make it work for me:
Code:
Book::Book(string ISBN) :isbn(ISBN) //will add other types later
{
//ISBN must be integer-integer-integer-digit/letter
stringstream ss(isbn);
char valid='-';
char invalid=' '; //ANYTHING other than '-' is invalid...
int printable=33;
for(int index=0; index<isbn.size(); index++) if(isbn[index]<printable) rpt_err("Invalid ISBN.\n"); //throws with whitespace
//check the first three sections for an integer followed by a '-'
for(int index=0; index<3; index++) {
int integer=-1;
char character=invalid; //both initialized for precaution
ss >> integer >> character;
if(integer<0 || character!=valid) rpt_err("Invalid ISBN.\n");
}
string final;
ss >> final; //check the remaining character(s)
if(final.size()!=1) rpt_err("Invalid ISBN.\n"); //only a single number/letter allowed
else if(!isdigit(final[0]) && !isalpha(final[0])) rpt_err("Invalid ISBN.\n"); //separate check to prevent range errors
}