Write a complete C++ program that accomplishes the task indicated below, Use goo
ID: 3843494 • Letter: W
Question
Write a complete C++ program that accomplishes the task indicated below, Use good form by including comments and meaningful identifiers. Be accurate with the syntax as if you were typing the program on the computer. Assume that there is a file on disk named Books.txt that contains information about all the books sold at amazon.com. Each line contains an ISBN number, and a count that indicates how many of that book was sold. Here is a sample for the first two lines in the file The first line means that 180 copies of the book with ISBN number 638-0-321-83389-1 were sold. Write the program with the assumption that you do not know how many lines are in the list. You do not need to use arrays for this program. Write a C++ program that will do the following: i. Open the file For each line of data, you must: ii. Read in the line of data from the file ii. Call a value returning function bestseller that returns true if the book made it on the best seller list and false otherwise. A book is a best seller if more than 100 copies were sold. Make sure that you write the code for this function. Compute the total number of books sold. Compute how many books made it on the best seller list, (not how many best seller books were sold, just how many unique ISBNs arc on the best seller list!) Print the ISBN number of the most sold book. Print the output so that it is organized as on the next page. (Of course there -will be more lines because this is shown only for the first two lines above and we don t know HOW many lines are in the file). All printing is done in the main function.Explanation / Answer
The program is as follows:
#include<iostream.h>
#include<string>
#include<vector>
using namespace std;
struct Book {
string isbn;
int num;
};
bool bestseller(Book bk){
if (bk.num > 100)
return true;
else
return false;
}
void main(){
ifstream infile;
vector<Book> list;
Book bk;
int num_books_sold;
int num_of_bestseller;
vector<string> isbnlist;
int max;
int index;
char ch;
infile.open("Books.txt");
if (!infile){
cout << "File opening error";
return;
}
while (infile >> bk.isbn >> bk.num){
list.push_back(bk);
}
infile.close();
num_books_sold = 0;
// Calculating total number of books sold
for (int i = 0; i<list.size(); i++){
num_books_sold = num_books_sold + list.at(i).num;
}
cout << "Total number of books sold: " << num_books_sold << endl;
// Calculating how many unique ISBN make it to best seller list
num_of_bestseller = 0;
max = 0;
for (int i = 0; i<list.size(); i++){ // for finding ISBN number with most books sold
if (list.at(i).num > max){
max = list.at(i).num;
index = i;
}
if (bestseller(list.at(i))){ // for finding unique ISBNs making it to best seller list
if (std::find(isbnlist.begin(), isbnlist.end(), list.at(i).isbn) == isbnlist.end()){
isbnlist.push_back(list.at(i).isbn);
num_of_bestseller++;
}
}
}
cout << "The number of unique ISBN making it to best seller :" << num_of_bestseller << endl;
cout << "The ISBN number with most sold book :" << list.at(index).isbn << endl;
//Printing the output. Assuming we need to print out all the data read from Books.txt page by page. Currently
// we are printing 20 lines in one slot and next 20 in the next slot. User needs to press enter to print the next
// slot
for (int i = 0; i<list.size(); i++){
if (i % 20 == 0)
cin >> ch;
cout << list.at(i).isbn << " " << list.at(i).num << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.