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

so this is what my programming run, but i am not satisfied about the four rows.

ID: 3826176 • Letter: S

Question

so this is what my programming run, but i am not satisfied about the four rows. they are not orderly. so can u help me to make the list to looks nice.

here are my coding

#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;
   Book books[size]= {};
   getBooks(bookDatabase);
   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;
           }
       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;
            }

           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(90) << "Author"
       << setw(90) << "Genre"
       << setw(90) << "Year Published" << endl;
   for (int i = 0; i<size; i++)
       cout << setw(15) << books[i].title
       << setw(90) << books[i].author
       << setw(90) <<books[i].genre
       << setw(90) << 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

#include<iostream>

#include<string>

#include<iomanip>

#include<vector>

#include <sstream>

#include<algorithm>

#include<fstream>

using namespace std;

struct Book

{

string title;

string author;

string genre;

string year;

};

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))  

{

stringstream lineStream(line);

getline(lineStream, b.title, ',');

getline(lineStream, b.author, ',');

getline(lineStream, b.genre, ',');

getline(lineStream, b.year, ',');

books.push_back(b);

}

} inputFile.close();

return true;

}

//main function

int main()

{

vector <Book>bookDatabase;

const int size= 30;

Book books[size]= {};

getBooks(bookDatabase);

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:

exit(0);

break;

}

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;

}

display(books,bookDatabase.size());

}

system("pause");

return 0;

}

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(90) << "Author"

        << setw(90) << "Genre"

        << setw(90) << "Year Published" << endl;

for (int i = 0; i<size; i++)

cout << setw(15) << books[i].title

<< setw(90) << books[i].author

        << setw(90) <<books[i].genre

        << setw(90) << 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;

}