I understand how to search and sort an array of int type. However I am confused how to sort/ search an array of object's in which I sort/search by a string in the object. (alphabetically) Here is the code I have so far, I want to search and sort the array by the author's last name:
#include <iostream>
using namespace std;
#include <string>
#include "Book.h"
void displayMenu();
int main()
{
const int SIZE = 5;
Book bookList[SIZE];
string locTitle;
string locLname;
string locFname;
int locYear = 0;
int choice = 1;
while (choice != 5)
{
displayMenu();
cin >> choice;
if (choice == 1)
{
int location = 0;
for (int x = location; x < location + 1; x++)
{
cout << "Enter the Title: ";
cin >> locTitle;
cout << "Enter the author's last name: " ;
cin >> locLname;
cout << "Enter the author's first name: " ;
cin >> locFname;
cout << "Enter the year of publication for the book: " ;
cin >> locYear;
cout << "Enter the location of the book, which you would like to overwrite: ";
cin >> location;
bookList[location - 1].setTitle(locTitle);
bookList[location - 1].setAuthor(locLname, locFname);
bookList[location - 1].setYear(locYear);
}
}
if (choice == 2)
{
for (int r = 0; r < SIZE; r++)
{
cout << "Title: " << bookList[r].getTitle() << endl;
cout << "Author Last Name: " << bookList[r].getAuthorLastName() << endl;
cout << "Author First Name: " << bookList[r].getAuthorFirstName() << endl;
cout << "Year: " <<bookList[r].getYear() << endl;
cout << endl;
}
}
if (choice == 3)
{
}
if (choice == 4)
{
}
}
return 0;
}
void displayMenu()
{
cout << "Menu" << endl;
cout << "1. Add a book to the list. " << endl;
cout << "2. Print out the current list of books. " << endl;
cout << "3. Sort the list by the author's last name. " << endl;
cout << "4. Search for books by author. " << endl;
cout << "5. Exit the program. " << endl;
cout << "Enter your choice: ";
}
----------------------------------------
Book.cpp
#include <iostream>
using namespace std;
#include <string>
#include "Book.h"
#include <ostream>
Book::Book()
{
title = "Unknown";
authorLastName = "Doe";
authorFirstName = "John";
year = 1500;
}
Book::Book(string ti, string ln, string fn, int yr)
{
title = ti;
authorLastName = ln;
authorFirstName = fn;
year = yr;
}
Book::Book(const Book& source)
{
title = source.title;
authorLastName = source.authorLastName;
authorFirstName = source.authorFirstName;
year = source.year;
}
void Book::setTitle(string ti)
{
title = ti;
}
void Book::setAuthor(string ln, string fn)
{
authorLastName = ln;
authorFirstName = fn;
}
bool Book::setYear(int yr)
{
bool result = false;
if (year >= 1500 && year <= 2009)
{ result = true;
year = yr;
}
else
result = false;
return result;
}
string Book::getTitle()
{
return title;
}
string Book::getAuthorLastName()
{
return authorLastName;
}
string Book::getAuthorFirstName()
{
return authorFirstName;
}
int Book::getYear()
{
return year;
}
bool Book::operator==(const Book & right)
{
if(title == right.title && authorLastName == right.authorLastName)
return true;
else
return false;
}
-------------------------------------------
book.h
#include <ostream>
using namespace std;
class Book
{
private:
string title;
string authorLastName;
string authorFirstName;
int year;
public:
Book(); // The default constructor
// The default constructor sets the title to “unknown”,
// the author to “John Doe”, and the year to 0
Book(string, string, string, int);
Book(const Book& source); // The copy constructor
// Mutator functions
void setTitle(string);
void setAuthor(string, string);
bool setYear(int); // A valid year is any year from
// 1500 and 2009. The function will set a valid year to the year
// member variable and return true. It not do the set on an invalid
// year, and return false.
// Accessor functions
string getTitle();
string getAuthorLastName();
string getAuthorFirstName();
int getYear();
bool operator==(const Book & right);
};