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

i wanted to know what is wrong or missing in my code #include<iostream> #include

ID: 3749631 • Letter: I

Question

i wanted to know what is wrong or missing in my code


#include<iostream>
#include<string>
using namespace std;
struct book
{
string title;
string author;
};
class library
{
private:
string address;
string name;
book *content;
int num_book;
  
  
public:
  
library();
void set_address(string passsed_address);
void set_name(string passsed_name);
void set_num_books(int passed_number_books);
void set_specific_book_info( string title , string author , int book_number);
string get_address();
string get_name();
book* get_content();
int get_num_books();

  
};
library::library()
{
address=" ";
name=" ";
num_book=0;
content=NULL;
}


void library::set_address(string passsed_address)
{
address=passsed_address;
}

void library::set_name(string passsed_name)
{
name=passsed_name;
}

void library::set_num_books(int passed_number_books)
{
num_book=passed_number_books;
  
}

void library::set_specific_book_info( string title , string author , int book_number)
{
content=new book[book_number];
for(int i=0;i<book_number;i++)
{
cout<<"Enter book's title:";
cin>>content[i].title;
cout<<"Enter book's author:";
cin>>content[i].author;
}
}


string library:: get_address()
{
return address ;
}
string library:: get_name()
{
return name;
}
book* library::get_content()
{
return content;
}
int library:: get_num_books()
{
return num_book;
}
int main()
{
int libraries_number, number_books;
string library_name, library_address ;
book b;
cout << " Enter the number of libraries " << endl;
cin >> libraries_number;
library *groupLibraries = new library[libraries_number];
for (int i = 0; i < libraries_number; i++)
{
cout << " please enter the address , name for library " <<i << endl;
cin>>library_address;
groupLibraries[i].set_address(library_address);
cin>>library_name;
groupLibraries[i].set_name(library_name);
cout << " Enter the number of books you want to be in for library " << i <<endl;
cin>>number_books;
groupLibraries[i].set_num_books(number_books);

  
for (int j = 0; j < number_books; j++)
{
cout << " Enter book info as title and author " << endl;
groupLibraries[j].set_specific_book_info(b.title,b.author, number_books);
  
}
  
}

book * tempContent;
for (int i = 0; i < libraries_number; i++)
{
cout << " Library " << i << " info : " << endl;
  
cout << groupLibraries[i].get_name();
cout<< " " << groupLibraries[i].get_address();
tempContent = groupLibraries[i].get_content();
cout << endl;
cout << " books info :" << endl;
for (int j = 0; j < groupLibraries[i].get_num_books(); j++)
{
cout << tempContent[j].author << " " << tempContent[j].title << endl;
}
  
}
  
  
  
  
  
return 0;
}

the question was

Write a C++ program that defines a struct named bookwith title and author as attributes. Then define a class named librarywith private data members as (name , address and numberOfbooks, and a pointer of books named content).
Your class must contain setters and getters to each member attribute.
Notes:

In set_number_of_Books, you should initialize the dynamic pointer using the member attribute numberOfbooks

In set_specific_book_info which is the function that should fill a specific cell of the contentarray, must take the index of the book it will set with specific author and title.

In main, please let the user enter the number of the required libraries which means you will define a pointer to libraries.

Explanation / Answer

This for loop is wrong:-

for (int j = 0; j < number_books; j++)
{
cout << " Enter book info as title and author " << endl;
groupLibraries[j].set_specific_book_info(b.title,b.author, number_books);  
}

In this you are passing number_books as third argument to set_specific_book_info method Due to this data will be duplicated but i think we should pass the j variable as third arguments.So that this programs will work correctly.

If a particular book number was passed then this method set_specific_book_info sets the correct title and author to a particular book and data will be displayed correctly.

void library::set_specific_book_info( string title , string author , int book_number)
{
content=new book[book_number];
for(int i=0;i<book_number;i++)
{
cout<<"Enter book's title:";
cin>>content[i].title;
cout<<"Enter book's author:";
cin>>content[i].author;
}
}