Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- */
- #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;
- ifstream lib;
- // 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', yesNoAnswer = 'n';
- string name;
- string title;
- // asks user for file pathname
- cout << "Welcome to Tony's Library Database." << endl;
- cout << "Please enter the name of the file: ";
- getline(cin, pathName);
- //loadData();
- count = loadData();
- cout << count << " Records loaded successfully." << endl;
- // Switch case menu
- do {
- cout << endl << "Please enter a keyword that corresponds to the list of options: " << endl;
- 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! " << endl;
- }
- else {
- showAll(count);
- }
- break;
- case 'A':
- sortByAuthor(count, name);
- cout << " Book Author: ";
- cin.ignore();
- getline(cin, name);
- if (count <= 0) {
- cout << " No records found! " << endl;
- }
- 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! " << endl;
- }
- else {
- showBooksByTitle(count, title);
- break;
- }
- }
- }
- // the condition that will break the do loop and exit
- while (selector != 'q' && selector != 'Q');
- return 0;
- }
- int loadData()
- {
- int count = 0;
- int i = 0;
- lib.open(pathName);
- ifstream lib(pathName);
- if (!lib)
- {
- cout << " Unable to open file path! " << endl;
- return -1;
- }
- while (!lib.eof())
- {
- getline(lib, books[count].title);
- getline(lib, 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 << ")" << endl;
- }
- }
- // 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 << ")" << endl;
- j++;
- }
- }
- cout << j << " records found! " << endl;
- }
- // 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 << ")" << endl;
- j++;
- }
- }
- cout << j << " records found! " << endl;
- }
- // 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 = 0; 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;
- }
- }
- }
- }
- // text file books.txt
- Objects First with Java
- Barnes and Kolling
- Game Development Essentials
- Novak
- The Game Maker's Apprentice
- Overmars
- C++ Programming: From Problem Analysis...
- Malik
- C++ Programming Lab Manual
- Scholl
- Beginning LINUX Programming
- Stones and Matthew
- C++ Programming: Program Design Including...
- D. S. Malik
- C++ How to Program
- Deitel and Deitel
- Programming and Problem Solving with C++
- Dale, Weems, Headington
- Game Character Development with Maya
- Ward
- Developing Games in Java
- Brackeen
- C# Programming
- Harvey, Robinson, Templeman, Watson
- Java Programming
- Farrell
- Audio for Games
- Brandon
Add Comment
Please, Sign In to add comment