include iostream include vector include string using namespace std str

 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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Book {
string author;
string name;
int year;
string genre;
void Init() {
cout << "Enter the name of book: ";
getline(cin, name);
cout << "Enter the author of book: ";
getline(cin, author);
cout << "Enter year: ";
cin >> year;
cin.ignore();
cout << "Enter the genre of book: ";
getline(cin, genre);
}
void print() {
cout << "Name: " << name << endl;
cout << "Author: " << author << endl;
cout << "Year: " << year << endl;
cout << "Genre: " << genre << endl;
}
};
vector<Book> search(Book *Books, int number, string author) {
vector<Book> result;
for (int i = 0; i < number; i++) {
if (Books[i].author == author) result.push_back(Books[i]);
}
return result;
}
int main() {
int number;
cout << "Enter the number of books: ";
cin >> number;
cin.ignore();
Book *Books = new Book[number];
for (int i = 0; i < number; i++) {
cout << "Enter information about book " << i + 1 << ": " << endl;
Books[i].Init();
}
string toFind;
cout << "Who are you looking for? ";
getline(cin, toFind);
auto toPrint = search(Books, number, toFind);
cout << "Found next books: \n";
for (auto i : toPrint) {
i.print();
}
delete[]Books;
system("pause");
return 0;
}