Hello,
I'm working on an assignment involving reading an input file, sorting the data, and outputting the data all while using topics such as struct, class, and header files. I can't for the life of me figure out where my code is going wrong here. Sorting by title works perfectly and sorts the titles accordingly. However, sorting by author and genre does not work and I don't understand why. I'm not expecting any hands out here, but any hints at a specific part of my code that is causing this would be very appreciated or any direction as to where I am writing bad code.
This is the contents of my input file
Starting out with c++, Tony Gaddis, technical
Fundamentals of Database Systems, Elmarsi & Navathe, technical
The C++ Programming Language, Bjarne Stroustrup, technical
The Pillars of the Earth, Ken Follett, historical fiction
Fall of Giants, Ken Follet, historical fiction
Mindset: The New Psychology of Success, Carol Dweck, psychology
One Hundred Years of Solitude, Gabriel Garcia Marquez, fiction
Ashes, Kenzo Kitakana, fiction
The Dark Forest, Liu Cixin, science fiction
Replay, Ken Grimwood, fantasy
This is my header file code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#ifndef COLLECTION_H
#define COLLECTION_H
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <string>
using namespace std;
class Collection
{
private:
struct Book
{
string title;
string author;
string genre;
};
vector<Book> books;
public:
Collection()
{
Book book;
ifstream inFile;
string filename, text;
inFile.open("Books.txt");
if (inFile.fail())
{
cout << "File not found. Please enter file name: ";
cin >> filename;
inFile.open(filename);
}
while (!inFile.eof())
{
getline(inFile, text);
stringstream lineStream(text);
getline(lineStream, book.title);
getline(lineStream, book.author);
getline(lineStream, book.genre);
books.push_back(book);
}
inFile.close();
}
void sortByTitle()
{
sort(books.begin(), books.end(), [](Book b1, Book b2) { return b1.title < b2.title; });
}
void sortByAuthor()
{
sort(books.begin(), books.end(), [](Book b1, Book b2) { return b1.author < b2.author; });
}
void sortByGenre()
{
sort(books.begin(), books.end(), [](Book b1, Book b2) { return b1.genre < b2.genre; });
}
void output()
{
for (auto &val:books)
{
cout << val.title << endl;
cout << val.author << endl;
cout << val.genre << endl;
}
}
};
#endif
|
This is my main code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include "Collection.h"
#include <iostream>
using namespace std;
int main()
{
Collection collection;
int option;
cout << "There are 3 fields for each book: title, author and genre.\n";
cout << "Please select an option to sort the books by a particular field by entering the correct number.\n";
cout << "1 = Sort by title\n";
cout << "2 = Sort by author\n";
cout << "3 = Sort by genre\n";
cout << "Please enter a sorting option: ";
cin >> option;
if (option == 1)
{
collection.sortByTitle();
}
else if (option == 2)
{
collection.sortByAuthor();
}
else if (option == 3)
{
collection.sortByGenre();
}
collection.output();
return 0;
}
|
This is my output when I run to sort by author:
There are 3 fields for each book: title, author and genre.
Please select an option to sort the books by a particular field by entering the correct number.
1 = Sort by title
2 = Sort by author
3 = Sort by genre
Please enter a sorting option: 2
Replay, Ken Grimwood, fantasy
The Dark Forest, Liu Cixin, science fiction
Ashes, Kenzo Kitakana, fiction
One Hundred Years of Solitude, Gabriel Garcia Marquez, fiction
Mindset: The New Psychology of Success, Carol Dweck, psychology
Starting out with c++, Tony Gaddis, technical
Fall of Giants, Ken Follet, historical fiction
The Pillars of the Earth, Ken Follett, historical fiction
The C++ Programming Language, Bjarne Stroustrup, technical
Fundamentals of Database Systems, Elmarsi & Navathe, technical
|