Display Book

Display book method

Define a Book struct that includes member variables: title, author, page_count, and a member function display_info that prints the title, author, and page count of the book. Demonstrate this function on one Book object.

Answer

#include <iostream>
#include <string>

struct Book {
    std::string title;
    std::string author;
    int page_count;

    void display_info() {
        std::cout << "Title: " << title << std::endl;
        std::cout << "Author: " << author << std::endl;
        std::cout << "Page Count: " << page_count << std::endl;
    }
};

int main() {
    Book myBook;

    std::cout << "Enter the title of the book: ";
    std::getline(std::cin, myBook.title);

    std::cout << "Enter the author of the book: ";
    std::getline(std::cin, myBook.author);

    std::cout << "Enter the page count of the book: ";
    std::cin >> myBook.page_count;

    myBook.display_info();

    return 0;
}
Questions