Search⌘ K

Structs for Categorization

Learn what structs are in C++ and how to use them for grouping related pieces of data together.

The project

You’ve been tracking calories by day and meal type, but now you want to see the whole week’s nutrition in one system. Instead of running your tracker day by day, you need a planner that stores all 7 days of meals, calculates per-meal totals, and shows both daily summaries and a weekly summary.

Type the following:

Day 1 Breakfast: 400
Day 1 Lunch: 600
Day 1 Dinner: 300

It is useful, but it doesn’t provide a comprehensive weekly overview. What you need is a structured way to group meal data—a format where each day can hold details for breakfast, lunch, and dinner together, and where you can easily loop through the week to compute totals.

That’s where structs come in.

The programming concept

The concept for today is structs, one of the most powerful building blocks in C++. A struct allows you to group related pieces of data ...