Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
  • Last Updated : 14 Nov, 2022
Improve Article
Save Article

Arrays are used to store sets of data of similar data types at contiguous memory locations. Unlike Arrays, Structures are user-defined data types that are used to store groups of items of non-similar data types. Here, we are going to compile a C++ program that will store the information of the students in a Structure.

Following is the information we will store in the structure:

  • Student Name (String)
  • Student Roll Number (String)
  • Subjects Enrolled (Array of Strings)
  • Marks in each subject (Array of Int)
  • CGPA(Float)

Examples:

Yash Gupta
S20200010234
[DSA, OOPS ,DBMS, CCN]
[89,78,86,90]
8.918

For a Student

C++




// C++ program to demonstrate
// a structure for student details
#include <bits/stdc++.h>
using namespace std;
 
// Structure Of students
struct student {
    string name; // Student Name
    string rollno; // Student Roll Number
    vector<string> subjects; // Subjects Enrolled(Array)
    vector<int> marks; // Marks in each subject(Array)
    float cgpa; // Student's CGPA
};
 
// Function to print a vector(for more "Different ways to
// print elements of vector" at GFG)
template <typename S> void printv(const vector<S>& v)
{
    cout << "[ ";
    // Iterating over all elements of vector
    for (auto elem : v) {
        cout << elem << " ";
    }
    cout << "]";
    cout << endl;
}
 
// Function to print a Student
void printStudent(student* s)
{
    cout << "Student Details:" << endl;
    cout << endl;
    cout << "Name: " << s->name << endl;
    cout << "Roll Number: " << s->rollno << endl;
    cout << "Subjects: ";
    printv(s->subjects);
    cout << "Marks: ";
    printv(s->marks);
    cout << "CGPA " << s->cgpa << endl;
}
 
// Driver Code
int main()
{
    // New Student
    student s;
 
    // Declaring all the information of a student
    s.name = "GeeksforGeeks";
    s.rollno = "S20200010234";
    s.subjects = { "DSA", "OOPS", "DBMS", "CCN" };
    s.marks = { 89, 78, 86, 90 };
    s.cgpa = 8.918;
 
    // Function call to print a Student
    printStudent(&s);
 
    return 0;
}


Output

Student Details:

Name: GeeksforGeeks
Roll Number: S20200010234
Subjects: [ DSA OOPS DBMS CCN ]
Marks: [ 89 78 86 90 ]
CGPA 8.918

For a Student (Array)

These two details will also be added with the old detail to showcase the use of structure with multiple parameters.

GFG
S20200010164
[DSA, OOPS ,DBMS, CCN]
[89,80,89,80]
8.45
gfg
Roll Number: S20200010169
Subjects: [ DSA OOPS DBMS CCN ]
Marks: [ 99 0 99 90 ]
CGPA 9.47

C++




// C++ program to demonstrate a
// structure for multiple student details
#include <bits/stdc++.h>
using namespace std;
 
// Structure Of students
struct student {
    string name;
    string rollno;
 
    // Subjects Enrolled(Array)
    vector<string> subjects;
 
    // Marks in each subject(Array)
    vector<int> marks;
 
    // Student's CGPA
    float cgpa;
};
 
// Function to print a vector
// (for more "Different ways to
// print elements of vector" at GFG)
template <typename S> void printv(const vector<S>& v)
{
    cout << "[ ";
    // Iterating over all elements of vector
    for (auto elem : v) {
        cout << elem << " ";
    }
    cout << "]";
    cout << endl;
}
 
// Function to print a Student
void printStudent(student* s)
{
    cout << "Student Details:" << endl;
    cout << endl;
    cout << "Name: " << s->name << endl;
    cout << "Roll Number: " << s->rollno << endl;
    cout << "Subjects: ";
    printv(s->subjects);
    cout << "Marks: ";
    printv(s->marks);
    cout << "CGPA " << s->cgpa << endl;
}
 
// Driver Code
int main()
{
    // Array of Students
    student arrayofstudents[10];
 
    // Student 1
    arrayofstudents[0].name = "GeeksforGeeks";
    arrayofstudents[0].rollno = "S20200010234";
    arrayofstudents[0].subjects
        = { "DSA", "OOPS", "DBMS", "CCN" };
    arrayofstudnets[0].marks = { 89, 78, 86, 90 };
    arrayofstudnets[0].cgpa = 8.918;
 
    // Student 2
    arrayofstudnets[1].name = "GFG";
    arrayofstudnets[1].rollno = "S20200010164";
    arrayofstudnets[1].subjects
        = { "DSA", "OOPS", "DBMS", "CCN" };
    arrayofstudnets[1].marks = { 89, 80, 89, 80 };
    arrayofstudnets[1].cgpa = 8.45;
 
    // Student 3
    arrayofstudnets[2].name = "gfg";
    arrayofstudnets[2].rollno = "S20200010169";
    arrayofstudnets[2].subjects
        = { "DSA", "OOPS", "DBMS", "CCN" };
    arrayofstudnets[2].marks = { 99, 00, 99, 90 };
    arrayofstudnets[2].cgpa = 9.47;
 
    // Loop to print all students
    for (int i = 0; i < 3; i++) {
 
        // Function call
        printStudent(&arrayofstudnets[i]);
    }
 
    return 0;
}


Output

Student Details:

Name: GeeksforGeeks
Roll Number: S20200010234
Subjects: [ DSA OOPS DBMS CCN ]
Marks: [ 89 78 86 90 ]
CGPA 8.918
Student Details:

Name: GFG
Roll Number: S20200010164
Subjects: [ DSA OOPS DBMS CCN ]
Marks: [ 89 80 89 80 ]
CGPA 8.45
Student Details:

Name: gfg
Roll Number: S20200010169
Subjects: [ DSA OOPS DBMS CCN ]
Marks: [ 99 0 99 90 ]
CGPA 9.47

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!