0
0
C++programming~15 mins

Defining structures in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
Defining structures
📖 Scenario: You are creating a simple program to store information about a book in a library system.
🎯 Goal: Build a program that defines a Book structure, sets up a book's details, and prints them.
📋 What You'll Learn
Define a structure named Book with three members: title (string), author (string), and year (integer).
Create a variable of type Book named myBook.
Assign the title "The Great Gatsby", author "F. Scott Fitzgerald", and year 1925 to myBook.
Print the book details in the format: Title: The Great Gatsby, Author: F. Scott Fitzgerald, Year: 1925.
💡 Why This Matters
🌍 Real World
Structures are used to group related information, like details about a book, a person, or a product, making data easier to manage.
💼 Career
Understanding structures is important for jobs involving C++ programming, embedded systems, game development, and any software that needs organized data.
Progress0 / 4 steps
1
Define the Book structure
Define a structure called Book with three members: title and author of type std::string, and year of type int.
C++
Need a hint?

Use the struct keyword followed by the structure name Book. Inside curly braces, declare the members with their types.

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?

Declare myBook as Book myBook;. Use dot notation to assign values to each member.

3
Print the book details
Use std::cout to print the book details in this exact format: Title: The Great Gatsby, Author: F. Scott Fitzgerald, Year: 1925.
C++
Need a hint?

Use std::cout with the insertion operator << to print each part with commas and spaces exactly as shown.

4
Run and display the output
Run the program and print the output showing the book details exactly as: Title: The Great Gatsby, Author: F. Scott Fitzgerald, Year: 1925.
C++
Need a hint?

Make sure the output matches exactly, including commas and spaces.