Question

please use C++ write a program to read a textfile containing a list of books. each...

please use C++

write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. each line in the file has tile, ..... write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. Each line in the file has tile, au... Write a program to read a textfile containing a list of books. Each line in the file has tile, author and genre of a book separated by comma. After reading each line, you can use the parser function provided to separate those attributes and store them in the corresponding fields in the Book structure. All books should go into a vector of structures that you will create (similarly to the vector you created in lab1, except instead of a vector of double, you will need a vector of Book). It is ok to define the structure type in the global scope but all variables must be kept local. After the program reads the data from the file and stores all the books in a vector, have your program give the user options to see the books on the screen sorted by title, author or genre. Also add an option to quit. Use the sort function (#include ) to sort the requested data. Example: sort (myBooks.begin(), myBooks.end(), compareByTitle); sort will use the function that you provide to sort the data. Since you need to sort by three different fields, you will need three different compare functions. If the user asks to see the books sorted by title, you need to call sort passing compareByTitle. If he/she asks for the books sorted by author, you will need to call sort passing compareByAuthor, and so forth. Define one compare function for each option. This is how function compareByTitle should look like: bool compareByTitle(Book b1, Book b2) { return b1.title < b2.title) } After sorting, display all the information of each book in table format. No matter how the data is sorted, you should print all the fields. It is best to define a function that prints the whole vector. Print a header like the following and then all the data that is in the vector, one book per line.

Title Author Genre

Starting out with c++, Tony Gaddis, technical

Fundamentals of Database Systems, Elmarsi & Navathe, technical

The C++ Programming Language, Bjarne Stroustrup, technical

The Pillars of the Earth, Ken Follett, historical fiction

Fall of Giants, Ken Follet, historical fiction

Mindset: The New Psychology of Success, Carol Dweck, psychology

One Hundred Years of Solitude, Gabriel Garcia Marquez, fiction

Ashes, Kenzo Kitakana, fiction

The Dark Forest, Liu Cixin, science fiction

Replay, Ken Grimwood, fantasy

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <stdlib.h>
using namespace std;

// Defines a structure for Book
struct Book
{
// Data member to store data
string title;
string author;
string genre;
};// End of structure

// Function to read books information from file and stores it in vector
void readFile(vector<Book> &myList)
{
// Declares an object of Book
Book books;
// To store skip data
string data;
// ifstream object declared
ifstream fRead;

// Opens the file for reading
fRead.open("bookInformation.txt");

// Checks if the file unable to open for reading display's error message with file name
if(!fRead)
{
cout<<"\n ERROR: Unable to open the file for reading.";
exit(0);
}// End of if condition

// Loops till end of the file
while(!fRead.eof())
{
// Extracts title from the file
getline(fRead, books.title, ',');

// Extracts author name from the file
getline(fRead, books.author, ',');

// Extracts genre from the file
fRead>>books.genre;
// Skip new line
getline(fRead, data, '\n');

// Adds books object to vector
myList.push_back(books);
}// End of while loop
// Closes the file
fRead.close();
}// End of function

// Function to display books information
void display(vector<Book> myList)
{
// Loops till number of records
for(int x = 0; x < myList.size(); x++)
{
// Displays each book information
cout<<"\n\n Title: "<<myList[x].title<<"\n Author: "<<myList[x].author<<"\n Genre: "<<myList[x].genre;
}// End of for loop
}// End of function

// Function to return true if title of object b1 is less than title of object b2
// otherwise returns false
bool compareByTitle(const Book &b1, const Book &b2)
{
return b1.title < b2.title;
}// End of function

// Function to return true if author of object b1 is less than author of object b2
// otherwise returns false
bool compareByAuthor(const Book &b1, const Book &b2)
{
return b1.author < b2.author;
}// End of function

// Function to return true if genre of object b1 is less than genre of object b2
// otherwise returns false
bool compareByGenre(const Book &b1, const Book &b2)
{
return b1.genre < b2.genre;
}// End of function

// main function definition
int main()
{
// Declares a vector of type Book to store book objects
vector<Book> myList;

// Calls the function to read books information and stores it in vector myList
readFile(myList);

// To store user choice
int ch;

// Loops till user choice is not 4
do
{
// Displays menu
cout<<"\n 1. Sort by Title";
cout<<"\n 2. Sort by Author";
cout<<"\n 3. Sort by Genre";
cout<<"\n 4. EXIT";

// Accepts user choice
cout<<"\n Enter your choice: ";
cin>>ch;

// Checks user choice and calls appropriate sort method
switch(ch)
{
case 1:
sort(myList.begin(), myList.end(), compareByTitle);
display(myList);
break;
case 2:
sort(myList.begin(), myList.end(), compareByAuthor);
display(myList);
break;
case 3:
sort(myList.begin(), myList.end(), compareByGenre);
display(myList);
break;
case 4:
exit(0);
default:
cout<<"\n Invalid choice!";
}// End of switch - case
}while(true); // End of do - while loop
}// End of main function

Sample Output:

1. Sort by Title
2. Sort by Author
3. Sort by Genre
4. EXIT
Enter your choice: 1


Title: Ashes
Author: Kenzo Kitakana
Genre: fiction

Title: Fall of Giants
Author: Ken Follet
Genre: historical

Title: Fundamentals of Database Systems
Author: Elmarsi & Navathe
Genre: technical

Title: Mindset: The New Psychology of Success
Author: Carol Dweck
Genre: psychology

Title: One Hundred Years of Solitude
Author: Gabriel Garcia Marquez
Genre: fiction

Title: Replay
Author: Ken Grimwood
Genre: fantasy

Title: Simple C
Author: Pyari
Genre: technical

Title: Starting out with c++
Author: Tony Gaddis
Genre: technical

Title: The C++ Programming Language
Author: Bjarne Stroustrup
Genre: technical

Title: The Dark Forest
Author: Liu Cixin
Genre: science

Title: The Pillars of the Earth
Author: Ken Follett
Genre: historical
1. Sort by Title
2. Sort by Author
3. Sort by Genre
4. EXIT
Enter your choice: 2


Title: The C++ Programming Language
Author: Bjarne Stroustrup
Genre: technical

Title: Mindset: The New Psychology of Success
Author: Carol Dweck
Genre: psychology

Title: Fundamentals of Database Systems
Author: Elmarsi & Navathe
Genre: technical

Title: One Hundred Years of Solitude
Author: Gabriel Garcia Marquez
Genre: fiction

Title: Fall of Giants
Author: Ken Follet
Genre: historical

Title: The Pillars of the Earth
Author: Ken Follett
Genre: historical

Title: Replay
Author: Ken Grimwood
Genre: fantasy

Title: Ashes
Author: Kenzo Kitakana
Genre: fiction

Title: The Dark Forest
Author: Liu Cixin
Genre: science

Title: Starting out with c++
Author: Tony Gaddis
Genre: technical

Title: Simple C
Author: Pyari
Genre: technical
1. Sort by Title
2. Sort by Author
3. Sort by Genre
4. EXIT
Enter your choice: 3


Title: Replay
Author: Ken Grimwood
Genre: fantasy

Title: One Hundred Years of Solitude
Author: Gabriel Garcia Marquez
Genre: fiction

Title: Ashes
Author: Kenzo Kitakana
Genre: fiction

Title: Fall of Giants
Author: Ken Follet
Genre: historical

Title: The Pillars of the Earth
Author: Ken Follett
Genre: historical

Title: Mindset: The New Psychology of Success
Author: Carol Dweck
Genre: psychology

Title: The Dark Forest
Author: Liu Cixin
Genre: science

Title: The C++ Programming Language
Author: Bjarne Stroustrup
Genre: technical

Title: Fundamentals of Database Systems
Author: Elmarsi & Navathe
Genre: technical

Title: Starting out with c++
Author: Tony Gaddis
Genre: technical

Title: Simple C
Author: Pyari
Genre: technical
1. Sort by Title
2. Sort by Author
3. Sort by Genre
4. EXIT
Enter your choice: 9

Invalid choice!
1. Sort by Title
2. Sort by Author
3. Sort by Genre
4. EXIT
Enter your choice: 4

Add a comment
Know the answer?
Add Answer to:
please use C++ write a program to read a textfile containing a list of books. each...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • MUST BE WRITTEN IN C++ Objective: Learn how to define structures and classes, create and access...

    MUST BE WRITTEN IN C++ Objective: Learn how to define structures and classes, create and access a vector of structures, use sort with different compare functions. Assignment: Your program will use the same input file, but you will print the whole book information, not just the title. And, most importantly, this time you will take an object oriented approach to this problem. Detailed specifications: Define a class named Collection, in which you will define a structure that can hold book...

  • MUST BE WRITTEN IN C++ Objective: Learn how to define structures and classes, create and access...

    MUST BE WRITTEN IN C++ Objective: Learn how to define structures and classes, create and access a vector of structures, use sort with different compare functions. Assignment: Your program will use the same input file, but you will print the whole book information, not just the title. And, most importantly, this time you will take an object oriented approach to this problem. Detailed specifications: Define a class named Collection, in which you will define a structure that can hold book...

  • Write a C program to create a list of books details. The details of a book...

    Write a C program to create a list of books details. The details of a book include title, author, publisher, publishing year, no. of pages , price Perform the following with respect to the list of books created Display all the details of books written by a given author. Sort the details of books in the increasing order of price. Display the details of books published by a given publisher in a given year. Sort the list of books in...

  • Using C++ Skills Required Create and use classes Exception Handling, Read and write files, work vectors....

    Using C++ Skills Required Create and use classes Exception Handling, Read and write files, work vectors. Create Functions, include headers and other files, Loops(while, for), conditional(if, switch), datatypes,  etc. Assignment You work at the computer science library, and your boss just bought a bunch of new books for the library! All of the new books need to be cataloged and sorted back on the shelf. You don’t need to keep track of which customer has the book or not, just whether...

  • Using an object-oriented approach, write a program to read a file containing movie data (Tile, Director,...

    Using an object-oriented approach, write a program to read a file containing movie data (Tile, Director, Genre, Year Released, Running Time) into a vector of structures and perform the following operations: Search for a specific title Search for movies by a specific director Search for movies by a specific genre Search for movies released in a certain time period Search for movies within a range for running time Display the movies on the screen in a nice, easy to read...

  • please write this program in C++(Linux). Also write all the explanation of codes, the comment for...

    please write this program in C++(Linux). Also write all the explanation of codes, the comment for your code. Description You are an avid reader and have decided that you would like to keep track of the books that you read. You will define a structure called book_list that will hold 1. a number associated with each book (int) 2. book title (string), 3. author (string), 4. a description of the book (string), 5. a date when the book was read...

  • Implement a Java application for the following: 1.) Keep track of a movie collection. 2.) Each...

    Implement a Java application for the following: 1.) Keep track of a movie collection. 2.) Each movie in the collection will contain: Title, Genre, Year (4 digits) and Runtime (double - ex. 2.1 (hrs)). 3.) Program will read movies from a local text file named movies.txt in the current project directory in Eclipse. 4.) Each line in the text file contains one movie, containing each field, per line.separated by commas. 5.) Read the text file and load the movies into...

  • LAB 7-Movie List Program Goali Your assignment is to write a C++program to implement the ADT...

    LAB 7-Movie List Program Goali Your assignment is to write a C++program to implement the ADT List by creating a list of movies. This assignment will help you practice: multiple file programming, classes, pablic and private methods, dynamie memory, constructors and destructors, arrays and files Implementation the required classes This lab assignment gives you the opportunity to practice creating classes and using dynamic memory in one of There are two classes to implement: Movie and MovieList. As you can see...

  • Write a C++ program that asks for the following information about a book order from the...

    Write a C++ program that asks for the following information about a book order from the console: • Title • Author • ISBN (hint: careful about what data type to use - validate 9 or 13 characters) • Price (validate number < 400) • Quantity (number of books to purchase – validate > 0 and < 100) • Fiction/Non-Fiction (‘N’ or ‘F’ - validate) • Genre (‘R’ romance, ‘D’ drama, ‘M’ mystery – validate) Make use of the following data...

  • Create a program SearchNewBooks.cc that given as command line input newbooks.dat and request.dat and a method...

    Create a program SearchNewBooks.cc that given as command line input newbooks.dat and request.dat and a method of search either binary or linear, creates a file of the number of books found from the request list. program should receive two filenames as command line arguments, the new books file and request file, which the program should read in. The list of new books and list of requested books should should be stored in only two ​std::vector​ containers. Because some code to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT