Hi, rezy3312
I would have write your code in another style, of which I give you an example below, but it doesn’t change a lot.
The issue, I think, is that you would like your class “Book” to perform operation which is not due to carry out.
Let me explain: “Book“ defines the ‘behaviour’ of a single book, while you are trying to add methods (= member functions) that aim to search amongst a collection of books.
It doesn’t make much sense to ask a single book to search inside a library, does it?
I think it could be simpler to separate the two concepts, book and library.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <vector>
#include "Book.h"
class Library {
public:
Library();
~Library();
void titleSearch(); // search inside vector books
void priceSearch();
void authorSeach();
void yearSearch();
private:
std::vector<Book> books;
};
|
Of course those methods are to be expunged by class “Book”.
Or, if you think of “Book” like a simple collection of proprieties, without methods, something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <string>
#include <vector>
struct Book {
double amt {0.0}; // book amount
std::string title; // book title
std::string author; // name of author
int year {0}; // year of the book
}
class Library {
public:
Library();
~Library();
void titleSearch(); // search inside vector books
void priceSearch();
void authorSeach();
void yearSearch();
Book getSingleBook(const std::string& title);
private:
std::vector<Book> books;
};
|
Or similar.
What follows is your code rewritten in a different style. It compiles and perform few basic, but, from my point of view, pointless actions.
Book.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#ifndef BOOK_H
#define BOOK_H
#include <string>
class Book
{
public:
Book() = default; // constructor assigning the info to variables
double getAmt() const; // return amt
void setAmt(double);
std::string getTitle() const; // return title
void setTitle(const std::string &);
std::string getAuth() const; // return author
void setAuth(const std::string &);
int getyear(); // return year
void setyear(int);
// swithc statments incase
void titleSearch();
void priceSearch();
void authorSeach();
void yearSearch();
private:
double amt {0.0}; // book amount
std::string title; // book title
std::string author; // name of author
int year {0}; // year of the book
};
#endif // BOOK_H
|
Book.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <iostream>
#include "Book.h"
void Book::setAmt(double am)
{ amt = am; }
double Book::getAmt() const
{ return amt; }
void Book::setTitle(const std::string& titl)
{ title = titl; }
std::string Book::getTitle() const
{ return title; }
void Book::setAuth(const std::string& aut)
{ author = aut; }
std::string Book::getAuth() const
{ return author; }
void Book::titleSearch()
{
std::cout << "you chosed to search by TITLE" << std::endl;
}
void Book::priceSearch()
{
std::cout << "you chosed to search by PRICE" << std::endl;
}
void Book::authorSeach()
{
std::cout << "you chosed to search by AUTHOR" << std::endl;
}
void Book::yearSearch()
{
std::cout << "you chosed to search by YEAR" << std::endl;
}
|
main.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include <iostream>
#include <limits>
#include "Book.h"
void PrintMenu();
int GetUserInput();
int main()
{
// usage
int userChoice = GetUserInput();
Book singlebook;
// switch statment for the users input
switch (userChoice)
{
case 1:
singlebook.titleSearch();
break;
case 2:
singlebook.priceSearch();
break;
case 3:
singlebook.authorSeach();
break;
case 4:
singlebook.yearSearch();
break;
case 5:
std::cout << "\n Thank you " << std::endl;
break;
}
std::cout << "\n\nPress ENTER to continue...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
void PrintMenu()
{
std::cout << "Welcome to sample library!" << std::endl;
std::cout << "What would you like to do? \n\n --------" << std::endl;
std::cout << "enter 1) if you wish to search by book titles"
<< std::endl;
std::cout << "enter 2) if you wish to search by book price "
"of the book " << std::endl;
std::cout << "enter 3) if you wish to search by book author"
<< std::endl;
std::cout << "enter 4) if you wish to search by book year"
<< std::endl;
std::cout << "enter 0) if you wish to exit the program" << std::endl;
}
int GetUserInput()
{
int choice {5};
while (choice <0 || choice >4 || !std::cin)
{
PrintMenu();
std::cin >> choice;
if (!std::cin)
{
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
std::cin.clear();
}
}
return choice;
}
|