As of above but i want to pass tat array of class object into a function by Reference.
Oh yea, I'm on an assignment which i've to write a program to store and manage books in a library system.
Here's my code:
Code:
class Book{
public:
Book(){};
void setBookInfo(string, string, float, string);
void getBookInfo(string&, string&, float&, string&);
static int maxBooks, bookCounter;
private:
string title, author, category;
float price;
};
void addNewBook(Book[]);
int main (void){
Book libraryBooks[Book::maxBooks];
char choice = ' ';
do{
cout << "\t--->\tWelcome to eLibrary!\t<---\t" << endl;
cout << "Press:" << endl;
cout << "\tA --> to Add New Book" << endl;
cout << "\tB --> to Update a Book Info" << endl;
cout << "\tC --> to Delete a Book" << endl;
cout << "\tD --> to Search for a Book" << endl;
cout << "\tE --> to Display All Books" << endl;
cout << "\tF --> to Display Library Statistics" << endl;
cout << "\tQ --> to Quit the program" << endl;
choice = getch();
system("cls");
switch(choice){
case 'a':
addNewBook(libraryBooks);
break;
case 'b':
updateBook(libraryBooks);
break;
case 'c':
break;
case 'd':
break;
case 'e':
break;
case 'f':
break;
case 'q':
break;
}
} while (choice != 'q');
system("pause");
return 0;
}
void Book::setBookInfo(string setTitle, string setAuthor, float setPrice, string setCategory){
title = setTitle;
author = setAuthor;
price = setPrice;
category = setCategory;
}
void Book::getBookInfo(string& getTitle, string& getAuthor, float& getPrice, string& getCategory){
getTitle = title;
getAuthor = author;
getPrice = price;
getCategory = category;
}
void addNewBook(Book libraryBooks[]){
string strTitle = "", strAuthor = "", strCategory = "";
float fltPrice = 0.0;
cout << "Enter the Book Title: ";
fflush(stdin);
getline(cin, strTitle);
for (int i = 0; i < sizeof(libraryBooks) ; i++){
string strTempTitle, strTempAuthor, strTempCategory;
float fltTempPrice;
libraryBooks[i].getBookInfo(strTempTitle, strTempAuthor, fltTempPrice, strTempCategory);
if (strTitle == strTempTitle){
cout << "This book " << strTitle << " has already been stored into the library." << endl;
cout << "Please enter the Book Title again: ";
fflush(stdin);
getline(cin, strTitle);
break;
}
}
cout << "Enter the Book Author: ";
fflush(stdin);
getline(cin, strAuthor);
cout << "Enter the Book Price: ";
fflush(stdin);
cin >> fltPrice;
cout << "Enter the Book Category: ";
fflush(stdin);
getline(cin, strCategory);
libraryBooks[Book::bookCounter].setBookInfo(strTitle, strAuthor, fltPrice, strCategory);
Book::bookCounter++;
cout << "\n" << endl;
cout << "Book added successfully!" << endl;
system("cls");
}
so how do i pass tat array of Book object into addNewBook() by reference?
feel free to give any comments! thanks~