Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add the following struct named Book to your code struct Book { string title; str

ID: 3544631 • Letter: A

Question

Add the following struct named Book to your code

      struct Book {

          string title;

          string author;

          double price;

};


Overload the << operator for the Book struct. Output the data in the form: author wrote title. For example, if the author is Mark Twain, and the title is Tom Sawyer, using the << operator would result in Mark Twain wrote Tom Sawyer.


Open the file bookDatabase.db for reading. The format of the file is:

      The column for author names is 20 characters wide.

      The column for book titles is 30 characters wide.

      The maximum price for a book is 99.99. Prices always include a decimal point and cents. There is no dollar sign.

      All columns are the same width for every book. If the value is shorter than the width, then the data is padded with spaces.

      There is exactly one space between an author and a title.

      There is exactly one space between the title and the price.

      The file is in Windows format.

     One book (author, title, price) per line.


Create a variable in your code like this:

      Book myBook;


Read every line in the bookDatabase.db file into the myBook variable and then print it using your overloaded << operator.

Explanation / Answer

#include <iostream>

#include<stdio.h>

#include<stdlib.h>

using namespace std;


struct Book {


char title[100];


char author[100];


double price;


};


int main() {


FILE *fp;

struct Book myBook;

fp = fopen("bookDatabase.db","r+");

while(fscanf(fp,"%s %s %lf",myBook.author,myBook.title,&myBook.price)!=-1)

{

cout<<myBook.author<< " wrote "<< myBook.title<<endl;

}

fclose(fp);

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote