phamt

Library

Dec 4th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.35 KB | None | 0 0
  1.  
  2.  
  3. */
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <fstream>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. // structure
  12. struct Book {
  13.     string title;
  14.     string author;
  15. };
  16.  
  17. const int ARRAY_SIZE = 1000;
  18. Book books[ARRAY_SIZE];
  19. string pathName;
  20. ifstream lib;
  21.  
  22. // global variables
  23. int loadData();
  24. void showAll(int count);
  25. void showBooksByAuthor(int count, string author);
  26. void showBooksByTitle(int count, string title);
  27. void sortByTitle(int count, string title);
  28. void sortByAuthor(int count, string author);
  29.  
  30. int main()
  31. {
  32.     // initialised variables
  33.     int count = 0;
  34.     char selector = 'q', yesNoAnswer = 'n';
  35.     string name;
  36.     string title;
  37.  
  38.     // asks user for file pathname
  39.     cout << "Welcome to Tony's Library Database." << endl;
  40.     cout << "Please enter the name of the file: ";
  41.     getline(cin, pathName);
  42.     //loadData();
  43.     count = loadData();
  44.     cout << count << " Records loaded successfully." << endl;
  45.     // Switch case menu
  46.     do {
  47.         cout << endl << "Please enter a keyword that corresponds to the list of options: " << endl;
  48.         cout << " Search by: (A)uthor, (T)itle, (S)how All, (Q)uit Program: ";
  49.         cin >> selector;
  50.         selector = toupper(selector);
  51.         switch (selector)
  52.         {
  53.         case 'S':
  54.             sortByTitle(count, title);
  55.             if (count <= 0) {
  56.                 cout << " No counts found! " << endl;
  57.             }
  58.             else {
  59.                 showAll(count);
  60.             }
  61.             break;
  62.         case 'A':
  63.             sortByAuthor(count, name);
  64.             cout << " Book Author: ";
  65.             cin.ignore();
  66.             getline(cin, name);
  67.             if (count <= 0) {
  68.                 cout << " No records found! " << endl;
  69.             }
  70.             else {
  71.                 showBooksByAuthor(count, name);
  72.                 break;
  73.             }
  74.         case 'T':
  75.             sortByTitle(count, title);
  76.             cout << " Book Title: ";
  77.             cin.ignore();
  78.             getline(cin, title);
  79.             if (count <= 0) {
  80.                 cout << " No records found! " << endl;
  81.             }
  82.             else {
  83.                 showBooksByTitle(count, title);
  84.                 break;
  85.             }
  86.         }
  87.     }
  88.     // the condition that will break the do loop and exit
  89.     while (selector != 'q' && selector != 'Q');
  90.     return 0;
  91. }
  92.  
  93. int loadData()
  94. {
  95.     int count = 0;
  96.     int i = 0;
  97.  
  98.     lib.open(pathName);
  99.     ifstream lib(pathName);
  100.  
  101.     if (!lib)
  102.     {
  103.         cout << " Unable to open file path! " << endl;
  104.         return -1;
  105.     }
  106.     while (!lib.eof())
  107.     {
  108.         getline(lib, books[count].title);
  109.         getline(lib, books[count].author);
  110.         count++;
  111.     }
  112.     return count;
  113. }
  114. // displays all book titles beside the author names
  115. void showAll(int count)
  116. {
  117.     for (int i = 0; i < count; i++)
  118.     {
  119.         cout << books[i].title << " " << "(" << books[i].author << ")" << endl;
  120.     }
  121. }
  122. // displays books based on author name matching user input.
  123. void showBooksByAuthor(int count, string name)
  124. {
  125.     int j = 0;
  126.     for (int i = 0; i < count; i++)
  127.     {
  128.         if (books[i].author.find(name) < 100)
  129.         {
  130.             cout << books[i].title << " " << "(" << books[i].author << ")" << endl;
  131.             j++;
  132.         }
  133.     }
  134.     cout << j << " records found! " << endl;
  135. }
  136. // Displays books based on title matching user input.
  137. void showBooksByTitle(int count, string title)
  138. {
  139.     int j = 0;
  140.     for (int i = 0; i < count; i++)
  141.     {
  142.         if (books[i].title.find(title) < 100)
  143.         {
  144.             cout << books[i].title << " " << "(" << books[i].author << ")" << endl;
  145.             j++;
  146.         }
  147.     }
  148.     cout << j << " records found! " << endl;
  149. }
  150. // Sorts by book title.
  151. void sortByTitle(int count, string title) {
  152.     Book temp;
  153.     for (int i = 1; i < count; i++) {
  154.         for (int j = 0; j < count - i; j++) {
  155.             if (books[j].title > books[j + 1].title) {
  156.                 temp = books[j];
  157.                 books[j] = books[j + 1];
  158.                 books[j + 1] = temp;
  159.             }
  160.         }
  161.     }
  162. }
  163. // Sorts by book author.
  164. void sortByAuthor(int count, string name) {
  165.     Book temp;
  166.     for (int i = 0; i < count; i++) {
  167.         for (int j = 0; j < count - i; j++) {
  168.             if (books[j].author > books[j + 1].author) {
  169.                 temp = books[j];
  170.                 books[j] = books[j + 1];
  171.                 books[j + 1] = temp;
  172.             }
  173.         }
  174.     }
  175. }
  176.  
  177.  
  178. // text file books.txt
  179. Objects First with Java
  180. Barnes and Kolling
  181. Game Development Essentials
  182. Novak
  183. The Game Maker's Apprentice
  184. Overmars
  185. C++ Programming: From Problem Analysis...
  186. Malik
  187. C++ Programming Lab Manual
  188. Scholl
  189. Beginning LINUX Programming
  190. Stones and Matthew
  191. C++ Programming: Program Design Including...
  192. D. S. Malik
  193. C++ How to Program
  194. Deitel and Deitel
  195. Programming and Problem Solving with C++
  196. Dale, Weems, Headington
  197. Game Character Development with Maya
  198. Ward
  199. Developing Games in Java
  200. Brackeen
  201. C# Programming
  202. Harvey, Robinson, Templeman, Watson
  203. Java Programming
  204. Farrell
  205. Audio for Games
  206. Brandon
Add Comment
Please, Sign In to add comment