C++ Program to Store Information of 5 Students in a Structure

In this program, the information of 5 students (name, roll, and marks entered by the user ) is stored in a structure and displayed on the screen. 

What is a structure in C++?

The structure is a collection of variables of different data types under a single name. It is similar to a class in that, both hold a collection of data of different data types.

How to declare a structure in C++ programming?

The struct keyword defines a structure type followed by an identifier (name of the structure).
Then inside the curly braces, you can declare one or more members (declare variables inside curly braces) of that structure. For example:
struct Person
{
    char name[50];
    int age;
    float salary;
};

Here a structure person is defined which has three members: name, age, and salary

How to define a structure variable?

Once you declare a structure person as above. You can define a structure variable as:
Person bill;

How do access members of a structure?

The members of the structure variable are accessed using a dot (.) operator.
Suppose, you want to access the age of the structure variable bill and assign 50 to it. You can perform this task by using the following code below:
bill.age = 50;

C++ Program to Store Information of a Student in a Structure

Here we are creating a structure called student and name, roll and marks as its data members.
struct student
{
    char name[50];
    int roll;
    float marks;
};

 Then create a structure array of size 5. Ask the user to enter the details of 5 students' data like name, roll, and marks. Use a for loop to take the information of 5 students from the user screen.
The for loop will be like
for(int i = 0; i < 10; ++i)
{
s[i].roll = i+1;
cin >> s[i].name;
cin >> s[i].marks
}

And finally display the data entered by the user on the screen.
for(int i = 0; i < 10; ++i)
    {
        cout << i+1 << endl;
        cout << s[i].name << endl;
        cout << s[i].marks << endl;
    }

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3:  Create a structure student

Step 4: Create data members name, roll, and marks for the structure student.

Step 5: create a structure array of size 5. s[5]

Step 6: Open the integer type main function; int main().

Step 7: Ask the user to enter the details of the student.

Step 8: Store the entered details in the array by using a for loop

Step 9: Display the details on the screen

Step 10: Exit
 

C++ Source Code

                                          #include <iostream>
using namespace std;

struct student
{
    char name[50];
    int roll;
    float marks;
} s[5];

int main()
{
    cout << "Enter information of students: " << endl;

    // storing information
    for(int i = 0; i < 5; ++i)
    {
        s[i].roll = i+1;
        cout << "For roll number" << s[i].roll << "," << endl;

        cout << "Enter name: ";
        cin >> s[i].name;

        cout << "Enter marks: ";
        cin >> s[i].marks;

        cout << endl;
    }

    cout << "Displaying Information: " << endl;

    // Displaying information
    for(int i = 0; i < 5; ++i)
    {
        cout << "\nRoll number: " << i+1 << endl;
        cout << "Name: " << s[i].name << endl;
        cout << "Marks: " << s[i].marks << endl;
    }

    return 0;
}
                                      

OUTPUT

Enter information of students: 
For roll number1,
Enter name: a1
Enter marks: 55
For roll number2,
Enter name: a2
Enter marks: 65
For roll number3,
Enter name: a3
Enter marks: 95
For roll number4,
Enter name: a4
Enter marks: 85
For roll number5,
Enter name: a5
Enter marks: 25
Displaying Information: 

Roll number: 1
Name: a1
Marks: 55

Roll number: 2
Name: a2
Marks: 65

Roll number: 3
Name: a3
Marks: 95

Roll number: 4
Name: a4
Marks: 85

Roll number: 5
Name: a5
Marks: 25