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

using c++ : Write a program that prompts the user for the name of a file. The fi

ID: 3714749 • Letter: U

Question

using c++ :

Write a program that prompts the user for the name of a file. The file will be a plain text file that contains information about different objects of type "BibleBook". BibleBook will be defined as a class.

The private member attributes are:

string title;

int numChapters;

double amountRead;

The title will hold the title of the book, for example, "Genesis", "Exodus", "Psalms", etc. numChapters will hold the number of chapters in that book. amountRead will be a decimal ranging from 0 to 1. If the value is 0, that means you have read none of that book. If the number of 0.50, that means you have read half of that book, etc.

You will need to discover the public member functions that need to be written based on the description.

The text file will be organize like so: Each line will hold the title of a book, then a space, then the number of chapters, then a space, then the amount read of that book.

Here's an example:

Genesis 50 0.98

Matthew 28 1.00

Your program will read in this information and store each line in it's own object, and the objects will be part of a vector. In other words, there will be a vector of BibleBooks.

Your program will then iterate through the vector, displaying all of the information about each object.

The program will also identify for which object the amount read is the lowest.

Sample output:

Genesis 50 0.98

Matthew 28 1

The book with the lowest amount read is Genesis.

Explanation / Answer

//BibleBook.h

#pragma once

#include <string>

using namespace std;

class BibleBook

{

public:

// Constructor

BibleBook(string _title, int _numChapters, double _amountRead);

// Returns the title of the book

string getTitle();

// Returns the number of chapeters in the book

int getNumChapters();

// Returns the amount of book read

double getAmountRead();

private:

// Private variables

string title;

int numChapters;

double amountRead;

};

//BibleBook.cpp

#include "BibleBook.h"

BibleBook::BibleBook(string _title, int _numChapters, double _amountRead) :

title(_title), numChapters(_numChapters), amountRead(_amountRead)

{}

string BibleBook::getTitle()

{

return title;

}

int BibleBook::getNumChapters()

{

return numChapters;

}

double BibleBook::getAmountRead()

{

return amountRead;

}

//main.cpp

#include <iostream>

#include <fstream>

#include <vector>

#include "BibleBook.h"

using namespace std;

int main()

{

// Collection storing the details of books read from the given file

vector<BibleBook> books;

string fileName;

string bookName;

int chapters;

double read;

cout << "Enter name of file: ";

cin >> fileName;

ifstream inFile(fileName);

if (!inFile)

{

cerr << "Unable to open file: " << fileName;

return 1;

}

while (inFile >> bookName)

{

inFile >> chapters;

inFile >> read;

BibleBook book(bookName, chapters, read);

books.emplace_back(book);

}

// Stroting the info of first book in the variables bookName and read

// Will compare them with other books and decide which book is read the lowest

bookName = books[0].getTitle();

read = books[0].getAmountRead();

for (auto b : books)

{

cout << endl << b.getTitle() << " " << b.getNumChapters() << " " << b.getAmountRead();

if (b.getAmountRead() < read)

bookName = b.getTitle();

}

cout << endl << "The book with the lowest amount read is " << bookName;

return 0;

}