Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 305397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:22:11+00:00 2026-05-12T07:22:11+00:00

I’m trying to input a form of data validation in which when a user

  • 0

I’m trying to input a form of data validation in which when a user enter’s a book’s ISBN number, if it has already been stored then it will output an error message. However, I’m having trouble doing this. I’m not sure if I’m overloading the == operator correctly, and I’m not sure how to compare the vector values in the store_ISBN() function.

Here is the code:

#include "std_lib_facilities.h"

// Classes ---------------------------------------------------------------------

class Book{
public:
       vector<Book> books; // stores book information
       Book() {}; // constructor
       friend ostream& operator<<(ostream& out, const Book& b);
       bool operator==(const Book& d);
       string what_title();
       string what_author();
       int what_copyright();
       void store_ISBN();
       void is_checkout();
private:
        char check;
        int ISBNfirst, ISBNsecond, ISBNthird;
        char ISBNlast;
        string title;
        string author;
        int copyright;
};

// Class Functions -------------------------------------------------------------

string Book::what_title()
{
       cout << "Title: ";
       getline(cin,title);
       cout << endl;
       return title;
}

string Book::what_author()
{
       cout << "Author: ";
       getline(cin,author);
       cout << endl;
       return author;
}

int Book::what_copyright()
{
    cout << "Copyright Year: ";
    cin >> copyright;
    cout << endl;
    return copyright;
}

void Book::store_ISBN()
{
     bool test = false;
     cout << "Enter ISBN number separated by spaces: ";
     while(!test){
     cin >> ISBNfirst >> ISBNsecond >> ISBNthird >> ISBNlast;
     for(int i = 0; i < books.size(); ++i)
             if(ISBNfirst == books[i]) cout << "test"; // no idea how to implement this line            
     if((ISBNfirst<0 || ISBNfirst>9) || (ISBNsecond<0 || ISBNsecond>9) || (ISBNthird<0 || ISBNthird>9))
                   error("Invalid entry.");
     else if(!isdigit(ISBNlast) && !isalpha(ISBNlast))
          error("Invalid entry.");
     else test = true;}     
     cout << endl;
}

void Book::is_checkout()
{
     bool test = false;
     cout << "Checked out?(Y or N): ";
     while(!test){
     cin >> check;
     if(check == 'Y') test = true;
     else if(check == 'N') test = true;                                
     else error("Invalid value.");}
     cout << endl;
}

// Operator Overloading --------------------------------------------------------

bool Book::operator==(const Book& d){ // is this right???
     if((ISBNfirst == d.ISBNfirst) && (ISBNsecond == d.ISBNsecond) 
             && (ISBNthird == d.ISBNthird) && (ISBNlast == d.ISBNlast)) return true;
     else return false;
}

ostream& operator<<(ostream& out, const Book& b){
         out << "Title: " << b.title << endl;
         out << "Author: " << b.author << endl;
         out << "ISBN: " << b.ISBNfirst << "-" << b.ISBNsecond << "-" << b.ISBNthird << "-" << b.ISBNlast << endl;
         out << endl;
         return out;
}

// Main ------------------------------------------------------------------------     

int main()
{
    Book store;
    string question;
    while(true){
        store.what_title();
        store.what_author();
        store.what_copyright();
        store.store_ISBN();
        store.is_checkout();
        store.books.push_back(store);
        cout << "Are you finished?(Y or N): ";
        cin >> question;
        if(question == "Y") break;
        else if(question == "N"){
              cout << endl;
              cin.ignore();}
        else error("Invalid value.");
        }
    cout << endl;
    cout << "Books stored -\n" << endl;
    for(int i = 0; i < store.books.size(); ++i)
            cout << store.books[i];
    keep_window_open();
}

Note that in the store_ISBN function I’ve only included testing for one variable since I don’t want to type out the whole thing before I figure out how to do it.

As you can see each time a book passes through the loop in main, the data for that book is stored. I’m then able to output all the data input after the loop by overloading the << operator to print Title, Author, and ISBN. So I think I should be able to access that individual data in the vector to compare to the user input ISBN, but I don’t know how. The parts that I am confused about have been commented as such.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:22:11+00:00Added an answer on May 12, 2026 at 7:22 am

    I’m not sure quite what the user is expected to type for an ISBN.

    Reading from a stream into an int will read digits up to a space, and convert the result to int (if all goes well, anyway). Reading into a char will store the char value. So at the moment you’re validating than an ISBN looks like three single digits (0-9), and then the next char. That’s not what I think an ISBN looks like.

    Your operator== looks OK, although note that for a bool return value,

    if (X) return true;
    else return false;
    

    can be replaced with

    return X;
    

    because conditionals are already of type bool.

    After setting your ISBN values (and any other fields you plan to use in operator==, if it’s not finished yet), the way to look for a matching book in the store is:

    for(int i = 0; i < books.size(); ++i)
        if(*this == books[i]) cout << "test";
    

    In other words, look for a book equal to this book. Or you could use std::find from <algorithms>, although in this case it wouldn’t really be any more concise.

    By the way, it is unusual to use the same class (Book) to represent both a single book, and the whole store. Unpicking that is a fairly complex set of changes and decisions, though, so I’ll just say that a class should represent a single kind of thing, and an object of the class represent an example of that kind. So normally Book and Bookstore are different kinds of thing. The vector in Book is an instance variable, meaning that every Book has its own vector of Books. That doesn’t really make sense.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 149k
  • Answers 149k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Create a partial class and add the property Children (or… May 12, 2026 at 9:37 am
  • Editorial Team
    Editorial Team added an answer I can't see the details of your stored proc, but… May 12, 2026 at 9:37 am
  • Editorial Team
    Editorial Team added an answer You should replace double quote with This code 9&quot; x… May 12, 2026 at 9:37 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.