Sorting book title alphabetically - c++

I use a for loop and if-else statement to try to sort the titles of the books alphabetically. However, I am facing some errors under the returnlistofBooks method. Is there any ways to fix this problem?
voidBookshelf::voidBookshelf(vector <Book*> listofBooks){
this->listofBooks = listofBooks;
}
void voidBookshelf::addBook()
{
int ID;
string Title;
string Author;
for (int i = 1; i <= 5; i++)
{
cout << "Book#"<< i << ":" << endl;
cout << "Enter an ID:";
cin >> ID;
cout << "Enter a title:";
cin >> Title;
cout << "Enter an author:";
cin >> Author;
Book *mybook = new Book(ID, Title, Author); //book object
listofBooks.push_back(mybook);
}
}
void returnListofBooks(int count, string name)
{
Book temp;
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count - i; j++)
{
if (books[j].author > books[j + 1].author)
{
//swapping the instances themselves, but still comparing by the member.
temp = books[j];
books[j] = books[j + 1];
books[j + 1] = temp;
}
}
}
}
int main(){
voidBookshelf * myBookshelf = new voidBookshelf;
myBookshelf->addBook();
myBookshelf->returnListofBooks();
return 0;
}

you can check here for sort function!it is quite simple!
go to:
http://www.cplusplus.com/articles/NhA0RXSz/

An easy method to allow sorting is to overload operator< in your class:
class Book
{
public:
bool operator< (const Book& b)
{
return author < b.author;
}
private:
std::string author;
};
If you want to sort by other fields, you will need to write a custom comparison operator:
class Book
{
std::string title;
friend bool order_by_title(const Book& a, const Book& b);
};
bool order_by_title(const& Book a, const Book& b)
{
return a.title < b.title;
}
std::vector<Book> library;
//...
std::sort(library.begin(), library.end(), order_by_title);

Related

Sorting class array in ascending order

