Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can you help me to check my code? and what should i put in \"display();\"/ which

ID: 3826071 • Letter: C

Question

can you help me to check my code? and what should i put in "display();"/ which is in main function.

#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
#include <sstream>
#include<algorithm>
#include<fstream>
using namespace std;
//Movie structure
struct Book
{
   string title;
   string author;
   string genre;
   string year;
};

// function prototypes
bool getBooks(vector<Book> &books);
int menu(vector<Book> &books);
bool compareByTitle(Book b1, Book b2);
bool compareByAuthor(Book b1, Book b2);
bool compareByGenre(Book b1, Book b2);
bool compareByYear(Book b1, Book b2);
void display(Book books[], int n);


bool getBooks(vector<Book> &books) {
   ifstream inputFile;
   string line;
   Book b;
   inputFile.open("Books.txt");
   if (inputFile.fail())
       return false;
   else
   {
       while (getline(inputFile, line))   // reads a line from the file
       {
           stringstream lineStream(line); // transforms the line into a string system
           // get fields from the string stream; fields are separated by comma in the input file

           getline(lineStream, b.title, ',');
           getline(lineStream, b.author, ',');
           getline(lineStream, b.genre, ',');
           getline(lineStream, b.year, ',');


           books.push_back(b);// add book to the vector
       }
   } inputFile.close();
   return true;
}
//main function
int main()
{
   vector <Book>bookDatabase;

    const int size= 30;
   int books[size]= {};
   int ch;
       while (1)
       {

           ch = menu(bookDatabase);

           switch (ch)
           {
           case 1:
               sort(bookDatabase.begin(), bookDatabase.end(),compareByTitle);
               break;
           case 2:
               sort(bookDatabase.begin(), bookDatabase.end(), compareByAuthor);
               break;
           case 3:
               sort(bookDatabase.begin(), bookDatabase.end(), compareByGenre);
               break;
           case 4:
               sort(bookDatabase.begin(), bookDatabase.end(), compareByYear);
               break;
           case 5:
               //system("pause");
               exit(0);
               break;
           }

           display(books,size);
       }

       system("pause");
       return 0;
}


//menu
int menu(vector<Book> &books)
{
   int choice;
   cout << " Menu" << endl;
   cout << "1.Sort by Title" << endl;
   cout << "2.Sort by Author" << endl;
   cout << "3.Sort by Genre" << endl;
   cout << "4.Sort by Year" << endl;
   cout << "5.Exit" << endl;

   cout << "Enter choice ?";
   cin >> choice;

   return choice;
}

void display(Book books[], int size )
{
  
   cout << setw(15) << "Title"
       << setw(20) << "Author"
       << setw(20) << "Genre"
       << setw(20) << "Year Published" << endl;
   for (int i = 0; i<size; i++)
       cout << setw(15) << books[i].title
       << setw(20) << books[i].author
       << setw(20) <<books[i].genre
       << setw(20) << books[i].year << endl;


}


bool compareByTitle(Book b1, Book b2)
{
   return b1.title < b2.title;
}
bool compareByAuthor(Book b1, Book b2)
{
   return b1.author < b2.author;

}
bool compareByGenre(Book b1, Book b2)
{
   return b1.genre < b2.genre;
}
bool compareByYear(Book b1, Book b2)
{
   return b1.year < b2.year;
}

Explanation / Answer

//you can find my changes under comment changed by chegg EA and give me good rating if find my ans helpful

#include<iostream>
#include<string>
#include<iomanip>
#include<vector>
#include <sstream>
#include<algorithm>
#include<fstream>
using namespace std;
//Movie structure
struct Book
{
   string title;
   string author;
   string genre;
   string year;
};

// function prototypes
bool getBooks(vector<Book> &books);
int menu(vector<Book> &books);
bool compareByTitle(Book b1, Book b2);
bool compareByAuthor(Book b1, Book b2);
bool compareByGenre(Book b1, Book b2);
bool compareByYear(Book b1, Book b2);
void display(Book books[], int n);

