#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 );
}
. To sort the data, use the generic sort function from the <algorithm> library. Note that this requires you to define the < operator to compare two objects of type Book so that the title field from the two books are compared.
how to sort for the alphabetically for string title?
for the compare two objects of type Book
is mean that using
Which part is it exactly that you're having trouble with? So far you've shown your code, and told us your assignment. You haven't said what part you want help with.
As question stated above.
just now i didn't not bold it.
Sort the data with algorithm library.
using sort( BookVector.begin() , BookVector.end() ) something like this?
but i don't know how to sort with compare only the Book Title and not the object name . can u see the compare two objects of type Book
is mean that using
What & Why
Do you just need the title to be sorted? Do you need to store the sorted data back into the same data set? Do you need the entire record associated with the title to also be sorted?
Normally the DBA controls the data-set and it's related indexes and keys, so that you as a programmer only has to attach the database and access the right key or index and voila, it's done except for printing.
If the sort is not going to be kept/ maintained, then you could simply strip off the Titles and preform a bubble or binary sort on the result set.
If the only requirement will be to Alpha sort the books by Title, then you may wish to remove the option from the menu and insert a call to the sort fnc() within the ADD subrt'n - so that new additions are sorted as they are added.
However, if you wish to expand the sort to include the users input as to how the books are sorted, i.e. by Title, by Author, by Year, by Subject, then your existing menu layout works fine - provided it's expanded for options.
From what I understand you need to sort a vector of your book object by Title. All you need to do is overload the greater-than operator, and then use the sort function from the algorithms library like you would normally.
@script coder. yours 1 i think i more understand about it.
can u just simply to provide a simple code that using < operator ?
because for me i only know to use this two friend &ostream operator <<( ostream & a,d object & );
i still haven't use the < operator
can you teach me base on my code ?
or someone might help me also? thanks
but then i need to compare if i have a lot of objects . but for this operator only two objects are allowed to compare? did i implement wrong?
It should only compare 2 objects at one time. However, you did implement it wrong. You made it a recursive call to itself. Recall that you're supposed to be sorting based on the title of the book.
1 2 3 4
booloperator<(const Books& a, const Books& b)
{
return a.title < b.title ;
}
2) If you want to sort it used std::sort, you will need to define an operator < for your Book class so that they can be compared. Once you do that, std::sort should function fine.
ya . i did the bool operator already .
erm .
the sorting is base for my code just that sorting(BookVector.begin() , BookVector.end() , ??);
what should i put for the ?? since i saw some sources that got put +4 . or + something?