This is the link of the file required for the Program https://drive.google.com/o
ID: 3688804 • Letter: T
Question
This is the link of the file required for the Program https://drive.google.com/open?id=0B9ijg580RznBZm1HVWFZdzBEVTA
Structures and File I/O: Write a C++ program to manage book data for a library. It will read some data from a file, save each book record in a structure, and use an array of structures to store all the records. It will have a menu to allow the user to view data from the book list, or print out the current list of books to an output file. For the basic version of the program, the output file should be identical to the input file, but for the extra credit version, it will be possible for the user to add new books to the list, so the output file may contain additional books besides the starting list.
Download the file “Data file for assignment 10” from Blackboard to your account in the lab and save it with the filename books.data. Each line of this file contains the data of a book record in the following format:
author
title
publisher
date
category
chars 0 - 37
chars 38 - 90
chars 91 - 109
chars 110 - 115
char 116
The category of a book is a character which can have one of the following values:
N – new
D – damaged R – reference
T – textbook
Requirements:
Name the source file for your program program10.cpp
Use this structure definition (in the global scope, right below any #include’s and constant definitions):
struct Book
{ string author; string title; string publisher; string date; char status; };
Write the main function which has an array called books of 100 elements of type Book. Read the data from the input file books.data into the elements of your array using code something like this:
while (!in_file.eof())
{ getline(in_file, line); if (line.length() > 0)
{ books[num_records] = make_book(line); num_records++;
}
}
where make book is a function that you write yourself which extracts the necessary characters from its parameter, uses them to set the values of a Book, and returns this Book.
Display the menu of options shown in the sample run below, respond to the user’s choice from it, and redisplay the menu, until the user chooses to quit. The valid menu choices are the numbers 1, 2, 3, and 4. If the user enters an invalid integer, the menu must be redisplayed. The program is only required to work if the menu choice is an integer.
Where the user must enter a character to choose a category, if an invalid selection is made, keep prompting for a selection until a valid choice is made. Also, if a particular letter is a valid choice, then accept that letter in either upper- or lower-case.
The first two choices from the main menu must display book data on the screen in the format shown in the sample run below – choice 1 must show the data for all books, and choice 2 only for those books of the requested category.
For choice 3 from the main menu, all of the book data must be saved in (i.e. written to) an output file named books new.data. This output file must have one line for each Book record, and be in exactly the same format as the input file for the program. To test if this is working correctly, run the program, choose this option, then quit the program and compare the input and output files – they should be identical. If doing the extra credit option, also try adding a new book to the list, then saving the data in the output file. In this case, the output file should contain the data of the input file, followed by the data for the new book that was added. If an output file was written, then print a message to the screen stating this, as shown in the sample run below.
After attempting to open the input file and the output file, check if the file opened successfully, and if not then exit the program.
Be sure to close both the input file and the output file before the program terminates.
A sample run of your program should look like:
Available choices:
1. Display all books
2. Display books of some category
3. Write list of books to file
4. Quit
Enter the number of your choice: 2
Available categories are
N - new
D - damaged
R - reference
T - textbook
Enter the category for which you want to display books: n
List of books in category N: ----------------------------
2014 J. Lewis, P. DePasquale and J. Chase
Java Foundations
2015 A. Gilat
Pearson
Matlab An Introduction with Applications
Available choices:
1. Display all books
2. Display books of some category
3. Write list of books to file
4. Quit
Enter the number of your choice: 1
List of books in library: ------------------------2011 M. Main and W. Savitch
Wiley
Data Structures and Other Objects Using C++
Addison-Wesley
2000 K. Dattatri
C++ Effective Object-Oriented Software Construction Prentice Hall
2014 B. Stroustrup
Programming Principles and Practice Using C++
2013 B. Stroustrup
Addison-Wesley
The C++ Programming Language
2015 W. Savitch
Addison-Wesley
Problem Solving with C++
1990 W. R. Stevens
Pearson
Unix Network Programming
1987 A. Aho, J. Hopcroft and J. Ullman
Prentice Hall
Data Structures and Algorithms 2014 J. Lewis, P. DePasquale and J. Chase
Addison-Wesley
Java Foundations
1981 H. Lewis and C. Papadimitriou
Pearson
Elements of the Theory of Computation
2015 A. Gilat
Prentice Hall
Matlab An Introduction with Applications
2012 S. Attaway
Wiley
Matlab A Practical Introduction
2014 M. Weiss
Elsevier
Data Structures and Algorithm Analysis in C++
Pearson
Available choices:
1. Display all books
2. Display books of some category
3. Write list of books to file
4. Quit
Enter the number of your choice: 3
List of books was saved in the file books_new.data.
Available choices:
1. Display all books
2. Display books of some category
3. Write list of books to file
4. Quit
Enter the number of your choice: 4
Hints:
Use functions as needed to make your program well-organized.
The string class contains functions (methods) called substr and at which are useful to extract characters at specific positions from a string. Look up the documentation for these methods.
Extra credit option:
Meeting the requirements given above will allow you to earn a maximum of 80/80 for this assignment. If you like, you can choose to modify your program by adding another menu option which lets the user add data for more books according to the following requirements. This will give you the opportunity for a maximum of an additional 20 points for a total of 100/80.
Update the main menu with the new choice 3 as shown in the sample run below.
If the user chooses to add a new book, the program must check to make sure that the array of Books has space available before attempting to add a new book.
To add a new book, prompt the user to enter each of the data fields.
Note that for data members of Book whose type is string, the user may enter a string which contains spaces. The program must be able to handle this.
Also, since data that is entered for the new book needs to be written to the output file in the same format as the input file, each field will need to have the same length as was originally used when reading the data. To achieve this, if the user enters a string which is too long, use the first part of it up to the required length; and if they enter a string shorter than the required length, use it as the beginning of the string followed by as many spaces as necessary to make it the required length.
The data entered by the user must be used to set the value of an array element in the book list, and the number of array elements in use be increased by 1, then a confirmation message displayed to the user “New book was added to list.”
When you add a new book, test the other menu choices including displaying the data on the screen, and writing it to an output file, to make sure that the new data was added correctly.
If you choose to do the extra credit option, then add a comment at the top of your file that says this.
A sample run showing the new menu item, and new behavior of adding a book, would look like:
Available choices:
1. Display all books
2. Display books of some category
3. Add a book
4. Write list of books to file
5. Quit
Enter the number of your choice: 2
Available categories are
N - new
D - damaged
R - reference
T - textbook
Enter the category for which you want to display books: R
List of books in category R: ---------------------------2013 B. Stroustrup
The C++ Programming Language 1981 H. Lewis and C. Papadimitriou
Addison-Wesley
Elements of the Theory of Computation
2014 M. Weiss
Prentice Hall
Data Structures and Algorithm Analysis in C++
Pearson
Available choices:
1. Display all books
2. Display books of some category
3. Add a book
4. Write list of books to file
5. Quit
Enter the number of your choice: 3
Adding a new book...
Enter author: W. Savitch
Enter title: Absolute C++
Enter publisher: Pearson
Enter date: 2016
Enter category (1-letter code): R New book was added to list.
Available choices:
1. Display all books
2. Display books of some category
3. Add a book
4. Write list of books to file
5. Quit
Enter the number of your choice: 2
Available categories are
N - new
D - damaged
R - reference
T - textbook
Enter the category for which you want to display books: r
List of books in category R: ---------------------------2013 B. Stroustrup
The C++ Programming Language 1981 H. Lewis and C. Papadimitriou
Addison-Wesley
Elements of the Theory of Computation
2014 M. Weiss
Prentice Hall
Data Structures and Algorithm Analysis in C++
Pearson
2016 W. Savitch
Absolute C++ Pearson
Available choices:
1. Display all books
2. Display books of some category
3. Add a book
4. Write list of books to file
5. Quit
Enter the number of your choice: 4
List of books was saved in the file books_new.data.
Available choices:
1. Display all books
2. Display books of some category
3. Add a book
4. Write list of books to file
5. Quit
Enter the number of your choice: 5
Hints for extra credit option:
The string class contains a function (method) called resize which is useful for shortening a string if too much data was entered, or lengthening a string is less than the maximum length was entered.
The getline function can be helpful when needing to read (from user input) a string which could possibly contain spaces. For more details about getline, see the class notes or C++ documentation.
For your convenience, you may use the following clear input function in your program if you like:
void clear_input()
{ while (getchar() != ’ ’) ;
}
It can be helpful in transitioning between user input statements of the form cin >> and getline.
author
title
publisher
date
category
chars 0 - 37
chars 38 - 90
chars 91 - 109
chars 110 - 115
char 116
Explanation / Answer
Solution: See the code below
----------------------------------------------------
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include <cstring>
using namespace std;
//structure for a book
typedef struct {
string author;
string title;
string publisher;
string date;
char status;
} Book;
//function to extract book details
Book make_book(string book_details)
{
Book book;
book.author=book_details.substr(0,(37-0));
book.title=book_details.substr(38,(90-38));
book.publisher=book_details.substr(91,(109-91));
book.date=book_details.substr(110,(115-110));
book.status=book_details.at(116);
//cout<<book.date<<" "<<book.author<<" "<<book.title<<" "<<book.publisher<<" "<<book.status<<endl;
return book;
}
//function to display information of all books
void display_all_books(Book* books,int num_records)
{
for(int i=0;i<num_records;i++)
{
cout<<books[i].date<<" "<<books[i].author<<" "<<books[i].title<<" "<<books[i].publisher<<endl;
}
}
//function to display books of a given category
void display_books_by_category(Book* books,int num_records,char status)
{
for(int i=0;i<num_records;i++)
{
if(books[i].status==toupper(status))
{
cout<<books[i].date<<" "<<books[i].author<<" "<<books[i].title<<" "<<books[i].publisher<<endl;
}
}
}
//function to save books to file
void save_books_to_file(Book* books,int num_records)
{
string output_filename="new.data";
ofstream out_file(output_filename.c_str());
for(int i=0;i<num_records;i++)
{
out_file<<books[i].author<<books[i].title<<books[i].publisher<<books[i].date<<books[i].status<<endl;
}
out_file.close();
}
//function to print menu of options
void print_menu()
{
cout<<"Available choices:"<<endl;
cout<<"1. Display all books"<<endl;
cout<<"2. Display books of some category"<<endl;
cout<<"3. Write list of books to file"<<endl;
cout<<"4. Quit"<<endl;
}
//function to display book categories
void display_book_categories()
{
cout<<"Available categories are:"<<endl;
cout<<"N - new"<<endl;
cout<<"D - damaged"<<endl;
cout<<"R - reference"<<endl;
cout<<"T - textbook"<<endl;
}
//function to process choice
void process_choice(int choice, Book* books, int num_records)
{
switch(choice)
{
case 1:
display_all_books(books,num_records);
break;
case 2:
char category;
while(1)
{
display_book_categories();
cout<<"Enter the category for which you want to display books:";
cin>>category;
category=toupper(category);
cout<<category;
if(category=='N' || category=='D' || category=='T' || category=='R')
{
display_books_by_category(books,num_records,category);
break;
}
else
{
cout<<"Wrong category. Re-enter."<<endl;
}
}
//display_books_by_category(books,num_records,category);
break;
case 3:
save_books_to_file(books,num_records);
break;
case 4:
cout<<"Exiting..."<<endl;
exit(1);
default:
cout<<"Wrong choice, Re-enter."<<endl;
break;
}
}
/*
* main function
*/
int main(int argc, char** argv) {
Book books[100]; //array to store book records
int num_records=0; //number of records
string input_filename="Books.data"; //input file name
ifstream in_file(input_filename.c_str()); //input file handle
//read book records, if input file open
if(in_file.is_open())
{
string line;
while (!in_file.eof())
{
getline(in_file,line);
//cout<<line<<endl;
if (line.length() > 0)
{
books[num_records] = make_book(line);
num_records++;
}
}
//menu of options
int choice;
while(1)
{
print_menu();
cin>>choice;
process_choice(choice, books,num_records);
}
}
in_file.close();
return 0;
}
}
----------------------------------------------------
Output: new.data
-------------------------------------------------------
M. Main and W. Savitch Data Structures and Other Objects Using C++ Addison-Wesley 2011 T
K. Dattatri C++ Effective Object-Oriented Software Construction Prentice Hall 2000 T
B. Stroustrup Programming Principles and Practice Using C++ Addison-Wesley 2014 T
B. Stroustrup The C++ Programming Language Addison-Wesley 2013 R
W. Savitch Problem Solving with C++ Pearson 2015 T
W. R. Stevens Unix Network Programming Prentice Hall 1990 D
A. Aho, J. Hopcroft and J. Ullman Data Structures and Algorithms Addison-Wesley 1987 D
J. Lewis, P. DePasquale and J. Chase Java Foundations Pearson 2014 N
H. Lewis and C. Papadimitriou Elements of the Theory of Computation Prentice Hall 1981 R
A. Gilat Matlab An Introduction with Applications Wiley 2015 N
S. Attaway Matlab A Practical Introduction Elsevier 2012 D
M. Weiss Data Structures and Algorithm Analysis in C++ Pearson 2014 R
-----------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.