C++ - I need to be able to search for the book by title and isbn. //header file/
ID: 663368 • Letter: C
Question
C++ - I need to be able to search for the book by title and isbn.
//header file//
//header//
#include <string>
using namespace std;
//create booktype class//
class bookType
{
//constructor//
public:
//booktype function//
bookType ();
//function to set, get, compare title//
void settitle (string);
string gettitle ();
bool comptitle (string);
//function to set, show, update, get author//
void setauth (string = " ");
void showauth ();
void updateauth (string = " ");
string *getauth ();
//function to set, show, update, get book copies//
void setcop (int);
void showcop ();
void updatecop (int);
int getcop ();
//function to set, show, update, get book publisher//
void setpub (string);
void showpub ();
void updatepub (string);
string getpub ();
//function to set, show, update, get, compare book isbn//
void setisbn (string);
void showisbn ();
void updateisbn (string);
string getisbn ();
bool compareisbn (string);
//function to set, show, update, get book price//
void setprice (double);
void showprice ();
void updateprice (double);
double getprice ();
private:
//declare private variable name with data typr//
string title;
string auth [4];
string pub;
string isbn;
double price;
int cop;
int authno;
};
//implementation//
//header//
#include <iostream>
#include "bookType.h"
using namespace std;
//constructor//
bookType::bookType()
{
//declare variables//
title = " ";
//initialize author//
for (int i = 0; i < 4; i++)
auth[i] = " ";
pub = " ";
isbn = " ";
price = 0;
cop = 0;
authno = 0;
}
//method for set title//
void bookType::settitle(string mytitle)
{
//return title//
title = mytitle;
}
//method for get title//
string bookType::gettitle()
{
return title;
}
//method to compare title and the argument other title//
bool bookType::comptitle(string othertitle)
{
//return title//
return (title.compare(othertitle) == 0);
}
void bookType::setauth(string myauth)
{
//check author number//
authno = authno %4;
//compare values//
if (myauth.compare(" ") == 0)
return;
else
{
//get myauthor function//
auth[authno] = myauth;
authno++;
}
}
//display authors//
void bookType::showauth()
{
for (int i = 0; i < authno; i++)
cout << auth [i] << ", ";
cout << " ";
}
//update book author names//
void bookType::updateauth(string myauth)
{
//my author from set author function//
setauth(myauth);
}
string *bookType::getauth()
{
return auth;
}
//method for copies in books//
void bookType::setcop(int mycop)
{
//return the copies//
cop = mycop;
}
//method for display the copies//
void bookType::showcop()
{
cout << "The number of copies " << cop;
}
//update copies method//
void bookType::updatecop(int mycop)
{
cop = mycop;
}
//method to get copies//
int bookType::getcop()
{
return cop;
}
//method for publisher set//
void bookType::setpub(string mypub)
{
pub = mypub;
}
//display publisher from book//
void bookType::showpub()
{
cout << pub;
}
//update the publisher function//
void bookType::updatepub(string mypub)
{
pub = mypub;
}
//method to get publisher//
string bookType::getpub()
{
return pub;
}
//method to set ISBN//
void bookType::setisbn(string myisbn)
{
isbn = myisbn;
}
//display ISBN from book//
void bookType::showisbn()
{
cout << isbn;
}
//update ISBN value//
void bookType::updateisbn(string myisbn)
{
isbn = myisbn;
}
//get ISBN value//
string bookType::getisbn()
{
return isbn;
}
//evaluate ISBN number//
bool bookType::compareisbn(string myisbn)
{
return (myisbn.compare(isbn) == 0);
}
//method to set the book price//
void bookType::setprice(double myprice)
{
price = myprice;
}
//method to display price//
void bookType::showprice()
{
cout << "The book price is " << price;
}
//method to update the price value//
void bookType::updateprice(double myprice)
{
price = myprice;
}
//get the price value//
double bookType::getprice()
{
return price;
};
//header//
#include <iostream>
#include "bookType.h"
using namespace std;
//main//
int main()
{
cout << "Program that works with an ADT booktype" << endl;
//creates 100 book objects//
bookType mybooks[100];
string str;
double price;
int cop;
char choice;
int count = 0;
//set book properties//
do
{
//get book title//
cout << "Enter book title: " << endl;
cin >> str;
mybooks[count].settitle (str);
int j = 0;
do
{
cout << "Enter author name: " << endl;
cin >> str;
//get author name//
mybooks[count].setauth (str);
j++;
//get additional authors//
cout << "More authors (Y/N)? " << endl;
cin >> choice;
}
while (j < 4 && tolower (choice) != 'n');
cout << "Enter publisher: " << endl;
cin >> str;
//get the publisher//
mybooks[count].setpub (str);
cout << "Enter ISBN: " << endl;
cin >> str;
//get ISBN//
mybooks[count].setisbn(str);
cout << "Enter price: " << endl;
cin >> price;
//get the price value//
mybooks[count].setprice (price);
cout << "Enter copies: " << endl;
cin >> cop;
mybooks[count].setcop (cop);
count++;
cout << "Enter more books (Y/N)? " << endl;
cin >> choice;
//retrieve one or more books//
if (choice < 92)
choice += 32;
}
while (choice != 'n');
do
{
//print values//
cout << "Search for a book by its title: T" << endl;
cout << "Search for a book by its ISBN: I" << endl;
cout << "Update the number of copies: U" << endl;
cout << "Quit: Q" << endl;
cout << "Enter your choice: " << endl;
cin >> choice;
int i;
switch (tolower(choice))
{
case 'T' : cout << "Enter title to search: " ;
cin >> str;
//get the title of the book//
for (i = 0; i < count; i++)
//search book if existing or not from user entry//
if (mybooks[i].comptitle(str))
break;
if (i == count)
cout << "Book not found." ;
else
cout << "Book found." ;
break;
case 'I' : cout << "Enter ISBN to search: " ;
cin >> str;
//get ISBN value//
for (i = 0; i < count; i++)
//search ISBN if existing or not//
if (mybooks[i].compareisbn(str))
break;
if (i == count)
cout << "Book not found." << endl;
else
cout << "Book found." << endl;
break;
case 'U' : cout << "Enter title to search: " ;
cin >> str;
//get tje title//
for (i = 0; i < count; i++)
//search if title is existing or not//
if (mybooks[i].comptitle(str))
break;
if (i == count)
cout << "Book not found." << endl;
else
cout << "Enter the copies " << endl;
cin >> cop;
//update copy values//
mybooks[i].updatecop(cop);
break;
case 'Q':
return 0;
}
}
while (true);
return 0;
};
Explanation / Answer
Whts the issue with the code?
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.