Question

In: Computer Science

This is a beginner C++ class Your program will be creating 10 library books. Each library...

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

Solutions

Expert Solution

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


Related Solutions

JAVA single method Loop (for beginner) Name your class LoopsFiles Create a program that reads a...
JAVA single method Loop (for beginner) Name your class LoopsFiles Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed When writing your CSV file, the first row (header row)...
Write a Python class to represent a LibraryMember that takes books from a library. A member...
Write a Python class to represent a LibraryMember that takes books from a library. A member has four private attributes, a name, an id, a fine amount, and the number of books borrowed. Your program should have two functions to add to the number of books and reduce the number of books with a member. Both functions, to add and return books, must return the final number of books in hand. Your program should print an error message whenever the...
C++ program, I'm a beginner so please make sure keep it simple Write a program to...
C++ program, I'm a beginner so please make sure keep it simple Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File.txt: Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets...
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
Start by creating a new Visual Studio solution, but chose a “class library .Net Core” as...
Start by creating a new Visual Studio solution, but chose a “class library .Net Core” as the project type. It will create a file and a class definition in that file.  Rename the file to be ToDo.cs  and accept the suggestion which will also rename that class to be Class Todo { } In that class, create             - a string property called Title             - an int property called  Priority             - a bool property called Complete Also, define one Constructor that takes in one...
Create a C# application for Library that allows you to enter data for books and saves...
Create a C# application for Library that allows you to enter data for books and saves the data to a file named Books.txt. Create a Book class that contains fields for title, author, number of pages, and price of the book and ToString() method. The fields of records in the file are separated with asterisk (*). Expecting sentinel value for ending the process of writing to file is "000"
explain the code for a beginner in c what each line do Question 3. In the...
explain the code for a beginner in c what each line do Question 3. In the following code, answer these questions: Analyze the code and how it works? How can we know if this code has been overwritten? Justify how? #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int changed = 0; char buff[8]; while (changed == 0){ gets(buff); if (changed !=0){ break;} else{     printf("Enter again: ");     continue; } }      printf("the 'changed' variable...
Write a C program that prints the Grade of each student in a class based on...
Write a C program that prints the Grade of each student in a class based on their mark. The program must also print the average mark of the class. The program must prompt (ask) the user for the mark of a student (out of 100). The program must then print the mark entered and the grade received based on the table below. Grade Range A 85 to 100 inclusive B 75 to 85 inclusive C 60 to 70 inclusive D...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside of main declare two static final variables and integer for number of days in the week and a double for the revenue per pizza (which is $8.50). Create a method called main Inside main: Declare an integer array that can hold the number of pizzas purchased each day for one week. Declare two additional variables one to hold the total sum of pizzas sold...
In this homework you will implement a Library class that uses your Book and Person class...
In this homework you will implement a Library class that uses your Book and Person class from homework 2, with slight modifications. The Library class will keep track of people with membership and the books that they have checked out. Book.java You will need to modify your Book.java from homework 2 in the following ways: field: dueDate (private)             A String containing the date book is due.  Dates are given in the format "DD MM YYYY", such as "01 02 2017"...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT