Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.53 KB | None | 0 0
  1. //--------------------------------------------------------------------------
  2. //  File name: searchbook.cpp
  3. //  Project name: Serendipity
  4. //--------------------------------------------------------------------------
  5. //  Creator's name and email: Felix Murray felix.murray@gmail.com
  6. //  Course Selection: CS1B
  7. //  Creation Date: 8/24/19
  8. //  Date of Last Modification: 9/2/19
  9. //--------------------------------------------------------------------------
  10. //  Purpose: This program allows the user to search the database for a
  11. //           certain book title and view the book information for it (latter
  12. //           occurs when bookinfo function is called).
  13. //          
  14. //--------------------------------------------------------------------------
  15. //  Algorithm:
  16. //      Step 1: Display Menu Options to the console and prompt user to make
  17. //              a selection.
  18. //      Step 2: Take in search title
  19. //      Step 3: Either retun titles that were found and allow the user to
  20. //              view desired book info or prompt that no books were found.
  21. //--------------------------------------------------------------------------
  22. #include <iostream>
  23. #include <string>
  24. #include "bookinfo.hpp"
  25. #include "searchbook.hpp"
  26.  
  27. using namespace std;
  28.  
  29. extern bookType books[20];
  30.  
  31. void lookUpBook(int &bookNums) {
  32.     //--------------------------------------------------------------------------
  33.     // DATA DICTIONARY
  34.     //--------------------------------------------------------------------------
  35.     // VARIABLES
  36.     //
  37.     //   NAME              DATA TYPE         VALUE
  38.     //--------------------------------------------------------------------------
  39.     //  selectRecord        char                null
  40.     //  exit                bool               false
  41.     //  found               bool               false
  42.     //  loopEnd             bool               false
  43.     //  recoredViewed       bool               false
  44.     //  reply               char                null
  45.     //  temp               string               null
  46.     //  searchTitle        string               null            
  47.     //--------------------------------------------------------------------------
  48.  
  49.     char reply;
  50.     char selectRecord;
  51.     bool recordViewed = false;
  52.     bool found = false;
  53.     int index = 0;
  54.     string temp;
  55.     string searchTitle;
  56.    
  57.  
  58.     system("clear");
  59.  
  60.     cout << "\t\t\t\t\t  Serendipity Book Sellers\n";
  61.     cout << "\t\t\t\t\t         Book Search\n\n";
  62.  
  63.     cin.ignore();
  64.  
  65.     if (bookNums > 0) {
  66.         cout << "\t\t\t   Enter the title or ISBN of the book to search for:\n";
  67.         cout << "\t\t\t\t\t\t      ";
  68.  
  69.         getline(cin, searchTitle);
  70.  
  71.         for (int i = 0; i < searchTitle.length(); i++) {
  72.             searchTitle[i] = tolower(searchTitle[i]);
  73.         }
  74.  
  75.         for (int index = 0; index < bookNums; index++) {
  76.             temp = "";
  77.             for (int indexInner = 0; indexInner < books[index].bookTitle.length(); indexInner++) {
  78.                 temp.append(1, tolower(books[index].bookTitle.at(indexInner)));
  79.             }
  80.              //uncomment below if temp value seems to be causing issues
  81.              //cout << temp << " " << "\n" << index << " ";
  82.             if (temp.find(searchTitle) != string::npos){
  83.                 found = true;
  84.  
  85.                 while (true) {         
  86.                     system("clear");
  87.                     cout << "\t\t\t\t\t  Serendipity Book Sellers\n";
  88.                     cout << "\t\t\t\t\t         Book Search\n\n";
  89.                     cout << "\t\t\t\t\t      RESULT>: " << books[index].bookTitle <<  "\n\n";
  90.                     cout << "\t\t\t\t\tView this book record? (Y/N): ";
  91.                     cin >> selectRecord;
  92.                     cin.ignore(600, '\n');
  93.        
  94.                     if (selectRecord == 'Y' || selectRecord == 'y') {
  95.                         bookInfo(index);
  96.                         recordViewed = true;
  97.                         break;
  98.                     } else if (selectRecord == 'N' || selectRecord == 'n') {
  99.                         break; 
  100.                     } else {
  101.                         system("clear");
  102.                         cout << "\t\t\t\t\t  Serendipity Book Sellers\n";
  103.                         cout << "\t\t\t\t\t         Book Search\n\n";
  104.                         cout << "\t\t\t\t       ERROR: Input must be 'Y' or 'N'\n\n";
  105.                         cout << "\t\t\t\t        Press any key to continue...";
  106.                         cin >> reply;      
  107.                         if (reply != '\0'){
  108.                             continue;
  109.                         }
  110.                     }
  111.                 }
  112.                 if (recordViewed) {
  113.                     break;
  114.                 }  
  115.             }
  116.             if (index > bookNums - 1) {
  117.                 found = false;
  118.             }
  119.         }
  120.  
  121.         if (!found) {
  122.             system("clear");
  123.             cout << "\t\t\t\t\t  Serendipity Book Sellers\n";
  124.             cout << "\t\t\t\t\t         Book Search\n\n";
  125.             cout << "\t\t\t Book not found. Please search for a book within the inventory.\n";
  126.             cout << "\t\t\t\t        Press any key to continue...";
  127.             cin >> reply;
  128.         }
  129.         //temp = "";
  130.     } else {
  131.         cout << "\t\t\t\t    No books currently in the database!\n\n";
  132.         cout << "\t\t\t\t        Press any key to continue...";
  133.         cin >> reply;
  134.     }
  135.    
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement