Hello I need help with these questions. I am still a beginner with the c++ langu
ID: 3756041 • Letter: H
Question
Hello I need help with these questions. I am still a beginner with the c++ language and im having problems trying to understand what to do here. IF you can include comments for what you did and how that would be appreciated! Will upvote thank you.
Book.h
#ifndef BOOK_H
#define BOOK_H
#include <string>
using namespace std;
class Book
{
public:
Book(int=0, string="Unknown", string="Unknown", int=0);
void setBook(int, string, string, int);
void print();
private:
int id;
string title;
string author;
int year;
};
#endif
~
Book.cc
#include <iostream>
#include <iomanip>
using namespace std;
#include "Book.h"
Book::Book(int i, string t, string a, int y)
{
id = i;
title = t;
author = a;
year = y;
}
void Book::setBook(int i, string t, string a, int y)
{
id = i;
title = t;
author = a;
year = y;
}
main.cc
#include <iostream>
using namespace std;
#include <string>
#include "Book.h"
#define MAX_ARR_SIZE 128
int mainMenu();
void printLibrary(Book arr[MAX_ARR_SIZE], int num);
int main()
{
Book library[MAX_ARR_SIZE];
int numBooks = 0;
string title, author;
int id, year;
int menuSelection;
while (1) {
menuSelection = mainMenu();
if (menuSelection == 0)
break;
else if (menuSelection == 1) {
cout << "id: ";
cin >> id;
cout << "title: ";
cin.ignore();
getline(cin, title);
cout << "author: ";
getline(cin, author);
cout << "year: ";
cin >> year;
library[numBooks].setBook(id, title, author, year);
++numBooks;
}
}
if (numBooks > 0)
printLibrary(library, numBooks);
return 0;
}
int mainMenu()
{
int numOptions = 1;
int selection = -1;
cout << endl;
cout << "(1) Add book" << endl;
cout << "(0) Exit" << endl;
while (selection < 0 || selection > numOptions) {
cout << "Enter your selection: ";
cin >> selection;
}
return selection;
}
void printLibrary(Book arr[MAX_ARR_SIZE], int num)
{
cout << endl << endl << "LIBRARY: " << endl;
for (int i=0; i<num; ++i)
arr[i].print();
cout << endl;
}
Makefile
OPT = -Wall
t01: main.o Book.o
g++ $(OPT) -o t01 main.o Book.o
main.o: main.cc Book.h
g++ $(OPT) -c main.cc
Book.o: Book.cc Book.h
g++ $(OPT) -c Book.cc
clean:
rm -f *.o t01
Explanation / Answer
Book.h // header file
Create the header file Book.h save it with that name below is header file and Book.cpp and main.cpp file each file save with separate in one folder. Below is the all files.
In header file only method declaration and variable declarations.
Book.h
#ifndef BOOK_H
#define BOOK_H
#include <string>
using namespace std;
class Book // Book class
{
public:
Book(int=0, string="Unknown",string="Unknown", int=0); // Book constructor
void setBook(int, string, string, int); //setBook method will set the bookid, title, author, year
void print(); // prints the book info added
private:
// Declare the id, title, author year
int id;
string title;
string author;
int year;
};
#endif
~
//Book.cpp file will implement the method which in declare in Book.h header file
Book.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#include "Book.h"
Book::Book(int i, string t, string a, int y) //Constructor will default set the book details which is id,title,author, year
{
id = i;
title = t;
author = a;
year = y;
}
void Book::setBook(int i, string t, string a, int y) //set the book details
{
id = i;
title = t;
author = a;
year = y;
}
main.cpp
#include <iostream>
using namespace std;
#include <string>
#include "Book.h"
#define MAX_ARR_SIZE 128 //set the size
int mainMenu(); //method declare mainMenu
void printLibrary(Book arr[MAX_ARR_SIZE], int num); // printLibrary method will take the array of book and num
int main() // main method
{
Book library[MAX_ARR_SIZE];// create a object of Book class
int numBooks = 0; //declare the numBook and set to 0
string title, author; // declare a title,year variable
int id, year; // declare id,year
int menuSelection; //variable menuSelection
while (1) { // run the loop continuously until user select 0 means exit
menuSelection = mainMenu(); // initialize the mainMenu() value into menuSelection
//if menu selection has 0 the loop will terminate
if (menuSelection == 0)
break;
else if (menuSelection == 1) { //menuSelection has 1 the if condition will execute and take the book info input from the user input
cout << "id: "; // input id
cin >> id;
cout << "title: "; //input book title
cin.ignore();
getline(cin, title);
cout << "author: "; //input author of book
getline(cin, author);
cout << "year: "; // input year
cin >> year;
library[numBooks].setBook(id, title, author, year); // call the setBook() method to set the book info in library array object
++numBooks; increase the nuBooks by one
}
}
if (numBooks > 0) // if numBooks has greater than 0 the prints the book info by using printLibrary method
printLibrary(library, numBooks);
return 0;
}
int mainMenu() // main menu method
{
int numOptions = 1; // declare the numOptuons and set to 1
int selection = -1; // declare selection and set to -1
cout << endl;//new line
//Prints the menu selection
cout << "(1) Add book" << endl;
cout << "(0) Exit" << endl;
while (selection < 0 || selection > numOptions) { // check if the selection is less than 0 or selection is greater than numOption
cout << "Enter your selection: "; // ask the user to enter selection menu
cin >> selection;
}
//When user enter the selection option it will return to the method
return selection;
}
//printLibrary method will take the Book array object and num to prints the entered book info using for loop one by one
void printLibrary(Book arr[MAX_ARR_SIZE], int num)
{
cout << endl << endl << "LIBRARY: " << endl;
for (int i=0; i<num; ++i)
arr[i].print();
cout << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.