Back to C++
2026-03-217 min read

Store and Display Information Using Structure (C++)

Learn Store and Display Information Using Structure (C++) step by step with clear examples and exercises.

Why This Matters

Learning to store and display information using structures in C++ is crucial for creating efficient programs that can handle complex data such as student records or inventory lists. By grouping related variables together, we make our code easier to read, write, and maintain. In real-world scenarios like creating a student management system or an inventory tracking application, structures are indispensable.

Prerequisites

Before diving into the core concept, it is essential that you have a good understanding of C++ fundamentals such as variables, data types, control statements (if...else and for loops), and arrays. Familiarity with basic input/output operations will also be helpful in this lesson.

Core Concept

A structure in C++ is a user-defined data type that allows us to group multiple variables of different data types under a single name. To create a structure, we use the struct keyword followed by the structure name and the variables enclosed within curly braces {}.

Here's an example of creating a simple structure for storing student information:

#include <iostream>
using namespace std;

// Define Student structure
struct Student {
string name;
int roll_number;
float marks;
};

int main() {
// Create an array of 10 students
Student students[10];

cout << "Enter student information:\n";

// Take input for each student and store in the structure array
for (int i = 0; i < 10; ++i) {
cout << "Student " << i + 1 << ":\n";
cout << "Name: ";
getline(cin, students[i].name);
cout << "Roll Number: ";
cin >> students[i].roll_number;
cout << "Marks: ";
cin >> students[i].marks;
}

// Display student information
cout << "\nStudent Information:\n";
for (int i = 0; i < 10; ++i) {
cout << "Student " << i + 1 << ":\n";
cout << "Name: " << students[i].name << "\n";
cout << "Roll Number: " << students[i].roll_number << "\n";
cout << "Marks: " << students[i].marks << "\n";
}

return 0;
}

In this example, we first define a structure named Student, which has three members: name, roll_number, and marks. Then, we create an array of students with a size of 10 to store information for ten students. We take input from the user for each student's name, roll number, and marks and store them in the structure array using a for loop. Finally, we display the stored student information on the screen.

Nested Structures

Sometimes, it may be necessary to create structures within other structures. This is known as a nested structure. Here's an example of creating a Student structure that includes a Address nested structure:

#include <iostream>
using namespace std;

// Define Address structure
struct Address {
string street;
string city;
string state;
int pincode;
};

// Define Student structure that includes an Address structure
struct Student {
string name;
int roll_number;
float marks;
Address address; // Nested Address structure
};

int main() {
// Create a student and initialize its address
Student student;
student.address.street = "123 Main St";
student.address.city = "Anytown";
student.address.state = "Anystate";
student.address.pincode = 12345;

// Take input for the student's name, roll number, and marks
cout << "Enter student information:\n";
cout << "Name: ";
getline(cin, student.name);
cout << "Roll Number: ";
cin >> student.roll_number;
cout << "Marks: ";
cin >> student.marks;

// Display student information and their address
cout << "\nStudent Information:\n";
cout << "Name: " << student.name << "\n";
cout << "Roll Number: " << student.roll_number << "\n";
cout << "Marks: " << student.marks << "\n";
cout << "Address:\n";
cout << "Street: " << student.address.street << "\n";
cout << "City: " << student.address.city << "\n";
cout << "State: " << student.address.state << "\n";
cout << "Pincode: " << student.address.pincode << "\n";

return 0;
}

Worked Example

Let's work through an example where we modify the previous code to store and display information for five employees instead of students:

#include <iostream>
using namespace std;

// Define Employee structure
struct Employee {
string name;
int age;
float salary;
};

