C++ Program to Store and Display Information Using Structure

Updated on December 13, 2024
Store and display information using structure header image

Introduction

In C++, structures are used to group different types of data into a single composite entity. This feature is particularly useful when it comes to storing and managing related data items, such as records or complex data attributes. By using structures, you can create a more organized and modular approach to handling your data in C++ programming.

In this article, you will learn how to define a structure to store information and how to display that information. You'll explore examples that demonstrate the practical use of structures in storing information like student records and employee details, as well as displaying this information correctly.

Understanding Structures in C++

What is a Structure?

  1. Recognize that a structure in C++ can contain multiple data types.
  2. Realize that structures help in organizing data efficiently, allowing access through a single entity rather than handling each data item separately.

Declaring a Structure

  1. Define a structure using the struct keyword followed by the structure name and a block containing the data members.

  2. Declare variables of the structure type just like any other data type.

    cpp
    struct Person {
        std::string name;
        int age;
        float height;
    };
    

    This code defines a Person structure with a name, age, and height, initializing the way to store personal information effectively.

Storing Information Using Structures

Define a Structure for Student Data

  1. Create a structure to store student information including ID, name, and grade.

  2. Initialize structures to add specific student details.

    cpp
    struct Student {
        int id;
        std::string name;
        float grade;
    };
    

Initialize Student Data

  1. Define a variable of type Student and store information in it.

    cpp
    Student student1 = {101, "John Doe", 88.5};
    

    Here, student1 is initialized with an ID of 101, name "John Doe", and a grade of 88.5.

Displaying Information from Structures

Display Function for Student Structure

  1. Write a function to display information about the student.

  2. Use this function to print the details of a student.

    cpp
    void displayStudent(Student s) {
        std::cout << "Student ID: " << s.id << std::endl;
        std::cout << "Name: " << s.name << std::endl;
        std::cout << "Grade: " << s.grade << std::endl;
    }
    

    This function takes a Student structure as an argument and prints out the student's ID, name, and grade.

Using the Display Function

  1. Call the display function to show the details of student1.

    cpp
    displayStudent(student1);
    

    When this function is called with student1, it outputs the student's information stored in the structure.

Struct Arrays for Multiple Records

  1. Understand how to manage multiple data records using an array of structures.

  2. Initialize an array of Student type to store multiple records easily.

    cpp
    Student classRoom[2] = {
        {102, "Alice Johnson", 91.2},
        {103, "Mark Smith", 84.6}
    };
    

    This array, classRoom, stores information for two students, simplifying data management via structures.

  3. Use a loop to display all student records in the array.

    cpp
    for (int i = 0; i < 2; ++i) {
        displayStudent(classRoom[i]);
    }
    

    This loop iterates through each element of the classRoom array and uses the displayStudent function to print each student's details.

Conclusion

Structures in C++ offer a robust method for managing grouped data. By understanding how to define, initialize, and manipulate structures, you enhance your ability to manage and display complex data efficiently. In scenarios requiring the organization of related data items, such as managing student or employee records, structures prove to be invaluable. Utilize structures in your C++ projects to keep your code organized and maintainable, making data handling tasks straightforward and efficient.