Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Wonyoung Lee MP8 part2.
- //Book.h file
- #pragma once
- #include <iostream>
- class Book
- {
- private:
- std::string authorName;
- std::string title;
- int isbn;
- public:
- void Constructor();
- void GetData(std::string, std::string, int);
- void print(int);
- int GetISBN();
- };
- // Book.cpp file
- #include "stdafx.h"
- #include "Book.h"
- #include <iostream>
- #include <string>
- void Book::Constructor() {
- Book::authorName = "No name";
- Book::title = "Unknown title";
- Book::isbn = 0;
- }
- void Book::GetData(std::string authorName, std::string title, int isbn)
- {
- Book::authorName = authorName;
- Book::title = title;
- Book::isbn = isbn;
- }
- void Book::print(int count)
- {
- std::cout << "The information for book " << count << " is: " << std::endl;
- std::cout << authorName << " / " << title << " / "<< isbn << std::endl;
- }
- int Book::GetISBN()
- {
- return isbn;
- }
- //main.cpp file.
- // Wonyoung Lee Machine Problem 8 part2.
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <fstream>
- #include "Book.h"
- using namespace std;
- int main()
- {
- string data; //use getline to get line and put that line in here.
- string authorName; //temporarily save name in here
- string title; //temporarily save title in here
- int isbn; //temporarily save isbn in here
- ifstream inputFile;
- inputFile.open("c:\\temp\\mp8bookarray.txt");
- Book books[10]; // save 10 books on array.
- while (!inputFile.eof()) {
- for (size_t count = 0; count < 10; count++) //put data on arryay.
- {
- getline(inputFile, data);
- authorName = data;
- getline(inputFile, data);
- title = data;
- getline(inputFile, data);
- isbn = atol(data.c_str()); //after get string use atol to change int, however atol use char* , so use c_str to change.
- books[count].GetData(authorName, title, isbn); //use GetData function on the Book.cpp to put data.
- books[count].print(count + 1); // print it.
- }
- }
- cout << "======================================================" << endl;
- int i = 0; //use this for print if i don't find data.
- ifstream inputFile2; // use inputFile2 to get ISBN.
- inputFile2.open("c:\\temp\\mp8bookISBN.txt");
- while (!inputFile2.eof()) {
- getline(inputFile2, data);
- isbn = atol(data.c_str()); //same as above.
- cout << "Find ISBN# " << isbn << endl; //show What user find.
- for ( i = 0; i < 10; i++)
- {
- if (isbn == books[i].GetISBN()) { //if that same print it.
- cout << "Found your Book!" << endl;
- books[i].print(i + 1);
- cout << "============================================" << endl;
- break;
- }
- }
- if (i == 10) { //if end of proses we don't find data print about that.
- cout << "We don't find the book." << endl;
- cout << "============================================" << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement