Write Program in C++ Description: You are an avid reader and have decided that y
ID: 3707740 • Letter: W
Question
Write Program in C++
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 stored in 2 parts – month (int) and year (int) a
6. a rating of the book (int).
The number will be generated by the program as each book is entered. The rating should be limited to a number between 1 and 10. It will be a scale the user can use to enter what they thought of the book. The month should be a valid month and the year should be less than the current year and not negative.
The program should ask the user for all of the book information, except for the number, until the user enters “exit” as the book title. After each book is entered, it should be written to an output file called “reading_list.txt”. The file should contain all of the information including the number for each book. Each item of the book should be printed on a new line. Each time the program is restarted, it should open the file to append to the end of the file.
Structure member definitions:
· Book number – to be generated by the programmer for each new book that is entered
· Book title – This should be able to hold spaces
· Author – should be able to hold spaces
· Description – should be able to hold spaces
· Date – should be a month stored as 1 – 12 and year stored with all four digits, i.e. 2017. It should be printed as XX XXXX i.e 9 2016 when printing it to the file.
· Rating – should be a number between 1 and 10
Requirements:
· The title, author, and description should be able to contain spaces.
· Book numbers must start with 1 and be generated by the program
Sample input:
Enter title: Les Miserables
Enter author: Victor Hugo
Enter description: A convict changes his life
Enter month: 01
Enter year: 2017
Enter rating: 9
Enter title: exit
Sample file output:
Les Miserables
Victor Hugo A
convict changes his life
01 2017
9
Explanation / Answer
code:
#include<iostream>
#include<string.h>
#include<fstream>
#include<ctime>
#include<iomanip>
using namespace std;
struct book_list
{
int number,year,month,rating;
string title,author,desc;
int book_number()
{
int count=0;
string line;//storing the line
ifstream file("reading_list.txt
while(getline(file,linecount++;
file.closereturn count}
void writedata()
{
fstream filefile.open("reading_list.txt",fstream::out|fstream::app);//opening file with open or append mode
file<<ob.number<<endl;//writing the book number to file
file<<ob.title<<endl;//writing book title to file
file<<ob.author<<endl;//writing author to file
file<<ob.desc<<endl;//writing description to file
file<<setfill('0')<<setw(2)<<ob.month<<" "<<setw(4) <<ob.year<<endl;//writing year and month to file
file<<ob.rating<<endl;//writing rating to file
file.close();
}
int main()
{
int n=book_number()/5;//getting the how many book are there in reading list
//divided the book_number()/5 because each record occupies the 5 lines so we get how many book are there
//getting current year
time_t theTime = time(NULL);
struct tm *aTime = localtime(&theTime);
int cyear=aTime->tm_year+1900;
while(true)
{
ob.number=n+1;//generating the book number
cout<<"Enter Book Title:"<<endl;
getline(cin,ob.title);
if(ob.title=="exit")//if book title is exit then quit
{
break;
}
cout<<"Enter Book Author:";
getline(cin,ob.author);
cout<<"Enter Book Description:";
getline(cin,ob.desc);
do{
cout<<"Enter Month:";
cin>>ob.month;
}while(ob.month<1 || ob.month>12);//checking the month
do{
cout<<"Enter year:";
cin>>ob.year;
}while(ob.year<0 ||ob.year>cyear);//checking the year
do{
cout<<"Enter Rating between 1 and 10";
cin>>ob.rating;
}while(ob.rating<1 || ob.rating>10);//checking the rating
cin.ignore();
n++;
cout.flush();
writedata();
}
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.