In: Computer Science
This is a beginner C++ class
Your program will be creating 10 library books. Each library book will have a title, author and publishing year. Each library book will be stored as a structure and your program will have an array of library books (an array of structures).
After creating the struct, the next task is to create a new separate array for book genres.
You will create this corresponding array of book genres: Mystery, Romance, Fantasy, Technology, Children, etc. If the first element of the structure is storing information about a book titled : "More about Paddington", then the corresponding first element of array book genre would indicate 'Children'.
You should only use structure and array. You are not allowed to use vectors.
The user of your program should be able to select what they want to do from a menu of the following things and your program needs to do what the menu selection indicates:
* Print the entire list of library books (including the title, author, publishing year and book genre)
* Display a count of how many books exist per genre
* Find and display all books where the publishing year is greater than the year user put in
* Print the titles of the books where the Genre may be indicated by a 'C' for Children, & 'M' for Mystery
Screenshot
Program
#include <iostream>
#include<string>
#include<iomanip>
using namespace std;
//Book struct
struct Book {
string title;
string author;
int year;
};
//Function prototypes
int loadData(Book*, string*);
int menu();
void printLibrary(const Book[], const string[], int sz);
void existPerGenre(string[], int sz);
void printGreater(const Book[], const string[], int sz,int
yr);
void searchForGenre(const Book[], const string[],int sz, char
ch);
int main()
{
//Array for library
Book library[10];
//Array for corresponding genres
string genres[10];
int yr;
char gen;
//Function call to load library with books
int cnt = loadData(library, genres);
int opt = menu();
//Loop unti exit
while (opt != 5) {
if (opt == 1) {
printLibrary(library, genres, cnt);
}
else if (opt == 2) {
existPerGenre(genres, cnt);
}
else if (opt == 3) {
cout <<
"\nEnter year: ";
cin >>
yr;
printGreater(library, genres, cnt, yr);
}
else if (opt == 4) {
cout <<
"\nEnter genre(c/m): ";
cin >>
gen;
while
(toupper(gen) != 'C' && toupper(gen) != 'M')
{
cout << "Error!!!Should be C or M"
<< endl;
cout << "\nEnter genre(c/m): ";
cin >> gen;
}
searchForGenre(library, genres, cnt, gen);
}
cout << endl;
opt = menu();
}
//End
cout << "GOOD BYE!!!" << endl;
return 0;
}
//Function to load data into arrays
int loadData(Book* library, string* genres) {
int cnt = 0;
library[cnt].title = "C++ Programming for
beginners";
library[cnt].author = "Benjamin Stroup";
library[cnt].year = 1989;
genres[cnt] = "Technology";
cnt++;
library[cnt].title = "Java Programming for
beginners";
library[cnt].author = "Micheal Mintos";
library[cnt].year = 1991;
genres[cnt] = "Technology";
cnt++;
library[cnt].title = "More about Paddington";
library[cnt].author = "Catherine Rose";
library[cnt].year = 2000;
genres[cnt] = "Children";
cnt++;
return cnt;
}
//Function to display menu
//and return user selection
int menu(){
int ch;
cout << "OPTIONS:-" << endl;
cout << "1. Print the entire list of library
books\n2. Display a count of how many books exist per genre"
<< "\n3 Find and display all
books where the publishing year is greater than the year user put
in"
<< "\n4. Print the titles of
the books where the Genre may be indicated by a 'C' for Children,
&'M' for Mystery"
<< "\n5. Exit" <<
endl;
cout << "Enter your choice: ";
cin >> ch;
while (ch < 1 || ch>5) {
cout << "Error!! Choice
should be 1-5" << endl;
cout << "Enter your choice:
";
cin >> ch;
}
return ch;
}
//Function to print all the books in library
void printLibrary(const Book lib[], const string genre[], int sz)
{
cout << "\n
Title
Author
PublishedYear
Genre" << endl;
cout <<
"------------------------------------------------------------------------------------------"
<< endl;
for (int i = 0; i < sz; i++) {
cout << setw(25) <<
lib[i].title << setw(25) << lib[i].author <<
setw(15)
<<
lib[i].year << setw(22) << genre[i] <<
endl;
}
}
//Function to display Genre count
void existPerGenre(string gen[], int sz) {
string *genre;
genre = new string[sz];
int k = 0;
for (int i = 0; i < sz; i++) {
bool check = false;
for (int l = 0;
l < k; l++) {
if (genre[l] == gen[i]) {
check = true;
break;
}
}
if (check ==
false) {
genre[k] = gen[i];
k++;
}
}
cout << "\n
Genre
count\n";
for (int i = 0; i < k; i++) {
int cnt = 0;
for (int j = 0; j < sz; j++)
{
if (gen[j] ==
genre[i]) {
cnt++;
}
}
cout << setw(10) <<
genre[i] <<setw(10)<< cnt << endl;
}
}
//Function to display details of books greater than given
year
void printGreater(const Book lib[], const string gen[], int sz, int
yr) {
bool check=false;
for (int i = 0; i < sz; i++) {
if (lib[i].year > yr) {
cout <<
setw(25) << lib[i].title << setw(25) <<
lib[i].author << setw(15)
<< lib[i].year << setw(22) <<
gen[i] << endl;
check =
true;
}
}
if (!check) {
cout << "No books present
after the year " << yr << endl;
}
}
//Function based on genre
void searchForGenre(const Book lib[], const string gen[],int sz,
char ch) {
bool check;
if (toupper(ch) == 'M') {
for (int i = 0; i < sz; i++)
{
if
(gen[i]=="Misery") {
cout << lib[i].title << endl;
check = true;
}
}
}
else{
for (int i = 0; i < sz; i++)
{
if (gen[i] ==
"Children") {
cout << lib[i].title<< endl;
check = true;
}
}
}
if (!check) {
cout << "No book found in
libraray" << endl;
}
}
------------------------------------------------------------------------------------------
Output
OPTIONS:-
1. Print the entire list of library books
2. Display a count of how many books exist per genre
3 Find and display all books where the publishing year is greater
than the year user put in
4. Print the titles of the books where the Genre may be indicated
by a 'C' for Children, &'M' for Mystery
5. Exit
Enter your choice: 1
Title
Author
PublishedYear
Genre
------------------------------------------------------------------------------------------
C++ Programming for
beginners
Benjamin
Stroup
1989
Technology
Java Programming for
beginners
Micheal
Mintos
1991
Technology
More about
Paddington
Catherine
Rose
2000
Children
OPTIONS:-
1. Print the entire list of library books
2. Display a count of how many books exist per genre
3 Find and display all books where the publishing year is greater
than the year user put in
4. Print the titles of the books where the Genre may be indicated
by a 'C' for Children, &'M' for Mystery
5. Exit
Enter your choice: 2
Genre
count
Technology 2
Children 1
OPTIONS:-
1. Print the entire list of library books
2. Display a count of how many books exist per genre
3 Find and display all books where the publishing year is greater
than the year user put in
4. Print the titles of the books where the Genre may be indicated
by a 'C' for Children, &'M' for Mystery
5. Exit
Enter your choice: 3
Enter year: 1990
Java Programming for
beginners
Micheal
Mintos
1991
Technology
More about
Paddington
Catherine
Rose
2000
Children
OPTIONS:-
1. Print the entire list of library books
2. Display a count of how many books exist per genre
3 Find and display all books where the publishing year is greater
than the year user put in
4. Print the titles of the books where the Genre may be indicated
by a 'C' for Children, &'M' for Mystery
5. Exit
Enter your choice: 4
Enter genre(c/m): c
More about Paddington
OPTIONS:-
1. Print the entire list of library books
2. Display a count of how many books exist per genre
3 Find and display all books where the publishing year is greater
than the year user put in
4. Print the titles of the books where the Genre may be indicated
by a 'C' for Children, &'M' for Mystery
5. Exit
Enter your choice: 5
GOOD BYE!!!
-----------------------------------------------------------------------------------------------
Note:-
I assume you are expecting your program this way