Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I don't know if this problem is still hard for people that already know C++ but I'd like to learn how this code works.

Hi, I don't know if this problem is still hard for people that already know C++ but I'd like to learn how this code works. Please include comments of what you're doing so I can study the code and understand it. It's preferable that you type the code in your response rather than a screenshot. Thank you very much! image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

heres split from last time:

int split (string str, char c, string array[], int size)

{

if (str.length() == 0) {

return 0;

}

string word = "";

int count = 0;

str = str + c;

for (int i = 0; i

{

if (str[i] == c)

{

if (word.length() == 0)

continue;

array[count++] = word;

word = "";

} else {

word = word + str[i];

}

}

return count;

}

Problem 5 (20 points) readBooks Write a function readBooks that populates a pair of arrays with the titles and authors found in books.txt. This function should . Accept five input arguments in this order: o string fileName: the name of the file to be read o Array of string: titles o Array of string: authors. o int numBookStored: the number of books currently stored in the arrays You can always assume this is the correct number of actual elements in the arrays o int size: capacity of the authors/titles arrays. This number should always be greater or equal to the number of books currently stored in the . Use ifstream and getline to read data from the file, placing author names in the . You can use the split function from Problem 3, with comma () as the arrays authors array and titles in the titles array delimiter. When you copy your code to the coderunner, make sure you put in the answer box both functions readBooks and split Special cases to consider: . o When the file is not opened successfully, return -1 o When numBookStored is equal to the size, return -2 o When numBookStored is smaller than size, keep the existing elements in titles and authors, then read data from file and add the data to the array. The number of books stored in the array cannot exceed to the size of the array . Empty lines should not be added to the arrays Example 1: The authors and titles arrays are empty, so numBookStored is zero fileName.txt Author A, Book 1 Author B, Book 2 Function call string authors [10] = { } ; string titles[10]-; readBooks ("fileName.txt", titles, authors, 0, 10); Return value 2 Value of authors "Author A", "Author B") Value of titles ("Book 1", "Book 2" Example 2: There's already one book and one author in the arrays, so the data read from the file will be added to the array fileName.txt Author A, Book 1 Author B, Book 2 Function call string authors [10] ("Author Z") string titles[10]-"Book N"); readBooks ("fileName.txt", titles,authors, 1, 10) Return value Value of authors ("Author Z", "Author A", "Author B" Value of titles ("Book N", "Book 1", "Book 2" Example 3 There's already one book and one author in the arrays, so numBookStored is one. However, the array size is only two, so only the first line of the file is stored fileName.txt Author A, Book 1 Author B, Book2 Author C, Book 3 Function call string authors [2] -"Author Z" string titles [2] "Book N" readBooks ("fileName.txt", titles, authors, 1, 2); Return value Value of authors ("Author Z", "Author A") Value of titles "Book N", "Book 1" Example 4: file does not exist. Function call string authors [2] -"Author Z") string titles [2] -"Book N"; readBooks ("badFileName.txt",titles,authors, 1, 2) Return value Value of authors "Author Z") Value of titles ("Book N") Example 4: file does not exist. Function ca string authors [2"Author 2" string titles [2] -"Book N" readBooks ("badFileName.txt",titles,authors, 1, 2) Return value Value of authors "Author Z") Value of titles ("Book N" Example 5: numBookstored equals size means the array is already full. fileName.txt Author A, Book1 Author B, Book2 Author C,Book3 Function call string authors [ 1 ] String titles[1]= {"Book N"); readBooks ("fileName.txt", titles, = { "Author z" } ; . authors, 1, 1); Return value 2 Value of authors ("Author 2") Value of titles ("Book N

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Experts Guide To Database 2

Authors: Bruce L. Larson

1st Edition

0070232679, 978-0070232679

More Books

Students also viewed these Databases questions