Quite new to programming and in need of some help, I'm looking to sort an class array in ascending order based on age, but I can only get the code to work in descending order. I may be overlooking something small since I've worked on this far too long so I'd appreciate any help!
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
string name;
int age;
Person(string name = "empty", int age = 0)
{
setName(name);
setAge(age);
}
void setName(string x) {
name = x;
}
string getName() {
return name;
}
void setAge(int y) {
age = y;
}
int getAge() {
return age;
}
void displayinfo()
{
cout << "Name: " << name; cout << " Age: " << age << endl;
}
};
void swap(Person &p, Person &q)
{
Person temp;
temp.name = p.name;
temp.age = p.age;
p.name = q.name;
p.age = q.age;
q.name = temp.name;
q.age = temp.age;
}
int main()
{
int userValue;
Person po("Jessica", 24);
Person po2("Robert", 49);
Person po3("Maria", 47);
Person po4("John", 19);
Person family[4] = {po,po2,po3,po4};
int sort(family[4].getAge());
{
for(int i = 0; i < 3; i++)
if (family[i].getAge() < family[i+1].getAge())
{
swap(family[i], family[i+1]);
}
}
for(int i = 0; i < 4; i++)
family[i].displayinfo();
}
int sort(family[4].getAge()); is not a function declaration (and you can't implement a function inside of another function). It is actually a variable declaration. It is declaring a variable int sort that is initialized with the value from family[4].getAge() (which is undefined behavior since index 4 is out of bounds of your family[] array).
So, you are declaring an unused sort variable, and then you enter your for(int i = 0; i < 3; i++) loop, which DOES NOT perform a full sort of the array. For what you are attempting, use the standard std::sort() algorithm instead, eg:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class Person
{
public:
string name;
int age;
Person(string name = "empty", int age = 0)
{
setName(name);
setAge(age);
}
void setName(string x) {
name = x;
}
string getName() const {
return name;
}
void setAge(int y) {
age = y;
}
int getAge() const {
return age;
}
void displayinfo() const
{
cout << "Name: " << name; cout << " Age: " << age << endl;
}
};
int main()
{
Person po1("Jessica", 24);
Person po2("Robert", 49);
Person po3("Maria", 47);
Person po4("John", 19);
Person family[4] = {po1, po2, po3, po4};
std::sort(family, family + 4,
[](const Person &p1, const Person &p2) {
return p1.getAge() < p2.getAge();
}
);
for(int i = 0; i < 4; i++) {
family[i].displayinfo();
}
return 0;
}
Live Demo
Note that your family[] array holds copies of the Person objects that you initialize it with. To avoid the overhead of those copies, you can sort pointers instead, eg:
int main()
{
Person po1("Jessica", 24);
Person po2("Robert", 49);
Person po3("Maria", 47);
Person po4("John", 19);
Person* family[4] = {&po1, &po2, &po3, &po4};
std::sort(family, family + 4,
[](const Person *p1, const Person *p2) {
return p1->getAge() < p2->getAge();
}
);
for(int i = 0; i < 4; i++) {
family[i]->displayinfo();
}
return 0;
}
Live Demo
Or, you can get rid of the individual p0... objects altogether and initialize the array directly instead, eg:
int main()
{
Person family[4]{
{"Jessica", 24},
{"Robert", 49},
{"Maria", 47},
{"John", 19}
};
std::sort(family, family + 4,
[](const Person &p1, const Person &p2) {
return p1.getAge() < p2.getAge();
}
);
for(int i = 0; i < 4; i++) {
family[i].displayinfo();
}
return 0;
}
Live Demo

Program runs through compiler but not giving answers(C++ Classes)

I'm new to C++ OOP and I find difficulties in implementing the following task:
The program is about taking information from a file, storing it in vector pointers and then printing it, but it gives proccess failure.
Basically doesn't call the second constructor in class Car and I don't seem to find a way to fix this issue.
Would like to get some tips.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class Person{
protected:
string name;
int age;
public:
Person(string n, int a) {
name = n;
age = a;
cout << "Constructor called" << endl;
}
void IncAge(){
age++;
}
string get_name() {
return name;
}
int get_age() {
return age;
}
};
class Car{
public:
string brand;
Person* owner;
Person* driver;
Car(string b, Person* o, Person* d){
brand = b;
owner = o;
driver = d;
cout << "Constructor 2 called" << endl;
}
string get_brand(){ return brand; }
void get_owner(){ cout << owner; }
void get_driver(){ cout << driver; }
};
void printall(vector<Car*> car){
for(int i =0; i < car.size(); i++){
cout << car[i]->get_brand();
}
}
int main() {
vector<Person*> people;
vector<Car*> cars;
ifstream openFile;
string filename = "input.txt";
openFile.open(filename.c_str());
if(openFile.fail()){
cout << "Failed opening targeted file!" << endl;
return 1;
}
else{
int n;
int read_age;
string read_name, read_car, read_owner,read_driver;
openFile >> n;
for(unsigned int i = 0; i < n; i++){
openFile >> read_name;
openFile >> read_age;
people[i] = new Person(read_name, read_age);
}
openFile >> n;
for(unsigned int i = 0; i < n; i++){
openFile >> read_car;
openFile >> read_owner;
openFile >> read_driver;
unsigned int j = 0;
unsigned int k = 0;
for(; j < people.size(); j++){
if(read_owner == people[j]->get_name()) break;
}
for(; k < people.size(); k++){
if(read_driver == people[k]->get_name()) break;
}
cars[i] = new Car(read_car, people[j], people[k]);
}
openFile.close();
for(unsigned int i = 0; i < people.size(); i++) {
people[i]->IncAge();
}
printall(cars);
}
return 0;
}
I am also learning c++, I want to comment instead of writing this as an answner, but I can't do it now.
people[i] = new Person(read_name, read_age);
use push_back() function to add person to vector
I don't think [index] will properly increase the size of vector and reallocate memory for new element

C++ Overloading operator >> (input) doesn't change original

I'm trying to build a custom dictionary class with custom string and definition classes. While trying to overload the >> (input) I get some kind of a problem. when the function ends the dictionary sent to it doesn't change. attaching the code:
This is the overloading:
istream& operator>>(istream& ip, Dictionary& var) {
Definition temp;
ip >> temp;
var += temp;
return ip;
}
and some other functions used in it:
Dictionary& Dictionary::operator+=(Definition& input) {
if (!checkCopy(input))
{
Definition** temp;
temp = new Definition*[numWords + 1];
for (int i = 0; i < numWords; i++)
{
temp[i] = book[i];
}
numWords++;
temp[numWords - 1] = &input;
delete[] book;
book = temp;
}
return *this;
}
Dictionary::Dictionary(Dictionary& input) {
*this = input;
}
Dictionary& Dictionary::operator=(Dictionary& input) {
if (numWords != 0)
delete[] book;
book = new Definition*[input.numWords];
for (int i = 0; i < input.numWords; i++)
{
*this += *input.book[i];
}
return *this;
}
And the class itself:
class Dictionary
{
private:
int numWords = 0;
Definition** book;
public:
Dictionary();
~Dictionary();
Dictionary(Dictionary&);
bool operator==(Dictionary&) const;
Dictionary& operator=(Definition&);
Dictionary& operator=(Dictionary&);
friend ostream& operator<<(ostream&, const Dictionary&);
friend istream& operator>>(istream&, Dictionary&);
Dictionary& operator-=(int);
Dictionary& operator+=(Definition&);
bool checkCopy(Definition&);
Definition& operator[](int); //left side brackets for input
Definition operator[](int) const; //right side brackets for output
};
EDIT: here is also the overloading operator for definition input:
istream& operator>>(istream& ip, Definition& var)
{
cout << "Please enter a word: " << endl;
ip >> var.word;
cout << "Please enter the number of definitions for this word: " << endl;
int idx;
cin >> idx;
while (idx<0)
{
cout << "Error: number of definitions not possible. Please Try again: " << endl;
cin.clear();
cin.ignore(INT_MAX, '\n');
cin >> idx;
}
cin.clear();
cin.ignore(INT_MAX, '\n');
String* temp = new String[idx];
for (int i = 0; i < idx; i++) {
cout << "Please enter the " << i + 1 << "th definition: " << endl;
cin >> temp[i];
var += temp[i];
}
var.sortDefinition();
return ip;
}
Help is indeed needed.
You should really stick to std::vector and other collection types rather than juggling around with pointers and new/delete as you do here.
In your operator+= function you're copying the address of a temporary variable into your array:
temp[numWords - 1] = &input;
Once the calling function operator>> ends, this pointer is less than worthless, because the original object (Definition temp;) does not exist any longer. Therefore the behaviour of that code is undefined!
You might get around this by defining a copy c'tor for Definition and then changing above line to:
*temp[numWords - 1] = input;
Also in your assignment operator you're making use of the operator+= function. However, your numWords member is not set appropriately at this time, so operator+= will likely do the wrong thing. So add a line to the assignment operator like this:
if (numWords != 0)
{
delete[] book;
numWords = 0; // add this line
}
There were 2 problems:
what Alexander said about the temporary variable. changed it to:
Dictionary& Dictionary::operator+=(Definition& input) {
if (!checkCopy(input))
{
Definition** temp;
temp = new Definition*[numWords + 1];
temp[0] = new Definition[numWords];
for (int i = 0; i < numWords; i++)
{
temp[i] = book[i];
}
temp[0][numWords] = input;
delete[] book;
book = temp;
numWords++;
}
return *this;
}
The second was that in the Definition class when I tried to access the number of definitions in an object that wasn't created due to the double pointer:
Definition** temp;
temp = new Definition*[numWords + 1];
So I changed it so it won't access it but first build it.
Thanks for the help Alexander!

Class with Pointer and Dynamic Arrays

I'm currently writing a program that will help a college keep track of which students are in what clubs. This code will be pasted into existing code that works, as provided by my professor.
Could someone please look my code over, and help me work through my mistakes? I included comments to explain what everything should do.
Also, please note this is an assignment and I cannot change anything about the class. I'm also a beginner, so keep it simple, please. Your help will be extremely appreciated beyond all measure.
class Club
{
public:
Club();
Club(Club &c);
Club(string cname);
void addMember(string name);
void removeMember(string name);
string getClubName() const;
string setClubName(const string& nameOfClub);
void loadClub();
bool isMember(string& name) const;
string getAllMembers() const;
friend Club mergeClubs(Club& c1, Club& c2);
~Club();
private:
string *members;
int numMembers;
string clubName;
};
Club::Club()
{
clubName = "";
numMembers = 0;
}
Club::Club(Club &c)
{
numMembers = c.numMembers;
members = new string [numMembers];
for (int i = 0; i < numMembers; i++)
{
members[i] = c.members[i];
}
//copy constructor
//watch out for memory leaks
}
Club::Club(string cname)
{
clubName = cname;
//cname should be saved as the club's name in the clubName variable
}
void Club::addMember(string name)
{
string *m;
m = new string [numMembers];
string *members = m;
for (int i = 0; i < numMembers; i++)
{
for (int j = 0; j < name.length(); i++)
{
m[i] = name[j];
}
}
delete [] m;
//adds new member to the club
//records their name in the members variable
//may need a dynamic array to make this work, watch for memory leaks!
}
void Club::removeMember(string name)
{
string *m;
m = new string [numMembers];
string *members = m;
for (int i = 0; i < numMembers; i++)
{
if ( m[i] == name)
{
m[i] = "";
}
}
delete [] m;
//deletes the person from the array in members
//will do nothing if the person is not in the array to begin with
//may require dynamic array to make this work- watch for memory leaks!
//if the person's name appears more than once, just delete the first instance
}
string Club::getClubName() const
{
return clubName;
//getter of clubName
}
string Club::setClubName(const string& nameOfClub)
{
return clubName = nameOfClub;
//setter of clubName
}
void Club::loadClub()
{
//should print "tell me the name of the next member of the club"
//reads into the variable name
//uses addMember() to add that person to the club
//the input should include up to the line break as the name, so it should take in "jane doe" as an entry
//keeps asking for more names until the user enters a blank entry
string name;
do
{
cout << "Tell me the name of the next member of the club, ";
cout << "or submit a blank entry to stopent ering names." << endl;
getline(cin, name, '\n');
addMember(name);
} while (name != "");
}
bool Club::isMember(string& name) const
{
/*for (int i = 0; i < numMembers; i++)
{
for (int j = 0; j < name.length(); j++)
{
if (members[i] == name)
{
return true;
}
else
{
return false;
}
}
}*/
for (int i = 0; i < numMembers; i++)
{
if (members[i] == name)
{
return true;
}
return false;
}
//returns true if the person is a member of the club, false otherwise
}
string Club::getAllMembers() const
{
for (int i = 0; i < numMembers; i++)
{
return members[i];
cout << ", ";
}
cout << endl;
//returns a string of all the names of the members of the club
//commas and spaces separating every entry of the list
//should not be a comma following the last name in the list
}
Club mergeClubs(Club& c1, Club& c2)
{
//creates a new club from 2 existing clubs
//combined club name should be Club 1/Club 2
Club temp;
temp.clubName = c1.clubName + "/" + c2.clubName;
return temp;
}
Club::~Club()
{
delete [] members;
//destructor
//watch out for memory leaks
}
For some reason I cannot fathom I have (mostly) corrected this program for you. There are still some nasty things like passing copies of strings into const functions but fixing these would be mere optimisations. At least this program is logically correct.
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
using namespace std;
class Club
{
public:
Club();
Club(const Club &c);
Club(string cname);
void addMember(string name);
void removeMember(string name);
string getClubName() const;
string setClubName(const string& nameOfClub);
void loadClub();
bool isMember(const string& name) const;
string getAllMembers() const;
friend Club mergeClubs(Club& c1, Club& c2);
~Club();
private:
vector<string> members;
string clubName;
};
Club::Club()
: members()
, clubName()
{
}
Club::Club(const Club &c)
: members(c.members)
, clubName(c.clubName)
{
}
Club::Club(string cname)
: members()
, clubName(cname)
{
}
void Club::addMember(string name)
{
members.push_back(name);
}
void Club::removeMember(string name)
{
members.erase(remove(members.begin(), members.end(), name), members.end());
}
string Club::getClubName() const
{
return clubName;
//getter of clubName
}
string Club::setClubName(const string& nameOfClub)
{
return clubName = nameOfClub;
//setter of clubName
}
void Club::loadClub()
{
//should print "tell me the name of the next member of the club"
//reads into the variable name
//uses addMember() to add that person to the club
//the input should include up to the line break as the name, so it should take in "jane doe" as an entry
//keeps asking for more names until the user enters a blank entry
string name;
do
{
cout << "Tell me the name of the next member of the club, ";
cout << "or submit a blank entry to stopent ering names." << endl;
getline(cin, name, '\n');
addMember(name);
} while (name != "");
}
bool Club::isMember(const string& name) const
{
return find(members.begin(), members.end(), name) != members.end();
}
string Club::getAllMembers() const
{
stringstream result;
vector<string>::const_iterator b = members.begin(), e = members.end();
for (bool comma = false ; b != e; ++b, comma = true)
{
if (comma) {
result << ", ";
}
result << *b;
}
return result.str();
}
Club mergeClubs(Club& c1, Club& c2)
{
Club temp(c1.clubName + "/" + c2.clubName);
struct memberAdd {
Club& _club;
memberAdd(Club& club) : _club(club) {}
void operator()(const string& member) {
_club.addMember(member);
}
};
for_each(c1.members.begin(), c1.members.end(), memberAdd(temp));
for_each(c2.members.begin(), c2.members.end(), memberAdd(temp));
return temp;
}
Club::~Club()
{
//destructor
//watch out for memory leaks
}
int main()
{
Club boys("red");
boys.addMember("Ben");
boys.addMember("Paul");
Club girls("blue");
girls.addMember("Lucy");
girls.addMember("Hermione");
Club unisex = mergeClubs(boys, girls);
cout << unisex.getClubName() << " has the following members: " << unisex.getAllMembers() << endl;
}
Some initial comments:
The copy constructor should take a const Club& like this:
Club(const Club& club)
members and numMembers are anachronistic. Use a std::vector for members and drop the numMembers altogether (std::vector has a size() method).
Club::Club does not initialise members or numMembers. This will result in a possible crash in the destrutor. This would be solved if you replaced them with a vector.
the logic in addMember does not make sense.
nor in removeMember
isMember should take a const string& and you would be well advised to write it in terms of std::find() (#include <algorithm>)
I could go on...

Deleting a record from array of pointers of my own class type

I have created an Employee class:
class Employee {
private:
int idNumber;
string name, department, position;
public:
Employee() {
idNumber = 0;
name = department = position = "";
}
Employee(string n, int idn) {
name = n;
idNumber = idn;
department = position = "";
}
Employee(string n, int idn, string dep, string pos) {
name = n;
idNumber = idn;
department = dep;
position = pos;
}
void setName(string n) {
name = n;
}
void setidNumber(int idn) {
idNumber = idn;
}
void setDepartment(string dep) {
department = dep;
}
void setPosition(string pos) {
position = pos;
}
string getName() {
return name;
}
int getidNumber() {
return idNumber;
}
string getDepartment() {
return department;
}
string getPosition() {
return position;
}
};
Now, i created a 2D array of Pointers of type Employee:
int n=2;
Employee **p = new Employee * [n];
for (int i=0; i < n; i++)
p[i] = new Employee;
I stored two records successfully as under:
Name ID Number Department Position
FS 30 CS BS
AT 27 CS BS
I have this code to delete the record of Employees:
string del_name;
int flag = 0;
cin.ignore();
cout << "Enter name: ";
getline(cin, del_name);
for (int i=0; i < n; i++) {
while (del_name == p[i]->getName() && i < n) {
if (del_name == p[i]->getName()) {
delete p[i];
p[i] = NULL;
--k;
++flag;
cout << "Record deleted." << endl;
break;
}
else
{
flag = 0;
}
}
}
if (flag == 0)
cout << "No record found having name " << del_name << "." << endl;
Now, What's the problem:
If a record is found at multiple times. It deletes successfully even if all the records gets deleted.
But if ALL the records are unique and I delete the records one by one and all the records get deleted in this way then the program gets terminated.
Also, is there any other optimized approach to delete records without using VECTORS.
I hope i have clearly explained my problem. I can provide further details if needed.
Thank you for your time
First, usage of std::vector<> or some other container object is the way to go about this. If you can write code that beats (in terms of speed) written by professional library writers, then go ahead.
Second, what is your goal? If it's to simply deallocate entries in that array depending on some criteria, the loop you wrote is overly complex.
bool recordDeleted = false;
for (int i=0; i < n; ++i)
{
if (del_name == p[i]->getName())
{
delete p[i];
p[i] = NULL;
recordDeleted = true;
}
}
if ( !recordDeleted )
{
// record not found
}