205: Book Catalog: Define a struct to represent a book with attributes like title, author, and publication year. Write a program to create a catalog of books by taking user input and display books published after a certain year.
// Book Catalog: Define a struct to represent a book with attributes like title, author, and publication year.
// Write a program to create a catalog of books by taking user input and display books published after a certain year.
#include <iostream>
using namespace std;
struct Books
{
string title, author;
int year;
};
Books new_book();
void print_book(int i, Books x[]);
int main()
{
int num;
cout << "Enter the total number of books: ";
cin >> num;
// const int total = num;
Books book[num];
for (int i = 0; i < num; i++)
{
book[i] = new_book();
}
system("cls");
int year;
cout << "Enter the year: ";
cin >> year;
cout << "\nEvery book published before " << year << " are: \n";
for (int i = 0; i < num; i++)
{
if (book[i].year < year)
{
print_book(i, book);
}
}
return 0;
}
Books new_book()
{
system("cls");
Books x;
cout << "Enter the name of the book: ";
getline(cin >> ws, x.title);
cout << "Enter the author of the book: ";
getline(cin >> ws, x.author);
cout << "Enter the year of publication: ";
cin >> x.year;
return x;
}
void print_book(int i, Books x[])
{
cout << endl
<< "Book name: " << x[i].title << endl;
cout << "Book author: " << x[i].author << endl;
cout << "Publication year: " << x[i].year << endl;
}
Comments
Post a Comment