134k views
0 votes
Write a program that uses cin to get the name of a file from the user. 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.

User Jan Katins
by
4.1k points

1 Answer

5 votes

Answer:

See explaination

Step-by-step explanation:

#include<iostream>

#include<vector>

#include<string>

#include<iostream>

#include<fstream>

using namespace std;

class BibleBooks{

public:

BibleBooks(string bk_title, int chapters, double percentage){

title=bk_title;

numChapters=chapters;

amountRead=percentage;

}

string getTitle(){

return title;

}

int getChapters(){

return numChapters;

}

double getPercentage(){

return amountRead;

}

private:

string title;

int numChapters;

double amountRead;

};

int main(){

char filename[200];

cout<<"Enter filename: ";

cin.get(filename,199);

vector<BibleBooks> bibleBooks;

ifstream infile(filename);

if(infile.is_open()){

string title; int chapters; double percentage;

while(infile>>title>>chapters>>percentage){

BibleBooks bb = BibleBooks(title,chapters,percentage);

bibleBooks.push_back(bb);

}

infile.close();

}else{

cout<<"Unable to read data from file: "<<filename;

}

//Your program will then iterate through the vector,

// displaying all of the information about each object.

int index_lowest_read=0;

for(int i=0; i<bibleBooks.size();i++){

cout<<bibleBooks.at(i).getTitle()<<" "

<<bibleBooks.at(i).getChapters()<<" "

<<bibleBooks.at(i).getPercentage()<<endl;

if(bibleBooks.at(index_lowest_read).getPercentage()>

bibleBooks.at(index_lowest_read).getPercentage()){

index_lowest_read=i;

}

}

cout<<"The book with the lowest amount read is "

<<bibleBooks.at(index_lowest_read).getTitle()<<endl;

}

User Perculator
by
4.8k points