int main() {
// Create an array of 5 employees
Employee employees[5];

cout << "Enter employee information:\n";

// Take input for each employee and store in the structure array
for (int i = 0; i < 5; ++i) {
cout << "Employee " << i + 1 << ":\n";
cout << "Name: ";
getline(cin, employees[i].name);
cout << "Age: ";
cin >> employees[i].age;
cout << "Salary: ";
cin >> employees[i].salary;
}

// Display employee information
cout << "\nEmployee Information:\n";
for (int i = 0; i < 5; ++i) {
cout << "Employee " << i + 1 << ":\n";
cout << "Name: " << employees[i].name << "\n";
cout << "Age: " << employees[i].age << "\n";
cout << "Salary: " << employees[i].salary << "\n";
}

return 0;
}

Common Mistakes

  1. Forgetting to include the necessary headers: Make sure you include the required header files (e.g., ``) for input/output operations and any other libraries needed for your specific structure.
  1. Incorrect member data types: Ensure that each member of your structure has the appropriate data type. For example, if you want to store an employee's ID, use an integer (int) instead of a string (std::string).
  1. Forgetting semicolons: Always end statements with a semicolon (;).
  1. Incorrect accessing of structure members: To access a member of a structure, you must use the dot operator (.) followed by the structure variable name and the member name. For example, to access the name member of an Employee structure named emp, use emp.name.
  1. Not initializing structure members: If you don't initialize structure members, they will be set to default values (e.g., zeros for integers and null pointers for pointers). To avoid this, always initialize your structure members with appropriate values during declaration or in a constructor.
  1. Inconsistent case when naming structure members: Structure member names are case-sensitive. Make sure that you use consistent casing throughout your code to avoid confusion and potential errors.
  1. Not properly handling memory allocation for dynamically allocated structures: If you allocate memory for a structure dynamically using new, make sure to deallocate it using delete[] when you're done with the structure to prevent memory leaks.

Practice Questions

  1. Create a structure named Car that stores the make, model, year of manufacture, and current speed. Write a program to create an array of 5 cars, take input for each car's details, and display them on the screen.
  1. Modify the employee example provided in this lesson to include an additional member designation in the Employee structure. Take input for each employee's designation and display it along with their name, age, salary, and department (if applicable).
  1. Create a structure named Product that stores the product name, price, quantity in stock, and manufacturer details (name, address, and phone number). Write a program to create an array of 10 products, take input for each product's details, and display them on the screen.
  1. Modify the Product structure from question 3 to include a member discount_percentage. Write a program that calculates the net price (after applying the discount) for each product and displays it along with other product details.

FAQ

  1. Why use structures instead of classes in C++?

Structures are simpler than classes as they don't support member functions or inheritance. However, both can be used to group variables together. Structures are often preferred when dealing with simple data structures where no methods need to be defined.

  1. What happens if I forget the struct keyword while defining a structure?

If you omit the struct keyword while defining a structure, it will be treated as a public class by default. This means that all members of the structure will have public accessibility, which may not always be desirable.

  1. How do I create a constructor for a structure?

To create a constructor for a structure in C++, you need to define a function with the same name as the structure and the explicit keyword. The constructor can then initialize the members of the structure. For example:

struct Employee {
string name;
int age;
float salary;

explicit Employee(string n, int a, float s) : name(n), age(a), salary(s) {}
};

In this example, we've created an Employee constructor that takes three arguments and initializes the structure members with them.

  1. What is the difference between a structure and a class in C++?

Structures and classes are both user-defined data types in C++, but they have some differences:

  • By default, all members of a structure are public, while in a class, private is the default access specifier for members.
  • Structures do not support inheritance or member functions, whereas classes can have both.
  • In a structure, if you omit the struct keyword, it will be treated as a public class by default. If you omit the class keyword while defining a class, it will be treated as a private class by default.
  1. How do I create a union in C++?

A union is a special type of user-defined data type that allows multiple variables to share the same memory location. To create a union, use the union keyword followed by the union name and enclose its members within curly braces {}. For example:

union Data {
int integer;
float floating_point;
};

In this example, we've created a Data union that can store either an int or a float, but not both simultaneously due to the limited memory allocated for it.

Store and Display Information Using Structure (C++) | C++ | XQA Learn