Advertisement
lwytop

Wonyoung Lee MP8 part2.

Jul 24th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. //Wonyoung Lee MP8 part2.
  2. //Book.h file
  3.  
  4. #pragma once
  5. #include <iostream>
  6.  
  7. class Book
  8. {
  9. private:
  10.     std::string authorName;
  11.     std::string title;
  12.     int isbn;
  13. public:
  14.     void Constructor();
  15.     void GetData(std::string, std::string, int);
  16.     void print(int);
  17.     int GetISBN();
  18.  
  19. };
  20.  
  21.  
  22. // Book.cpp file
  23.  
  24. #include "stdafx.h"
  25. #include "Book.h"
  26. #include <iostream>
  27. #include <string>
  28.  
  29. void Book::Constructor() {
  30.     Book::authorName = "No name";
  31.     Book::title = "Unknown title";
  32.     Book::isbn = 0;
  33. }
  34.  
  35. void Book::GetData(std::string authorName, std::string title, int isbn)
  36. {
  37.     Book::authorName = authorName;
  38.     Book::title = title;
  39.     Book::isbn = isbn;
  40. }
  41.  
  42. void Book::print(int count)
  43. {
  44.    
  45.     std::cout << "The information for book " << count << " is: " << std::endl;
  46.     std::cout << authorName << " / " << title << " / "<< isbn << std::endl;
  47. }
  48.  
  49. int Book::GetISBN()
  50. {
  51.     return isbn;
  52. }
  53.  
  54.  
  55. //main.cpp file.
  56.  
  57. // Wonyoung Lee Machine Problem 8 part2.
  58.  
  59. #include "stdafx.h"
  60. #include <iostream>
  61. #include <string>
  62. #include <fstream>
  63. #include "Book.h"
  64.  
  65. using namespace std;
  66.  
  67. int main()
  68. {
  69.  
  70.     string data;                    //use getline to get line and put that line in here.
  71.     string authorName;              //temporarily save name in here
  72.     string title;                   //temporarily save title in here
  73.     int isbn;                       //temporarily save isbn in here
  74.  
  75.  
  76.  
  77.     ifstream inputFile;
  78.     inputFile.open("c:\\temp\\mp8bookarray.txt");
  79.    
  80.     Book books[10];         // save 10 books on array.
  81.  
  82.    
  83.        
  84.     while (!inputFile.eof()) {
  85.         for (size_t count = 0; count < 10; count++)     //put data on arryay.
  86.         {
  87.  
  88.  
  89.             getline(inputFile, data);
  90.             authorName = data;
  91.             getline(inputFile, data);
  92.             title = data;
  93.             getline(inputFile, data);
  94.             isbn = atol(data.c_str());                      //after get string use atol to change int, however atol use char* , so use c_str to change.
  95.  
  96.             books[count].GetData(authorName, title, isbn);  //use GetData function on the Book.cpp to put data.
  97.             books[count].print(count + 1);                  // print it.
  98.         }
  99.     }
  100.    
  101.  
  102.  
  103.  
  104.  
  105.  
  106.     cout << "======================================================" << endl;
  107.  
  108.     int i = 0;      //use this for print if i don't find data.
  109.        
  110.     ifstream inputFile2;                                // use inputFile2 to get ISBN.
  111.     inputFile2.open("c:\\temp\\mp8bookISBN.txt");
  112.  
  113.     while (!inputFile2.eof()) {
  114.  
  115.         getline(inputFile2, data);
  116.         isbn = atol(data.c_str());          //same as above.
  117.  
  118.         cout << "Find ISBN# " << isbn << endl;      //show What user find.
  119.  
  120.         for ( i = 0; i < 10; i++)
  121.         {
  122.             if (isbn == books[i].GetISBN()) {           //if that same print it.
  123.                 cout << "Found your Book!" << endl;
  124.                 books[i].print(i + 1);
  125.                 cout << "============================================" << endl;
  126.                 break;
  127.             }
  128.  
  129.         }
  130.         if (i == 10) {                                  //if end of proses we don't find data print about that.
  131.             cout << "We don't find the book." << endl;
  132.             cout << "============================================" << endl;
  133.         }
  134.     }
  135.  
  136.  
  137.  
  138.  
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement