Thread: Book Program - Help Needed

  1. #16
    Registered User
    Join Date
    Mar 2015
    Posts
    384
    @Jig: Changing the state of a class member? I may have to study up on that. Do I do it in the constructor?

    Edit: How do I check the ISBN for validity? I have to check for if it's in the format "n-n-n-x", where n is a number and x is a digit or a letter, so how am I supposed to do that? I mean, like, do I use is_digit()? And how do I check for how it's been divided up by the hyphens?
    Last edited by Osman Zakir; 06-30-2015 at 02:50 PM.

  2. #17
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by whiteflags View Post
    *gets popcorn*
    If it's not kettlecorn, it's not edible.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Osman Zakir View Post
    @Jig: Changing the state of a class member? I may have to study up on that. Do I do it in the constructor?
    It just means writing to a class member. Any member function can do it.

    Code:
    struct foo
    {
        void bar() { m_bar = 10; } // Changes state of member variable (i.e. class member)
        int m_bar = 0;
    }
    
    int main()
    {
        foo myfoo;
        myfoo.bar();
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Mar 2015
    Posts
    384
    Oh, okay. I think I know how to do that, then.

    How do I check the ISBN for validity? I have to check for if it's in the format "n-n-n-x", where n is a number and x is a digit or a letter, so how am I supposed to do that? I mean, like, do I use is_digit()? And how do I check for how it's been divided up by the hyphens?

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    String streams if your book has covered that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Mar 2015
    Posts
    384
    Stroustrup's library access header has <sstream> in it. Is that what you mean?

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it is contained in that header.
    std::basic_stringstream - cppreference.com
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Registered User
    Join Date
    Mar 2015
    Posts
    384
    I'm checking for valid data in a Book in the constructor, and so far, I've got this:
    Code:
    Book::Book(string ISBN, string title, string author, int copyright_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)");
        }
        
        string isbn_number = ISBN, book_title = title, author_name = author;
        int date = copyright_date;
    }
    I'm having trouble understanding string streams, so I'll need an example usage code (if it's not too much trouble).

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    std::basic_stringstream::basic_stringstream - cppreference.com
    The key thing to remember is that when extracting integral types, it extracts data from the stream up to the first character that cannot be converted to the target type.

    // str = "123-456-789"
    str >> myint; // "Extracts 123"; leaves "-456-789".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Guest
    Guest
    Quote Originally Posted by Osman Zakir View Post
    I'm checking for valid data in a Book in the constructor, and so far, I've got this:
    Code:
    Book::Book(string ISBN, string title, string author, int copyright_date)
    {
        // (...)
        string isbn_number = ISBN, book_title = title, author_name = author;
        int date = copyright_date;
    }
    What do you think happens to the variables isbn_number and date which you are declaring here? Your choices often still seem like hit-and-miss. Read this here, I think it covers classes and members in a fairly concise manner.

  11. #26
    Registered User
    Join Date
    Mar 2015
    Posts
    384
    @Elysia: I'm not reading input from the user for the ISBN and those other things anymore, I'm just defining it myself in main():
    Code:
    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());
    }
    So should I check the ISBN after reading it in from the string stream? Or is there an alternative way of checking it?

    @Adrian: So declaring date and initializing it to the value in copyright_date won't do what I want?

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Osman Zakir View Post
    @Elysia: I'm not reading input from the user for the ISBN and those other things anymore, I'm just defining it myself in main():
    So should I check the ISBN after reading it in from the string stream? Or is there an alternative way of checking it?
    The point of a string stream is that it behaves as a stream, but it doesn't use a file or keyboard as its source. It uses a string as its source.

    std::string s = "123-456-789";
    std::stringstream sstr(s);
    int n;
    sstr >> n; // Extracts 123 from s.

    @Adrian: So declaring date and initializing it to the value in copyright_date won't do what I want?
    They're local variables, so what do you expect will happen?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Mar 2015
    Posts
    384
    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
    }
    Last edited by Osman Zakir; 06-30-2015 at 07:30 PM.

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Osman Zakir View Post
    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: 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.]
    Seems fine to me.

    For reference, when I try to compile the code, I get this:
    Yeah, you didn't declare or define those functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Registered User
    Join Date
    Mar 2015
    Posts
    384
    Where are they declared? That's what I'm asking. is_digit() should be in cctype and ctype.h, but how about is_string()?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Book Drill Question Help Needed
    By Osman Zakir in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2015, 09:33 PM
  2. Book recomendation needed
    By baxy in forum Programming Book and Product Reviews
    Replies: 1
    Last Post: 09-22-2013, 06:02 PM
  3. I need help with this program its from a C++ book
    By dark21 in forum C++ Programming
    Replies: 7
    Last Post: 03-27-2009, 03:30 AM
  4. Easy, simple programming book needed - HELP!
    By 3boyzmom in forum C Programming
    Replies: 1
    Last Post: 05-05-2008, 09:31 AM
  5. Address Book program
    By sundeeptuteja in forum C++ Programming
    Replies: 0
    Last Post: 07-28-2002, 02:08 AM