Printing Struct Elements with cout
An uses the insertion operator << to print supported values and can chain them together. A custom struct needs its own insertion operator to print as a whole. You can print its individual members directly.
Task: Use the provided book to print The Night Garden: 284 pages.
struct.cpp · print-members · you write the boxed part
1#include <iostream>2#include <string>3 4int main() {5 struct Book {6 std::string title;7 int pages;8 };9 Book book;10 book.title = "The Night Garden";11 book.pages = 284;12 13 14 return 0;15}$g++ -std=c++17 struct.cpp -o main
press RUN to compile
Hint
Use the struct's members directly.
0 of 1 done