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

In this program, the information of a student (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 variable s. Ask the user to enter the details of the student data like name, roll, and marks. Store these data taken from the user in data members of structure variable s.

cin >> s.name
cin >> s.roll;
cin >> s.marks;

And finally display the data entered by the user on the screen.

 

cout << s.name << endl;
cout << s.roll << endl;
cout << s.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: Open the integer type main function; int main().

Step 6: Create a structure variable s

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

Step 8: Store the entered details in the data members of structure variable s.

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;
};

int main() 
{
    student s;
    cout << "Enter information," << endl;
    cout << "Enter name: ";
    cin >> s.name;
    cout << "Enter roll number: ";
    cin >> s.roll;
    cout << "Enter marks: ";
    cin >> s.marks;

    cout << "\nDisplaying Information," << endl;
    cout << "Name: " << s.name << endl;
    cout << "Roll: " << s.roll << endl;
    cout << "Marks: " << s.marks << endl;
    return 0;
}
                                      

OUTPUT

Enter information,
Enter name: John
Enter roll number: 007
Enter marks: 99%
Displaying Information,
Name: Shyn
Roll: 7
Marks: 99