Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* TonyPLab8.cpp
- Created by: Tony Pham
- Date: 12.01.2019
- */
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <string>
- using namespace std;
- // structure
- struct Book {
- string title;
- string author;
- };
- const int ARRAY_SIZE = 1000;
- Book books[ARRAY_SIZE];
- string pathName;
- bool userInput;
- // global variables
- int loadData();
- void showAll(int count);
- void showBooksByAuthor(int count, string author);
- void showBooksByTitle(int count, string title);
- void sortByTitle(int count, string title);
- void sortByAuthor(int count, string author);
- int main()
- {
- // initialised variables
- int count = 0;
- char selector = 'q';
- string name;
- string title;
- // asks user for file pathname
- cout << "Welcome to Tony's Library Database.\n";
- cout << "Please enter the name of the file: ";
- cin >> pathName;
- count = loadData();
- // catches if count value is less than 0
- while (count < 0) {
- cout << " Do you want to try again(1) or quit(0)? \n";
- cin >> userInput;
- if (userInput == 1) {
- cout << "Please enter the name of the file: ";
- cin >> pathName;
- count = loadData();
- }
- if (userInput == 0)
- {
- cout << " Program will now close! \n";
- return 0;
- }
- }
- cout << count << " Records loaded successfully.\n";
- // Switch case menu
- do {
- cout << endl << "Please enter a keyword that corresponds to the list of options: \n";
- cout << " Search by: (A)uthor, (T)itle, (S)how All, (Q)uit Program: ";
- cin >> selector;
- selector = toupper(selector);
- switch (selector)
- {
- case 'S':
- sortByTitle(count, title);
- if (count <= 0) {
- cout << " No counts found! \n";
- }
- else {
- showAll(count);
- }
- break;
- case 'A':
- sortByAuthor(count, name);
- cout << " Book Author: ";
- cin.ignore();
- getline(cin, name);
- if (count <= 0) {
- cout << " No records found! \n";
- }
- else {
- showBooksByAuthor(count, name);
- break;
- }
- case 'T':
- sortByTitle(count, title);
- cout << " Book Title: ";
- cin.ignore();
- getline(cin, title);
- if (count <= 0) {
- cout << " No records found! \n";
- }
- else {
- showBooksByTitle(count, title);
- break;
- }
- }
- }
- while (selector != 'q' && selector != 'Q');
- return 0;
- }
- int loadData()
- {
- int count = 0;
- ifstream fileLocation;
- fileLocation.open(pathName);
- if (!fileLocation.is_open())
- {
- cout << " Unable to open file path! \n";
- return -1;
- }
- while (!fileLocation.eof()) // Echo starts here
- {
- getline(fileLocation, books[count].title);
- getline(fileLocation, books[count].author);
- count++;
- }
- return count;
- }
- // displays all book titles beside the author names
- void showAll(int count)
- {
- for (int i = 0; i < count; i++)
- {
- cout << books[i].title << " " << "(" << books[i].author << ")\n";
- }
- }
- // displays books based on author name matching user input.
- void showBooksByAuthor(int count, string name)
- {
- int j = 0;
- for (int i = 0; i < count; i++)
- {
- if (books[i].author.find(name) < 100)
- {
- cout << books[i].title << " " << "(" << books[i].author << ")\n";
- j++;
- }
- }
- cout << j << " records found! \n";
- }
- // Displays books based on title matching user input.
- void showBooksByTitle(int count, string title)
- {
- int j = 0;
- for (int i = 0; i < count; i++)
- {
- if (books[i].title.find(title) < 100)
- {
- cout << books[i].title << " " << "(" << books[i].author << ")\n";
- j++;
- }
- }
- cout << j << " records found! \n";
- }
- // Sorts by book title.
- void sortByTitle(int count, string title) {
- Book temp;
- for (int i = 1; i < count; i++) {
- for (int j = 0; j < count - i; j++) {
- if (books[j].title > books[j + 1].title) {
- temp = books[j];
- books[j] = books[j + 1];
- books[j + 1] = temp;
- }
- }
- }
- }
- // Sorts by book author.
- void sortByAuthor(int count, string name) {
- Book temp;
- for (int i = 1; i < count; i++) {
- for (int j = 0; j < count - i; j++) {
- if (books[j].author > books[j + 1].author) {
- temp = books[j];
- books[j] = books[j + 1];
- books[j + 1] = temp;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement