please solve this question in C++ and write your comment in each step Descriptio
ID: 3705652 • Letter: P
Question
please solve this question in C++ and write your comment in each step
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 fileExplanation / Answer
Solution:
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;
}ob;//structure variable
int book_number()
{
//initializing the count variable
int count=0;
string line;//storing the line
ifstream file("reading_list.txt");//opening the file in read mode
while(getline(file,line))//getting the each line from the file and loop terminates when there is no line to count
count++;//increment the line count by 1
file.close();//close the file
return count;//return the count
}
void writedata()
{
fstream file;//opening file
file.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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.