bool getBooks(vector<Book> &books) {
   ifstream inputFile;
   string line;
   Book b;
   //added by cheg EA
   int count = 0;
   inputFile.open("Books.txt");
   if (inputFile.fail())
       return false;
   else
   {
       while (getline(inputFile, line)) // reads a line from the file
       {
           stringstream lineStream(line); // transforms the line into a string system
           // get fields from the string stream; fields are separated by comma in the input file
           getline(lineStream, b.title, ',');
           getline(lineStream, b.author, ',');
           getline(lineStream, b.genre, ',');
           getline(lineStream, b.year, ',');

           books.push_back(b);// add book to the vector

       }
   } inputFile.close();
   return true;
}
//main function
int main()
{
   vector <Book>bookDatabase;
   const int size = 30;
   //changed by Chegg EA from int type to Book type,assign the data as show below or take input from User and assign array of structure book.
   //int books[size] = {};
   Book books[size] = {};
   //get book database loaded with data from file
   getBooks(bookDatabase);
   /*End of changes by chegg EA*/
   int ch;
   while (1)
   {
       ch = menu(bookDatabase);
       switch (ch)
       {
       case 1:
           sort(bookDatabase.begin(), bookDatabase.end(), compareByTitle);
           break;
       case 2:
           sort(bookDatabase.begin(), bookDatabase.end(), compareByAuthor);
           break;
       case 3:
           sort(bookDatabase.begin(), bookDatabase.end(), compareByGenre);
           break;
       case 4:
           sort(bookDatabase.begin(), bookDatabase.end(), compareByYear);
           break;
       case 5:
           //system("pause");
           exit(0);
           break;
       }
       //changed by Chegg EA ,copy to book array
       for (int i = 0; i < bookDatabase.size(); i++)
       {
           books[i].author = bookDatabase[i].author;
           books[i].genre = bookDatabase[i].genre;
           books[i].title = bookDatabase[i].title;
           books[i].year = bookDatabase[i].year;
       }
       //changed by chegg EA from size to size of vector
       display(books, bookDatabase.size());
   }
   system("pause");
   return 0;
}

//menu
int menu(vector<Book> &books)
{
   int choice;
   cout << " Menu" << endl;
   cout << "1.Sort by Title" << endl;
   cout << "2.Sort by Author" << endl;
   cout << "3.Sort by Genre" << endl;
   cout << "4.Sort by Year" << endl;
   cout << "5.Exit" << endl;
   cout << "Enter choice ?";
   cin >> choice;
   return choice;
}
void display(Book books[], int size)
{

   cout << setw(15) << "Title"
       << setw(20) << "Author"
       << setw(20) << "Genre"
       << setw(20) << "Year Published" << endl;
   for (int i = 0; i<size; i++)
       cout << setw(15) << books[i].title
       << setw(20) << books[i].author
       << setw(20) << books[i].genre
       << setw(20) << books[i].year << endl;

}

bool compareByTitle(Book b1, Book b2)
{
   return b1.title < b2.title;
}
bool compareByAuthor(Book b1, Book b2)
{
   return b1.author < b2.author;
}
bool compareByGenre(Book b1, Book b2)
{
   return b1.genre < b2.genre;
}
bool compareByYear(Book b1, Book b2)
{
   return b1.year < b2.year;
}

------------------------------------------------------------------

//ouput

Menu
1.Sort by Title
2.Sort by Author
3.Sort by Genre
4.Sort by Year
5.Exit
Enter choice ?1
Title Author Genre Year Published
C programming Dennies Ritchie Computer 1990
Java programming Ritchie K Computer 1995
Randal L. Schwartz Scripting language 2000 1998
Unix programming Vijay Mukhi Unix Concepts 1998
Menu
1.Sort by Title
2.Sort by Author
3.Sort by Genre
4.Sort by Year
5.Exit
Enter choice ?1
Title Author Genre Year Published
C programming Dennies Ritchie Computer 1990
Java programming Ritchie K Computer 1995
Randal L. Schwartz Scripting language 2000 1998
Unix programming Vijay Mukhi Unix Concepts 1998
Menu
1.Sort by Title
2.Sort by Author
3.Sort by Genre
4.Sort by Year
5.Exit
Enter choice ?2
Title Author Genre Year Published
C programming Dennies Ritchie Computer 1990
Java programming Ritchie K Computer 1995
Randal L. Schwartz Scripting language 2000 1998
Unix programming Vijay Mukhi Unix Concepts 1998
Menu
1.Sort by Title
2.Sort by Author
3.Sort by Genre
4.Sort by Year
5.Exit
Enter choice ?3
Title Author Genre Year Published
Randal L. Schwartz Scripting language 2000 1998
C programming Dennies Ritchie Computer 1990
Java programming Ritchie K Computer 1995
Unix programming Vijay Mukhi Unix Concepts 1998
Menu
1.Sort by Title
2.Sort by Author
3.Sort by Genre
4.Sort by Year
5.Exit
Enter choice ?4
Title Author Genre Year Published
C programming Dennies Ritchie Computer 1990
Java programming Ritchie K Computer 1995
Randal L. Schwartz Scripting language 2000 1998
Unix programming Vijay Mukhi Unix Concepts 1998
Menu
1.Sort by Title
2.Sort by Author
3.Sort by Genre
4.Sort by Year
5.Exit
Enter choice ?5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote