#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <conio.h>
usingnamespace std;
class Books{
private:
string authorName;
string title;
int date , year , month;
public:
Books();
void setAuthorName( string );
void setTitle( string );
void setDate( int );
void setMonth( int );
void setYear ( int );
int getAuthorName();
int getTitle();
int getDate();
int getMonth();
int getYear();
};
Books :: Books(){
authorName = "";
title = "";
date = 0 ;
year = 0 ;
month = 0 ;
}
void Books::setAuthorName( string Name ){
authorName = Name;
}
void Books::setTitle( string theTitle ){
title = theTitle;
}
void Books :: setDate( int thedate ){
date = thedate;
}
void Books :: setMonth( int themonth ){
month = themonth;
}
void Books :: setYear( int theyear ){
year = theyear;
}
use a class to hold the data for each book. Store the entire database of books in a vector in which the vector element is a book object
how i store my books detail into the date and so on?
because while i in main declare like vector<Books> theBooks;
i don't have theBooks.setAuthorName something like that
how i fill in the element?
//Style 2: (used if you've already added the book to the vector)
theBooks[0].setAuthorName("Some_Author");
In short, you can access vector elements using the [] operator exactly the same way you access arrays. You can call functions that are part of that object also.
If you want to input an unlimited amount of books, you need to structure your program to loop, and create the book object inside the loop (so you create a new one every time it loops.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
vector<Books> bookVector;
while(true)
{
Books tempBook;
//fill out book information here
bookVector.push_back(tempBook);
char choice = ' ';
cout << "Would you like to add another book? (Y/N) ";
cin >> choice;
if(choice == 'n' || choice == 'N')
break; //exit while loop, finished adding books.
}
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <conio.h>
usingnamespace std;
class Books{
private:
string authorName;
string title;
int date , year , month;
public:
Books();
void setAuthorName( string );
void setTitle( string );
void setDate( int );
void setMonth( int );
void setYear ( int );
int getAuthorName();
int getTitle();
int getDate();
int getMonth();
int getYear();
};
Books :: Books(){
authorName = "";
title = "";
date = 0 ;
year = 0 ;
month = 0 ;
}
void Books::setAuthorName( string Name ){
authorName = Name;
}
void Books::setTitle( string theTitle ){
title = theTitle;
}
void Books :: setDate( int thedate ){
date = thedate;
}
void Books :: setMonth( int themonth ){
month = themonth;
}
void Books :: setYear( int theyear ){
year = theyear;
}
void MainMenu(){
int choice = 0;
do{
system( "cls" );
cout << "----------------------------\n\tDatabase of Books\n---------------------------- " << endl
<< "1.Add a book's information" << endl
<< "2.Print an alphabetical list of the books sorted by title" << endl
<< "3.Quit" << endl << endl
<< "Enter Choice : ";
cin >> choice;
switch( choice ){
case 1:{
vector<Books> BookVector;
bool match = 1;
string tempName , tempTitle;
int tempDate = 0 , tempMonth = 0 , tempYear = 0;
char choice = ' ';
while( true ){
system ( "cls" );
Books tempBook;
cout << "Enter Author Name : ";
getline( cin , tempName );
tempBook.setAuthorName( tempName );
cout << "Enter Book Title : ";
getline( cin , tempTitle );
tempBook.setTitle( tempTitle );
cout << "\n\tEnter Publication Date\n---------------------------------" << endl;
//Skip validation for year since year cannot be validate
cout << "Year : ";
cin >> tempYear;
//Do validation & input checking on month
do{
cout << "Month : ";
cin >> tempMonth;
}while( tempMonth < 1 || tempMonth > 12 );
//Do validation for every month and their date
do{
cout << "Date : ";
cin >> tempDate;
if( tempMonth == 2 ){
if( tempDate < 1 || tempDate > 29 )
match = false;
}
elseif ( tempMonth == 1 || tempMonth == 3 || tempMonth == 5 || tempMonth == 7 || tempMonth == 8
|| tempMonth == 8 || tempMonth == 10 || tempMonth == 12 )
if( tempDate < 1 || tempDate > 31 ){
match = false;
}
elseif ( tempMonth == 2 || tempMonth == 4 || tempMonth == 6 || tempMonth == 9 || tempMonth == 11 ){
if( tempDate < 1 || tempDate > 30 )
match = false;
}
cout << endl;
}while( match );
tempBook.setDate( tempDate );
tempBook.setMonth( tempMonth );
tempBook.setYear( tempYear );
BookVector.push_back(tempBook);
cout << "Add Record Successfull " << endl << endl;
cout << "Would you like to add another book? (Y/N) ";
cin >> choice;
if( choice == 'n' || choice == 'N')
break; //exit while loop, finished adding books.
}
getch();
break;
}
case 2:
break;
case 3:
getch();
break;
default:
cout << "Invalid input ! Please re-enter ! " << endl;
break;
}
}while( choice != 1 || choice != 2 || choice !=3 );
}
and am i fullfill for this requirement? must use a class to hold the data for each book. Store the entire database of books in a vector in which the vector element is a book object.
If not mistaken it's correct right?