Book Struct
Define a Book struct that includes the member variables: title, author, and page_count. Create an instance of the Book struct and assign its member variables with user input.
Hint
Steps
Define a
Bookstruct withtitle,author, andpage_countmember variables.Instanciate a
Bookobject.Use
std::getlineto properly read strings with spaces fortitleandauthor.Use
std::cinfor reading the integerpage_count.Print the values of the
Bookinstance in a readable format.Defined a
Bookstruct withtitle,author, andpage_countmember variables.Used
std::getlineto properly read strings with spaces fortitleandauthor.Used
std::cinfor reading the integerpage_count.Printed the values of the
Bookinstance in a readable format.
Answer
#include <iostream>
#include <string>
struct Book {
std::string title;
std::string author;
int page_count;
};
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;
std::cout << "Book Information:" << std::endl;
std::cout << "Title: " << myBook.title << std::endl;
std::cout << "Author: " << myBook.author << std::endl;
std::cout << "Page Count: " << myBook.page_count << std::endl;
return 0;
}