Using C++, write a class BooksInfo

Using C++, write a class BooksInfo which has the following attributes:
Book_Title, Book_author(s), Book_publisher, noOfCopies, ISBN, price, and Publication_Year.
Add the following functions in your class
A. A default constructor
B. A constructor that sets all variables of the class
C. A copy constructor
D. Appropriate get and set functions to get and set all attributes
E. Functions to search the book by Book_Title and by ISBN
F. Function to show the number of copies in stock, set the number of copies in stock, update the number of
copies in stock, and return the number of copies in stock.
G. Function to modify the Book_author, Book_publisher, price
H. An operator overloading >= function to compare two books to display which book was recently published
I. An operator overloading == function to compare two books to display if the books were published in the
same year
J. A function to print the information of the class BooksInfo.
Write a program to test various operations on the objects of the class booksInfo. Make a record of 25 books using Arrays of type BooksInfo.
Last edited on

that my code but it is not working

#include<iostream>
#include <string>
#include <iomanip>

using namespace std;

const int nob = 25;

class BooksInfo
{
private:
string Book_Title;
string Book_author;
string Book_publisher;
int Number_Of_Copies;
long ISBN;
double price;
int Publication_Year;

public:
BooksInfo()
{
Book_Title = "";
Book_author = "";
Book_publisher = "";
Number_Of_Copies = 0;
ISBN = 0;
price = 0;
Publication_Year = 0;
}
BooksInfo(BooksInfo& BI)
{
Book_Title = BI.Book_Title;
Book_author = BI.Book_author;
Book_publisher = BI.Book_publisher;
Number_Of_Copies = BI.Number_Of_Copies;
ISBN = BI.ISBN;
price = BI.price;
Publication_Year = BI.Publication_Year;
}
void setBook_Title(string BT)
{
Book_Title = BT;
}
void setBook_author(string BA)
{
Book_author = BA;
}
void setBook_publisher(string BP)
{
Book_publisher = BP;
}
void setNumber_Of_Copies(int NOC)
{
Number_Of_Copies = NOC;
}
void setISBN(int IS)
{
ISBN = IS;
}
void setprice(double pr)
{
price = pr;
}
void setPublication_Year(int PY)
{
Publication_Year = PY;
}

string getBook_Title()
{
return Book_Title;
}
string getBook_author()
{
return Book_author;
}
string getBook_publisher()
{
return Book_publisher;
}
int getNumber_Of_Copies()
{
return Number_Of_Copies;
}
int getISBN()
{
return ISBN;
}
double getprice()
{
return price;
}
int getPublication_Year()
{
return Publication_Year;
}
void search()
{
string bt;
int is;
BooksInfo b[nob];
cout << "Please Enter the ISBN of the book you want to search for: "<< endl;
cin >> is;
cout <<"Please Enter the Book title you want to search for: " << endl;
cin >> bt;

for (int i=0;i<nob;i++)
{
if ((bt == b[i].Book_Title) && (is == b[i].ISBN))
{
cout << "The book's info you searched for is: " << endl;
cout << b[i].Book_Title << endl;
cout << b[i].Book_author << endl;
cout << b[i].Book_publisher << endl;
cout << b[i].Number_Of_Copies << endl;
cout << b[i].ISBN << endl;
cout << b[i].price << endl;
cout << b[i].Publication_Year << endl;
}
}
}

int copiesStock()
{
BooksInfo b[nob];
string bt;
int newC;
cout << "Enter the book title: ";
cin >> bt;
for (int i = 0; i < 25; i++)
{
if (bt == b[i].Book_Title)
{
cout << "Number of copies in stock: " ;
cin >> b[i].Number_Of_Copies;
cout << "Please Set the number of copies in stock: ";
cin >> newC;
Number_Of_Copies = Number_Of_Copies + newC;
cout << "\nUpdated the number of copies in stock: " << Number_Of_Copies;
setNumber_Of_Copies(Number_Of_Copies);
}
}
return Number_Of_Copies;

}
void modify()
{
string ba, bp;
double pri;
cout << "Enter the new name of the book author: ";
cin >> ba;
cout << endl;
cout << "Enter the new name of the book publisher: ";
cin >> bp;
cout << endl;
cout << "Enter the new price of the book: ";
cin >> pri;
Book_author = ba;
Book_publisher = bp;
price = pri;
}
void print()
{
int Book_Title,Book_author,Book_publisher,Number_Of_Copies,ISBN,price,Publication_Year;
cout << "Enter the Title: ";
cin >> Book_Title;
cout << "Enter the Author(s): ";
cin >> Book_author;
cout << "Enter the Publisher: ";
cin >> Book_publisher;
cout << "Enter the Number of copies: ";
cin >> Number_Of_Copies;
cout << "Enter the ISBN: ";
cin >> ISBN;
cout << "Enter the Price: ";
cin >> price;
cout << "Enter the Publication Year: ";
cin >> Publication_Year;

}

bool operator >= (BooksInfo b)
{
if (Publication_Year >= b.Publication_Year)
{
return true;
}
else
{
return false;
}
}

bool operator == (BooksInfo b)
{
if (Publication_Year == b.Publication_Year)
{
return true;
}
else
{
return false;
}
}
};
this the main:
int main()
{
string sb;
int sis=1, ch;
BooksInfo b[nob];
BooksInfo book1,book2,book3;

b[0].setBook_Title("Self Assessment in Action");
b[0].setBook_author("Betty McDonald");
b[0].setBook_publisher("Common Ground Publishing");
b[0].setNumber_Of_Copies(100);
b[0].setISBN(9781863);
b[0].setprice(1468);
b[0].setPublication_Year(2009);

b[1].setBook_Title("CAT AND MOUSE");
b[1].setBook_author("JAMES PATTERSON");
b[1].setBook_publisher("Little, Brown and Company");
b[1].setNumber_Of_Copies(200);
b[1].setISBN(9780755);
b[1].setprice(57.4);
b[1].setPublication_Year(1997);

b[2].setBook_Title("THE DA VINCI CODE");
b[2].setBook_author("DAN BROWN");
b[2].setBook_publisher("Transworld & Bantam Books");
b[2].setNumber_Of_Copies(300);
b[2].setISBN(9780552);
b[2].setprice(35.5);
b[2].setPublication_Year(2003);

b[3].setBook_Title("DIE TRYING");
b[3].setBook_author("LEE CHILD");
b[3].setBook_publisher("‎Putnam");
b[3].setNumber_Of_Copies(400);
b[3].setISBN(9780857);
b[3].setprice(45.7);
b[3].setPublication_Year(1998);

b[4].setBook_Title("THE ONE MAN");
b[4].setBook_author("Andrew Gross");
b[4].setBook_publisher("Pan MacMillan");
b[4].setNumber_Of_Copies(500);
b[4].setISBN(9781509);
b[4].setprice(77.2);
b[4].setPublication_Year(2017);

b[5].setBook_Title("Fingersmith");
b[5].setBook_author("Sarah Waters");
b[5].setBook_publisher("Virago Press");
b[5].setNumber_Of_Copies(600);
b[5].setISBN(1860498825);
b[5].setprice(83.47);
b[5].setPublication_Year(2002);

b[6].setBook_Title("Reckless");
b[6].setBook_author("Andrew Gross");
b[6].setBook_publisher("William Morrow an Imprint of Harper Collins");
b[6].setNumber_Of_Copies(700);
b[6].setISBN(9780061);
b[6].setprice(40);
b[6].setPublication_Year(2009);

b[7].setBook_Title("The Gangster");
b[7].setBook_author("Clive Cussler & Justin Scott");
b[7].setBook_publisher("G. P. Putnam's Sons ");
b[7].setNumber_Of_Copies(800);
b[7].setISBN(9780399);
b[7].setprice(26.42);
b[7].setPublication_Year(2016);

b[8].setBook_Title("EVIL GAMES");
b[8].setBook_author("Angela Marsons");
b[8].setBook_publisher("Bookouture");
b[8].setNumber_Of_Copies(900);
b[8].setISBN(9781909);
b[8].setprice(71.65);
b[8].setPublication_Year(2015);

b[9].setBook_Title("They Both Die at the End");
b[9].setBook_author("Adam Silvera");
b[9].setBook_publisher("Simon & Schuster Ltd");
b[9].setNumber_Of_Copies(1000);
b[9].setISBN(9781471);
b[9].setprice(76.03);
b[9].setPublication_Year(2017);

b[10].setBook_Title("Shadow and Bone");
b[10].setBook_author("Leigh Bardugo");
b[10].setBook_publisher("Hachette Children's Group");
b[10].setNumber_Of_Copies(1100);
b[10].setISBN(9781510);
b[10].setprice(68.15);
b[10].setPublication_Year(2018);

b[11].setBook_Title("History Is All You Left Me");
b[11].setBook_author("Adam Silvera");
b[11].setBook_publisher("Simon & Schuster Ltd");
b[11].setNumber_Of_Copies(1200);
b[11].setISBN(9781471);
b[11].setprice(80.94);
b[11].setPublication_Year(2017);

b[12].setBook_Title("How the King of Elfhame Learned to Hate Stories ");
b[12].setBook_author("Holly Black");
b[12].setBook_publisher("Hot Key Books");
b[12].setNumber_Of_Copies(1300);
b[12].setISBN(9781471);
b[12].setprice(102.88);
b[12].setPublication_Year(2033);

b[13].setBook_Title("The Invisible Life of Addie LaRue");
b[13].setBook_author("V.E. Schwab");
b[13].setBook_publisher("Titan Books Ltd");
b[13].setNumber_Of_Copies(1400);
b[13].setISBN(978178);
b[13].setprice(116.91);
b[13].setPublication_Year(2020);

b[14].setBook_Title("Circe");
b[14].setBook_author("Madeline Miller");
b[14].setBook_publisher("Bloomsbury Publishing PLC");
b[14].setNumber_Of_Copies(1500);
b[14].setISBN(9781408);
b[14].setprice(91.07);
b[14].setPublication_Year(2019);

b[15].setBook_Title("The Psychology of Money");
b[15].setBook_author("Morgan Housel");
b[15].setBook_publisher("Harriman House Publishing");
b[15].setNumber_Of_Copies(1600);
b[15].setISBN(97808);
b[15].setprice(81.65);
b[15].setPublication_Year(2020);

b[16].setBook_Title("Shadow and Bone: Siege and Storm ");
b[16].setBook_author("Leigh Bardugo");
b[16].setBook_publisher("Hachette Children's Group");
b[16].setNumber_Of_Copies(1700);
b[16].setISBN(9781);
b[16].setprice(69.99);
b[16].setPublication_Year(2018);

b[17].setBook_Title("Crooked Kingdom Collector's Edition");
b[17].setBook_author("Leigh Bardugo");
b[17].setBook_publisher(" Hachette Children's Group");
b[17].setNumber_Of_Copies(1800);
b[17].setISBN(978151);
b[17].setprice(106.48);
b[17].setPublication_Year(2019);

b[18].setBook_Title("The Choice");
b[18].setBook_author("Edith Eger");
b[18].setBook_publisher(" Ebury Publishing");
b[18].setNumber_Of_Copies(1900);
b[18].setISBN(978184);
b[18].setprice(108.68);
b[18].setPublication_Year(2019);

b[19].setBook_Title("The Cruel Prince ");
b[19].setBook_author("Holly Black");
b[19].setBook_publisher("Hot Key Books");
b[19].setNumber_Of_Copies(2000);
b[19].setISBN(97814);
b[19].setprice(91.07);
b[19].setPublication_Year(2018);

b[20].setBook_Title("Red Queen");
b[20].setBook_author("Victoria Aveyard");
b[20].setBook_publisher("Orion Publishing Co");
b[20].setNumber_Of_Copies(2100);
b[20].setISBN(978140);
b[20].setprice(67.51);
b[20].setPublication_Year(2015);

b[21].setBook_Title("One Of Us Is Lying");
b[21].setBook_author("Karen M. McManus");
b[21].setBook_publisher("Penguin Random House Children's UK");
b[21].setNumber_Of_Copies(2200);
b[21].setISBN(97801);
b[21].setprice(80.94);
b[21].setPublication_Year(2020);

b[22].setBook_Title("The Code of the Extraordinary Mind");
b[22].setBook_author("Vishen Lakhiani");
b[22].setBook_publisher("Potter/Ten Speed/Harmony/Rodale");
b[22].setNumber_Of_Copies(2300);
b[22].setISBN(97805);
b[22].setprice(86.46);
b[22].setPublication_Year(2019);

b[23].setBook_Title("The Boy, The Mole, The Fox and The Horse");
b[23].setBook_author("Charlie Mackesy");
b[23].setBook_publisher(" Ebury Publishing");
b[23].setNumber_Of_Copies(2400);
b[23].setISBN(97815);
b[23].setprice(153.5);
b[23].setPublication_Year(2019);

b[24].setBook_Title("The Order of Time");
b[24].setBook_author("Carlo Rovelli");
b[24].setBook_publisher("Penguin Books Ltd");
b[24].setNumber_Of_Copies(2500);
b[24].setISBN(978014);
b[24].setprice(91.07);
b[24].setPublication_Year(2020);

cout << "\n Book Information ";
cout << "\n 1.search the book by Book Title and by ISBN " << endl;
cout << "\n 2.show the number of copies in stock, set the number of copies in stock, update the number of copies in stock " << endl;
cout << "\n 3.Modifyin \n\n 4. print the information of the books \n\n 5. comparenting based on Publication Year \n\n 6.Exit";
cout << "\n Enter Your Choice:";
cin >> ch;

switch (ch)
{
case 1:
book1.search();
break;
case 2:
book1.copiesStock();
break;
case 3:
book1.modify();
break;
case 4:
for (int i = 0; i < nob; i++)
{
b[i].print();
cout << endl;
}
break;
case 5:
cout << "Enter first book" << endl;
book2.print();
cout <<endl;
cout << "Enter second book" << endl;
book3.print();


if (book2 >= book3)
{
cout << book2.getBook_Title() << " was recently published" << endl;
}
else
{
cout << book3.getBook_Title() << " was recently published" << endl;
}
if (book2 == book3)
{
cout << book2.getBook_Title() << " and " << book3.getBook_Title() << " were published on same year" << endl;
}
else
{
cout << book2.getBook_Title() << " and " << book3.getBook_Title() << " were NOT published on same year" << endl;
}

cout << endl;
case 6:
exit(EXIT_SUCCESS);
break;
cout << "\n Wrong Choice";
break;
default:
cout << "\n wrong choice";

}
Hello mar1972,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



That is a lot of code to go through. Exactly what part is not working and what is it not doing.

Just presenting the instructions and the code does not help when one does not know where to start.

Andy
Hello mar1972,

Just glancing at you code am I to understand that all this code is in 1 file?

Andy
As a first pass through the code to get something that compiles and runs and can be built upon, consider:

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <string>
//#include <iomanip>

using namespace std;

const size_t nob {25};

class BooksInfo
{
private:
	string Book_Title;
	string Book_author;
	string Book_publisher;
	int Number_Of_Copies {};
	long ISBN {};
	double price {};
	int Publication_Year {};

public:
	BooksInfo() {}

	BooksInfo(const string& bt, const string& ba, const string& bp, int noc, long ISB, double pr, int py) :
		Book_Title(bt), Book_author(ba), Book_publisher(bp), Number_Of_Copies(noc), ISBN(ISB), price(pr), Publication_Year(py) {}

	BooksInfo(const BooksInfo& BI) : Book_Title(BI.Book_Title), Book_author(BI.Book_author), Book_publisher(BI.Book_publisher),
		Number_Of_Copies(BI.Number_Of_Copies), ISBN(BI.ISBN), price(BI.price), Publication_Year(BI.Publication_Year) {}

	void setBook_Title(const string& BT) { Book_Title = BT; }
	void setBook_author(const string& BA) { Book_author = BA; }
	void setBook_publisher(const string& BP) { 	Book_publisher = BP; }
	void setNumber_Of_Copies(int NOC) { Number_Of_Copies = NOC; }
	void setISBN(int IS) { 	ISBN = IS; }
	void setprice(double pr) { 	price = pr; }
	void setPublication_Year(int PY) { Publication_Year = PY; }

	string getBook_Title() const { 	return Book_Title; }
	string getBook_author() const { return Book_author; }
	string getBook_publisher() const { return Book_publisher; }
	int getNumber_Of_Copies() const { return Number_Of_Copies; }
	int getISBN() const { return ISBN; }
	double getprice() const { return price; }
	int getPublication_Year() const { return Publication_Year; }

	bool operator >= (const BooksInfo& b)
	{
		return Publication_Year >= b.Publication_Year;
	}

	bool operator == (BooksInfo b)
	{
		return Publication_Year == b.Publication_Year;
	}
};

ostream& operator<<(ostream& os, const BooksInfo bi)
{
	if (!bi.getBook_Title().empty()) {
		os << bi.getBook_Title() << '\n';
		os << bi.getBook_author() << '\n';
		os << bi.getBook_publisher() << '\n';
		os << bi.getNumber_Of_Copies() << '\n';
		os << bi.getISBN() << '\n';
		os << bi.getprice() << '\n';
		os << bi.getPublication_Year() << '\n';
	}

	return os;
}

using Books = BooksInfo[nob];

void search(const Books b)
{
	string bt;
	int is {};

	cout << "Please Enter the ISBN of the book you want to search for: ";
	cin >> is;

	cout << "Please Enter the Book title you want to search for: ";
	getline(cin >> ws, bt);

	for (size_t i = 0; i < nob; ++i)
		if ((bt == b[i].getBook_Title()) && (is == b[i].getISBN())) {
			cout << "The book's info you searched for is:\n";
			cout << b[i] << '\n';
			return;
		}

	cout << "Not found\n";
}

void copiesStock(Books b)
{
	string bt;
	int newC {};

	cout << "Enter the book title: ";
	getline(cin >> ws, bt);

	for (size_t i = 0; i < nob; ++i)
		if (bt == b[i].getBook_Title()) {
			cout << "Number of copies in stock: ";
			cout << b[i].getNumber_Of_Copies();

			cout << "\nPlease Set the number of copies in stock: ";
			cin >> newC;

			b[i].setNumber_Of_Copies(newC);

			cout << "\nUpdated the number of copies in stock: " << b[i].getNumber_Of_Copies() << '\n';
			return;
		}

	cout << "Not found\n";
}

void modify(Books b)
{
	// TO BE COMPLETED
}

int main()
{
	Books b {{"Self Assessment in Action", "Betty McDonald", "Common Ground Publishing", 100, 9781863, 1468, 2009},
		{"CAT AND MOUSE", "JAMES PATTERSON", "Little, Brown and Company", 200, 9780755, 57.4, 1997},
		{ "THE DA VINCI CODE", "DAN BROWN", "Transworld & Bantam Books", 300, 780552, 35.5, 2003 }};

	for (bool again {true}; again; ) {
		int  ch {};

		cout << "\nBook Information\n";
		cout << "\n1. search the book by Book Title and by ISBN";
		cout << "\n2. show the number of copies in stock, set the number of copies in stock, update the number of copies in stock";
		cout << "\n3. Modify";
		cout << "\n4. print the information of the books";
		cout << "\n5. compare based on Publication Year";
		cout << "\n6. Exit";
		cout << "\n\nEnter Your Choice: ";
		cin >> ch;

		switch (ch)
		{
			case 1:
				search(b);
				break;

			case 2:
				copiesStock(b);
				break;

			case 3:
				modify(b);
				break;

			case 4:
				for (int i = 0; i < nob; i++)
					if (!b[i].getBook_Title().empty())
						cout << b[i] << '\n';

				break;

			case 5:
				cout << "To do\n";
				break;

			case 6:
				again = false;
				break;

			default:
				cout << "\n wrong choice";
				break;
		}
	}
}

Hello mar1972,

The first thing I found is "main" was missing a closing }. Then in line:b[3].setBook_publisher("Putnam"); there were 3 characters that would not display in my IDE, but are enough to cause an error when compiled.

Your menu is nice, but hard to read as option 2 wraps my screen in the middle of a word. You do not need a cout statement for each line just 1 will do. Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout <<
    "\n" <<
    std::string(22, ' ') <<
    "Book Information Menu\n" <<
    std::string(64, '-') <<
    "\n\n"
    " 1. search the book by Book Title and by ISBN\n"
    " 2. show the number of copies in stock, set the number of copies\n"
    "    in stock, update the number of copies in stock\n"
    " 3. Modifyin\n"
    " 4. print the information of the books\n"
    " 5. comparenting based on Publication Year\n"
    " 6. Exit\n"
    "  Enter Your Choice: ";
cin >> ch;

Much easier to work with and change.

in your code you have:
1
2
3
4
5
6
7
8
    case 6:
        exit(EXIT_SUCCESS);
        break;
        cout << "\n Wrong Choice";
        break;
    default:
        cout << "\n wrong choice";  // <--- The following blank line that was there is not needed.
}

You will never get to line 3 because the "exit" leaves the program. Also lines 3 and 4 are duplicated in the "default" case.


This is a much better way to end the program. Although each case will leave the switch and end the program.
1
2
3
4
5
6
        case 6:
            //exit(EXIT_SUCCESS);  // <--- Do not use "exit()" or exit from here.
            break;
        default:
            cout << "\n wrong choice";
    }


And do not use "exit()" this is a C function that causes an immediate and unconditional exit and knows nothing about cleaning up classes.

That said I see no point in going any farther as seeplus has fixed your program.

Andy
Hello Andy,
In my code in search function, i can enter the if statement ,and same as copiesinstock function and the operator overloading part in main not working
Hello mar1972,

You wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
 void search()
{
    string bt;
    int is;
    BooksInfo b[nob];  // <--- This is local to the search function. Not what you defined in "main'.

    cout << "Please Enter the ISBN of the book you want to search for: " << endl;
    cin >> is;

    cout << "Please Enter the Book title you want to search for: " << endl;
    cin >> bt;

    for (int i = 0; i < nob; i++)

The search function is 1 function of 1 object and knows nothing about the array that you defined in "main". The array you define in the function may be done correctly, but has no information. So in the end there is nothing to match to.

If you want to search the array defined in "main" then you need to pass it to the function.

A couple of suggestions:
When you define a constant most people I have seen use capital letters for the name. It at least sets it apart from a regular variable.

BooksInfo b[nob]; is nice, but what is a "b". Consider "book" or "bookInfo". Give it a name that actually means something.

You do not need a "cout" for every single line you need to display. The insertion operator(<<) allows you to chain many pieces together.
1
2
3
4
5
6
7
8
9
cout <<
    "The book's info you searched for is: "
    << b[i].Book_Title
    << b[i].Book_author
    << b[i].Book_publisher
    << b[i].Number_Of_Copies
    << b[i].ISBN
    << b[i].price
    << b[i].Publication_Year << '\n';

Also prefer to use the new line (\n) over the function endl.

I also fine that using "idx" as a for loop iterator makes more sense when it is used as a subscript to an array.

In the for loop the if condition may work better as an ||. This way if the book title is not spelled correctly there is a better chance that the ISBN number would be entered correctly.

Maybe it is me, but I read this

E. Functions to search the book by Book_Title and by ISBN


as meaning that the function should search both "Book_Title" and "ISBN" separately and not at the same time.

In "int copiesStock()" you are doing the same thing defining an array that is local to the function and empty.

I am not sure about the modify and print function yet as I have not tested them

In "main":
1
2
3
4
5
6
7
    b[0].setBook_Title("Self Assessment in Action");
    b[0].setBook_author("Betty McDonald");
    b[0].setBook_publisher("Common Ground Publishing");
    b[0].setNumber_Of_Copies(100);
    b[0].setISBN(9781863);
    b[0].setprice(1468);
    b[0].setPublication_Year(2009);

You need to find something better than this 25 times. seeplus has a solution that is better than 175 lines of code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
switch (ch)
{
    case 1:
        book1.search();  // <--- This is a single object. What do you expect to search.
        break;
    case 2:
        book1.copiesStock();  // <--- This is a single object. What do you expect to get, set or modify?
        break;
    case 3:
        book1.modify();  // <--- This is a single object. What do you expect to modify. And if you do how will it affect the array.
        break;
    case 4:
        for (int i = 0; i < nob; i++)
        {
            b[i].print();  // <--- This tends to make sense, but have not tried it yet.
            cout << endl;
        }
        break;


Andy
Hello mar1972,

As I was delving into the search function I noticed:
I made some variable name changes to give you an idea of what a good variable name can do.
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
void search(BOOKS& books)
{
    string bookTitle;
    int isbnNum;
    //BooksInfo books[NUM_OFBOOKS];  // <--- This is local to the search function. Not what you defined in "main'.

    cout << "Please Enter the ISBN of the book you want to search for: " << endl;
    cin >> isbnNum;

    cout << "Please Enter the Book title you want to search for: " << endl;
    cin >> bookTitle;

    for (int i = 0; i < NUM_OFBOOKS; i++)
    {
        if ((bookTitle == books[i].Book_Title) && (isbnNum == books[i].ISBN))
        {
            cout <<
                "The book's info you searched for is: "
                << books[i].Book_Title
                << books[i].Book_author
                << books[i].Book_publisher
                << books[i].Number_Of_Copies
                << books[i].ISBN
                << books[i].price
                << books[i].Publication_Year << '\n';
        }
    }
}

Just after the class definition I added: using BOOKS = BooksInfo[NUM_OF_BOOKS];. Bu adding and using this you can easily pass the array to a function by reference, which is much nicer than the pointer it degrades to, allowing you to be more efficient and be able to see the entire array not just the 1st element.

On line 11 given the book title of "Self Assessment in Action" in the formatted input only "Self" will be stored in "bookTitle" when the input stops at the 1st white space. What you need here is to use the unformatted input of "std::getline()". You will also need to follow line 8 with:
 
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. 

To clear the buffer before the next input reads what is left in the buffer.

I was think of a menu to choose to search by title or ISBN.

The above is based on your code, but after revisions it may not be needed or it may need to move before the "std::getline".

Getting back to the using statement I also had to add using BOOKS = BooksInfo[NUM_OF_BOOKS]; to the public section of the class before it would compile.

Sorry I got a bit side tracked working on the program. The output I get right now is:

                      Book Information Menu
----------------------------------------------------------------

 1. search the book by Book Title and by ISBN
 2. show the number of copies in stock, set the number of copies
    in stock, update the number of copies in stock
 3. Modifyin
 4. print the information of the books
 5. comparenting based on Publication Year
 6. Exit
  Enter Your Choice: 1


                             BOOK SEARCH
----------------------------------------------------------------------

 1. Search by book title
 2. Search by ISBN
   Enter choice: 1

 The book's info you searched for is:
 Title:            They Both Die at the End
 Author:           Adam Silvera
 Publisher:        Simon & Schuster Ltd
 Number of copies: 1000
 ISBN#:            9781471
 Price:            76.03
 Publication year: 2017


I did a little cheating to get this output, but wanted to show you what I ended up with. And yes this output does make use of "std::setw()".

I also added to "main" std::cout << std::fixed << std::setprecision(2); so the price would look correct. Adding a ($) is up to you.

Andy
Hello mar1972,

mar1972 wrote:

In my code in search function, i can enter the if statement ,and same as copiesinstock function and the operator overloading part in main not working

It is best to work on 1 part at a time. I started with the "search" because it was choice #1 and it looked like the "print" function would work. if not it should be easy to fix.

Once you get the "search working" what you learn there will go a long way with the other functions.

Andy
Hello mar1972,

I will save yo some time and a headache:
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
BOOKS books
{
    { "Self Assessment in Action", "Betty McDonald", "Common Ground Publishing", 100, 9781863, 1468, 2009 },
    { "CAT AND MOUSE", "JAMES PATTERSON", "Little, Brown and Company", 200, 9780755, 57.4, 1997 },
    { "THE DA VINCI CODE", "DAN BROWN", "Transworld & Bantam Books", 300, 9780552, 35.5, 2003 },
    { "DIE TRYING", "LEE CHILD", "Putnam", 400, 9780857, 45.7, 1998 },
    { "THE ONE MAN", "Andrew Gross", "Pan MacMillan", 500, 9781509, 77.2, 2017 },
    { "Fingersmith", "Sarah Waters", "Virago Press", 600, 1860498825, 83.47, 2002 },
    { "Reckless", "Andrew Gross", "William Morrow an Imprint of Harper Collins", 700, 9780061, 40, 2009 },
    { "The Gangster", "Clive Cussler & Justin Scott", "G. P. Putnam's Sons ", 800, 9780399, 26.42, 2016 },
    { "EVIL GAMES", "Angela Marsons", "Bookouture", 900, 9781909, 71.65, 2015 },
    { "They Both Die at the End", "Adam Silvera", "Simon & Schuster Ltd", 1000, 9781471, 76.03, 2017 },
    { "Shadow and Bone", "Leigh Bardugo", "Hachette Children's Group", 1100, 9781510, 68.15, 2018 },
    { "History Is All You Left Me", "Adam Silvera", "Simon & Schuster Ltd", 1200, 9781471, 80.94, 2017 },
    { "How the King of Elfhame Learned to Hate Stories ", "Holly Black", "Hot Key Books", 1300, 9781471, 102.88, 2033 },
    { "The Invisible Life of Addie LaRue", "V.E. Schwab", "Titan Books Ltd", 1400, 978178, 116.91, 2020 },
    { "Circe", "Madeline Miller", "Bloomsbury Publishing PLC", 1500, 9781408, 91.07, 2019 },
    { "The Psychology of Money", "Morgan Housel", "Harriman House Publishing", 1600, 97808, 81.65, 2020 },
    { "Shadow and Bone: Siege and Storm ", "Leigh Bardugo", "Hachette Children's Group", 1700, 9781, 69.99, 2018 },
    { "Crooked Kingdom Collector's Edition", "Leigh Bardugo", " Hachette Children's Group", 1800, 978151, 106.48, 2019 },
    { "The Choice", "Edith Eger", " Ebury Publishing", 1900, 978184, 108.68, 2019 },
    { "The Cruel Prince ", "Holly Black", "Hot Key Books", 2000, 97814, 91.07, 2018 },
    { "Red Queen", "Victoria Aveyard", "Orion Publishing Co", 2100, 978140, 67.51, 2015 },
    { "One Of Us Is Lying", "Karen M. McManus", "Penguin Random House Children's UK", 2200, 97801, 80.94, 2020 },
    { "The Code of the Extraordinary Mind", "Vishen Lakhiani", "Potter/Ten Speed/Harmony/Rodale", 2300, 97805, 86.46, 2019 },
    { "The Boy, The Mole, The Fox and The Horse", "Charlie Mackesy", " Ebury Publishing", 2400, 97815, 153.5, 2019 },
    { "The Order of Time", "Carlo Rovelli", "Penguin Books Ltd", 2500, 978014, 91.07, 2020 }
};

This will replace those 175 lines of set function calls.

I defiantly makes "main" shorter.

Andy
Oh man... It's like your prof is teaching "How to be a Bad Developer 101." He/she is forcing you to do things that you'd never do in the real world.

Add the following functions in your class
...
E. Functions to search the book by Book_Title and by ISBN

So you're BookInfo class is supposed to search for a book? Where does it search? Normally BookInfo would represent a single book and you might have a Library class with a search function. I guess for this backwards implementation, you need to pass the collection of books into the search function. reading further down the assignment, I see you're supposed to store the books in an array so:
1
2
3
// Search for this book's title and ISBN in arr, which has "size" element.
// Return the index of the matching book, or -1 if not found.
int BookInfo::search(BookInfo arr[], unsigned size);


Be sure to think of the BookInfo class as info about a single title. The array of books is the collection of the titles. These are separate entities. It's easy to mix them up.

F. Function to show the number of copies in stock, set the number of copies in stock, pdate the number of copies in stock, and return the number of copies in stock.

They probably mean different functions for each of these things.

H. An operator overloading >= function to compare two books to display which book was recently published
I. An operator overloading == function to compare two books to display if the books were published in the same year

Two books aren't equal just because they are published in the same year, so in the real world, you'd never use publication year alone in operator==().
Oh man... It's like your prof is teaching "How to be a Bad Developer 101." He/she is forcing you to do things that you'd never do in the real world.


Agree! When I re-worked the code above into a first-steps towards something reasonable, I stripped all that out from the class!
Hello mar1972,


Using C++, write a class BooksInfo which has the following attributes:

Book_Title, Book_author(s), Book_publisher, noOfCopies, ISBN, price, and Publication_Year.

Add the following functions in your class
► A. A default constructor
  B. A constructor that sets all variables of the class
► C. A copy constructor
► D. Appropriate get and set functions to get and set all attributes
  E. Functions to search the book by Book_Title and by ISBN
  F. Function to show the number of copies in stock, set the number of copies in stock, update the number of
    copies in stock, and return the number of copies in stock.
  G. Function to modify the Book_author, Book_publisher, price
  H. An operator overloading >= function to compare two books to display which book was recently published
  I. An operator overloading == function to compare two books to display if the books were published in the
    same year
  J. A function to print the information of the class BooksInfo.

Write a program to test various operations on the objects of the class booksInfo. Make a record of 25 books using
Arrays of type BooksInfo.

► denotes what has been done.


You defiantly need to work on "B".

dhauden wrote:

Add the following functions in your class
...
E. Functions to search the book by Book_Title and by ISBN


You could split this into 2 separate functions or make better use of what you have. Looking at what I posted earlier: http://www.cplusplus.com/forum/beginner/277286/#msg1196827 The menu would allow the user to choose between the 2. Then by changing the if statement in the for loop from (&&) to (||) it will only be matching what it needs to, either book title or ISBN. The string will either be empty or the ISBN will be (0)zero. Either way the || will have 1 side or the other to work with.

I did try one and got this output:

                            BOOK SEARCH
----------------------------------------------------------------------

 1. Search by book title
 2. Search by ISBN
   Enter choice: 2

 The book's info you searched for is:
 Title:            They Both Die at the End
 Author:           Adam Silvera
 Publisher:        Simon & Schuster Ltd
 Number of copies: 1000
 ISBN#:            9781471
 Price:            76.03
 Publication year: 2017

 The book's info you searched for is:
 Title:            History Is All You Left Me
 Author:           Adam Silvera
 Publisher:        Simon & Schuster Ltd
 Number of copies: 1200
 ISBN#:            9781471
 Price:            80.94
 Publication year: 2017

 The book's info you searched for is:
 Title:            How the King of Elfhame Learned to Hate Stories
 Author:           Holly Black
 Publisher:        Hot Key Books
 Number of copies: 1300
 ISBN#:            9781471
 Price:            102.88
 Publication year: 2033


If you read carefully you will notice that those 3 books have the same ISBN number. This may be alright for this assignment, but should be fixed.

Then I notices something else:
1
2
3
4
5
6
7
8
9
10
11
12
13
class BooksInfo
{
    private:  // <--- Not necessary as a class is private by default unless changed.
        string Book_Title;
        string Book_author;
        string Book_publisher;
        int Number_Of_Copies;
        long ISBN;
        double price;
        int Publication_Year;

    public:
        using BOOKS = BooksInfo[NUM_OF_BOOKS];

In the class you define ISBN as a long. But even this may not be big enough to store an ISBN number. Unless you plan on doing some kind of math on the number, and I do not see that, this would work better as a "std::string". Just looking up the first title 1 of the ISBN numbers is "978-1863356138". That (-) may be a problem depend on how the input reads it. It could be a minus sign or a dash. As a dash it could cause "std::cin" to fail and be unusable the rest of the program.

Getting back to the "search" function. From the menu choice you could use a switch to enter either the book title or the ISBN. I was also thing add to the function bool found{};. This gives "found" the value of (0)zero or false. Then when the for loop finds a match set "found" to true. Then after the for loop add:
1
2
3
4
if (!found)
{
    // a message to say that no match was found
}

This may not be required,but makes good sense and looks better when the program runs.

Next I decided to look at the "print" function. Or should it be called "input"?
1
2
3
4
5
6
void print()
{
    int Book_Title,Book_author,Book_publisher,Number_Of_Copies,ISBN,price,Publication_Year;

    cout << "Enter the Title: ";
    cin >> Book_Title;

The first problem here is that all the variables are defines as an "int", but the first input is a string. This will cause "std::cin" to fail and be unusable the rest of the program.

This is a print function and because of the way it is being called it only needs to print the private variables of a single object. Since this is a member function of the class it already has access to the private variables.

You need to rework the function to just print the private variables.

Next I looked at the "copiesStock" function. You start be defining an array that has no value and is local to the function. It has no connection to the array created in "main" that does information to use. So when you get to the for loop there is nothing in the array to compare to. Strings are empty and numeric values are (0)zero based on the default ctor. I have not finished working on this yet.

I have looked at the "modify" function and was wondering why you decided to change all 3 variables when you may only have to change 1?

I have not started with your last 3 functions yet as everything needs changed, but it is best to work on 1 part at a time.

Andy
Topic archived. No new replies allowed.