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

In C++, a structure is a collection of variables with different data types under one name.

We can use a structure to store information about a student such as name, age, and grade.

Here is a simple C++ program that demonstrates how to store information of a student in a structure.

#include <iostream>
#include <string>

using namespace std;

struct Student {
    string name;
    int age;
    char grade;
};

int main() {
    Student s;

    cout << "Enter student's name: ";
    getline(cin, s.name);

    cout << "Enter student's age: ";
    cin >> s.age;

    cout << "Enter student's grade: ";
    cin >> s.grade;

    cout << "Student's name: " << s.name << endl;
    cout << "Student's age: " << s.age << endl;
    cout << "Student's grade: " << s.grade << endl;

    return 0;
}

In this program, we first define a structure called Student which has three members: name of string type, age of integer type, and grade of character type.

Inside the main function, we declare a variable s of type Student.

We then prompt the user to enter the student’s information using the cin function and store it in the respective members of the s variable.

We use the getline function to read the entire name, which can have spaces.

Finally, we display the student’s information using the cout function.

This program can be extended further by adding more members to the Student structure, such as address, phone number, and so on.

In conclusion, C++ structures provide an easy way to store and manipulate related data. By using a structure, we can group related variables under one name and create more organized and maintainable code.