#include "structs.h" using namespace std; // this function initialize the array of authors // edit: instead of hard coded values, we took input from the file stream void authors_init(string *a, int size_au, ifstream& fin){ for (int i = 0; i < size_au; ++i) { fin >> a[i]; } } // this funciton initialize ONE book within the book array, at index idx // edit: instead of hard coded values, we took input from the file stream void book_init_one_book(Book* book_arr, int idx, ifstream& fin){ fin >> book_arr[idx].title; fin >> book_arr[idx].pages; fin >> book_arr[idx].num_authors; book_arr[idx].authors = new string [book_arr[idx].num_authors]; authors_init(book_arr[idx].authors, book_arr[idx].num_authors, fin); } //this function initialize the ENTIRE array of books // edit: instead of hard coded values, we took input from the file stream void book_init (Book* book_arr, int size, ifstream& fin) { for (int i = 0; i < size; ++i) { //initialize book_arr[i] fin >> book_arr[i].title; fin >> book_arr[i].pages; fin >> book_arr[i].num_authors; book_arr[i].authors = new string [book_arr[i].num_authors]; //initailize the authors of book_arr[i] authors_init(book_arr[i].authors, book_arr[i].num_authors, fin); } } //print a single book object void print_book (Book &b){ cout << "----------------" << endl; cout << "Pages: " << b.pages << endl; cout << "Title: " << b.title << endl; for (int i = 0; i < b.num_authors; ++i) { cout << "Author " << i+1 << ": "; cout << b.authors[i] << endl; } cout << "----------------" << endl; } //print a single book object void print_book_to_file (Book &b, ofstream& fout){ fout << "----------------" << endl; fout << "Pages: " << b.pages << endl; fout << "Title: " << b.title << endl; for (int i = 0; i < b.num_authors; ++i) { fout << "Author " << i+1 << ": "; fout << b.authors[i] << endl; } fout << "----------------" << endl; } //free the dynamic memory void delete_books(Book* &arr, int size){ for (int i = 0; i < size; ++i) { delete [] arr[i].authors; arr[i].authors = nullptr; } delete [] arr; arr = nullptr; cout << "inside delete: " << arr << endl; }