0
0
C++programming~15 mins

Accessing structure members in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
Accessing structure members
📖 Scenario: You are creating a simple program to store and display information about a book in a library.
🎯 Goal: Build a program that defines a structure for a book, stores information in it, and prints the book's details by accessing its members.
📋 What You'll Learn
Define a structure named Book with members title, author, and year.
Create a variable of type Book and assign values to its members.
Access and print the members of the Book variable.
💡 Why This Matters
🌍 Real World
Structures help organize related data together, like storing details about books, students, or products in real applications.
💼 Career
Understanding how to define and access structures is fundamental for many programming jobs, especially in systems programming, game development, and software engineering.
Progress0 / 4 steps
1
Define the Book structure
Define a structure called Book with three members: title and author as std::string, and year as int.
C++
Need a hint?

Use the struct keyword to define Book with the specified members.

2
Create a Book variable and assign values
Inside main(), create a variable named myBook of type Book. Assign "The Great Gatsby" to myBook.title, "F. Scott Fitzgerald" to myBook.author, and 1925 to myBook.year.
C++
Need a hint?

Create a variable of type Book and use the dot . operator to assign values to its members.

3
Access and print the Book members
Use std::cout to print the book details in this exact format:
Title: The Great Gatsby
Author: F. Scott Fitzgerald
Year: 1925
Access the members of myBook using the dot operator.
C++
Need a hint?

Use std::cout and the dot operator to print each member on its own line.

4
Run the program to display the book details
Run the program and ensure it prints exactly:
Title: The Great Gatsby
Author: F. Scott Fitzgerald
Year: 1925
C++
Need a hint?

Make sure your output matches exactly, including capitalization and spacing.