Advertisement
Guest User

OOP 1 Exercise 3

a guest
Feb 23rd, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int NUMBER_OF_BOOKS = 3;
  5.  
  6. struct Book {
  7. char title[30];
  8. char author[30];
  9. char isbn[10];
  10. };
  11.  
  12. struct Library {
  13. Book books[1000];
  14. };
  15.  
  16. Book readBook()
  17. {
  18. Book newBook;
  19. cout << "Give me a book title:" << endl;
  20. cin >> newBook.title;
  21. cout << "Give me a book author:" << endl;
  22. cin >> newBook.author;
  23. cout << "Give me a book isbn:" << endl;
  24. cin >> newBook.isbn;
  25. return newBook;
  26. }
  27.  
  28. Library* readAndAddBook(Library* library,int indexOfBook)
  29. {
  30. Book newBook = readBook();
  31. library->books[indexOfBook] = newBook;
  32. return library;
  33. }
  34. void printBooksInLibrary(Library* library,int size)
  35. {
  36. for (int i = 0; i < size; i++)
  37. {
  38. cout << library->books[i].title << endl;
  39. cout << library->books[i].author << endl;
  40. cout << library->books[i].isbn << endl;
  41. }
  42. }
  43.  
  44.  
  45. int main()
  46. {
  47. Library* myLibrary = new Library;
  48. for (int i = 0; i < NUMBER_OF_BOOKS; i++)
  49. {
  50. myLibrary = readAndAddBook(myLibrary, i);
  51. }
  52. printBooksInLibrary(myLibrary, NUMBER_OF_BOOKS);
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement