Using vectors to create a list of sorted books and altering inventory.

My main concerns vary widely for this program. One issue is creating lists among two cpp files and a header file. For one I can't seem to grasp the concept of pointers and lists, let alone sorting a list. The program basically creates a list which is an inventory of books. Initially, the list should be empty and it should display that the inventory is empty before the menu is displayed in the loop. The loop keeps running the program until option 5 is selected (Quit). The program must do the following: insert book, remove book, search by key word, display inventory and quit. The list should always be sorted. If you have "Cooking Rio" and "Cooking Boston", "Cooking Boston" must be higher than "Cooking Rio". Insert should allow the user to enter a book title, quantity, and price per book. If a book with the same name is already in the list, it just adds to its quantity. I have trouble understanding how to enter all three since there are strings and integers involved. Remove asks the user to type exactly what book needs to be removed, and removes it from the list while also subtracting that quantity from the total count. Display, or print, should show the total amount of books and the list of books with their quantity and price. Search looks for any book that is titled whatever the user enters. If I type Cooking in, both "Cooking Rio" and "Cooking Boston" should show up with their attributes. Quit simply quits. I would be very grateful if someone would help me with lists and sorting out the books into alphabetical order, A being the first.

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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//Baka.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
#include "Baka.h"
using namespace std;

Book::Book()
{
}

BookList::BookList()
{
}

BookList::~BookList()
{
	return;
}

void BookList::insert(string bookt, int bquan, double bookpri)
{
	
	//validate userinput to see if name is correct
	bool loop = false;
	string bookt = "";
	while (loop = false) {
		if (bookt  == "")
		{
			cout << "Please enter book title: " << endl;
			cin >> bookt;
			if (!cin) {
				cout << "Invalid word, please try again...:" << endl;
				cin.clear();
				cin.ignore();
				bookt = "";
				;
			}
			else {
				loop = true;
				break;
				;
			}
			;
		}
		;
	}

	//validate if userinput for quantity is a number
	bool loop = false;
	int bquan = -1;
	while (loop = false) {
		if (bquan == NULL)
		{
			cout << "Please enter how many books to add: " << endl;
			cin >> bquan;
			if (!cin) {
				cout << "Invalid amount, please try again...:" << endl;
				cin.ignore();
				cin.clear();
				bqex = -1;
				;
			}
			else if (bquan < 0) {
				cout << "Invalid amount, please try again...:" << endl;
				cin.clear();
				cin.ignore();
				bqex = -1;
				;
			}
			else {
				loop = true;
				break;
				;
			}
			;
		}
		;
	}

	//validate if price is a double

	bool loop = false;
	int bookpri = -1;
	while (loop = false) {
		if (bookpri == NULL)
		{
			cout << "Please enter book price: " << endl;
			cin >> bookpri;
			if (!cin) {
				cout << "Invalid amount, please try again...:" << endl;
				cin.ignore();
				cin.clear();
				bpex = -1;
				;
			}
			else if (bookpri < 0) {
				cout << "Invalid amount, please try again...:" << endl;
				cin.ignore();
				cin.clear();
				bookpri = -1;
				;
			}
			else {
				loop = true;
				break;
				;
			}
			;
		}
		;
	}
	

	cout << "Item(s) added to inventory." << endl;
}

void BookList::search(string key)
{
	cout << "Please enter a keyword to search inventory with:" << endl;
	cin >> key;

	//display items with that word or print that there is no such item that exists
	
	
}

void BookList::remove(string t)
{
	cout << "Please enter an item to remove from inventory: " << endl;
	cin >> t;

	//presume to check for item to remove otherwise print item doesn't exist
}

void BookList::print()
{
cout << "Number of total books: " << count << endl;
//print list with books, quantity and prices
}

//Baka.h

//YOU CAN'T ALTER THE HEADER, ONLY ADD THINGS TO IT
#pragma once
class Book
{
public:  
	Book(); // default constructor   
	Book(string bookt, int bquan, double bookpri);  // constructor 
	string bookt;
	int bquan;
	double bookpri;
private:  
	string title; // book title  
	int quantity; // number of copies in stock  
	double price;  // book price
						 // Assign friendship to class BookList 
						 // so that BookList object can directly access private data of Book    
	friend class BookList; // Assign friendship to class BookList 
};
class BookList
{
public: 
	BookList(); // Constructor 
	~BookList(); // Destructor
	//--------------------------------------------------------
	void insert(string bookt, int bquan, double bookpri);
	// Insert a new book record into the book list.  
	// The book has title bookt, number of copies in stock bquan 
	// and the price bookpri. 
	// Note that the vector of books should be maintained 
	// in the lexicographical order of the book titles. 
	// If the book title already exists in the list,  
	// then upgrade the quantity by adding bquan to current quantity; 
	// and upgrade the price with bookpri
	//--------------------------------------------------------
	void search(string key);
	// Search for matched book titles with the given key 
	// The book title which contains key as substring matches 
	// If a book is found, print its title, quantity and price; 
	// If more than one, display them in lexicographical order 
	// otherwise report No Matched Book is in stock 
	//--------------------------------------------------------
	void remove(string t);
	// Delete a book record with title exactly matching t,  
	// If the book is not in stock, report it then do nothing. 
	//--------------------------------------------------------
	void print();
	// First display totally how many books in stock;   
	// then display the book records in stock in the    
	// lexicographical order of book titles 
	//--------------------------------------------------------
	// other member functions if you need…     
private:  
	vector<Book> BL; // a vector to store Book objects in stock 
	int count;       // total number of books in stock  

}; 

//BakaMain.cpp

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
#include "Baka.h"
using namespace std;

bool sentinel = true;

int main()
{
	int selection;
	BookList list1;

	//loop that continues until the user chooses Quit (5)
	while (sentinel == true) {

		//Menu
		//------
		if (int count = 0) {cout << "The list is currently empty" << endl;}
		cout << "Welcome to Book Management System for Wildcar Bookstore" << endl;
		cout << "Select from:" << endl;
		cout << "1. Insert a book record" << endl;
		cout << "2. Delete a book record" << endl;
		cout << "3. Print a book record" << endl;
		cout << "4. Search the book list" << endl;
		cout << "5. Quit" << endl;
		//------
		//End of menu

		int selection = 0;

		if (selection == 1) {

			list1.insert("", -1, -1);
			
			; }

		else if (selection == 2) { 
			list1.remove("example");
			; }

		else if (selection == 3) { 
			list1.print();
			; }

		else if (selection == 4) { 
			list1.search("examplekey");
			; }

		else if (selection == 5) { 
			sentinel = false;
			cout << "Thanks for using my program!" << endl << endl;
			exit(0);
			; }
		else {
			cout << "Invalid input, please try again..." << endl;
			continue;
			; }
		;}

system("pause");
return 0;


}
Why does your BookList::insert do input and output? It does get the data via arguments. It is the duty of the caller to supply sane data. The insert should do insertion. Nothing more, nothing less. (Same goes for the other functions; main() gets data from user, not the BookList.)

An invariable is that the BL is sorted. Always. Make it so.

Therefore, you can use std::lower_bound
http://www.cplusplus.com/reference/algorithm/lower_bound/

That either finds an existing book, whose numbers should be updated, or the point where to insert a new book with
http://www.cplusplus.com/reference/vector/vector/insert/


In the BookList::search you simply iterate over the BL and print each entry that fulfils the condition.

The BookList::remove can use the std::lower_bound.
When I insert an item, how do I go about adding book title, quantity and name into one list element?
When you should insert a Book into vector BL, how do you create a Book object to insert?

How do you create a Book object?

Your class Book has two constructors. When they should be used? What they should do?
1
2
3
4
5
6
Book::Book(string bt, int bq, double bp)
{
	title = bt;
	quantity = bq;
	price = bp;
}


It says "+ 4 overload - no instance of Book::Book matches the specified type"
Topic archived. No new replies allowed.