C++ Program to Store Information of a Student in a Structure
Last Updated :
21 Jun, 2023
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.
Information in Structure
- Student Name (String).
- Student Roll Number (String).
- Subjects Enrolled (Array of Strings).
- Marks in each subject (Array of Int).
- CGPA(Float)
Example of the Structure
Yash Gupta
S20200010234
[DSA, OOPS ,DBMS, CCN]
[89,78,86,90]
8.918
1. For a Student
Below is the implementation of the topic:
C++
// C++ program to demonstrate
// a structure for student details
#include <bits/stdc++.h>
using namespace std;
// Structure Of students
struct student {
// Student Name
string name;
// Student Roll Number
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()
{
// 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;
}
OutputStudent Details:
Name: GeeksforGeeks
Roll Number: S20200010234
Subjects: [ DSA OOPS DBMS CCN ]
Marks: [ 89 78 86 90 ]
CGPA 8.918
2. 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
Below is the implementation of the above topic
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;
}
OutputStudent 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
Similar Reads
C program to print employee details using Structure In this program, we will read and display details of n employees using Structure. Employee details like Name, age, phone number, salary will be printed. Method : Declare variables using Structure Read details of all employees like employee name, age, phone number and salary. Display all the details
2 min read
Structure of C++ Program The C++ program is written using a specific template structure. The structure of the program written in C++ language is as follows: Documentation Section:This section comes first and is used to document the logic of the program that the programmer going to code.It can be also used to write for purpo
5 min read
How to Create an Array of Structs in C++? In C++, a struct is a user-defined data type that allows us to combine data of different types and an array of structs is an array in which each element is of the struct type. In this article, we will learn how to create an array of structs in C++. Creating an Array of Structs in C++To create an arr
2 min read
How to Create std::vector of Custom Classes or Structs in C++? In C++, std::vectors are dynamic arrays that can resize themselves during the runtime, and classes or structs allow the users to create custom data types of their choice. There might be many situations when we want to store a custom data type into a vector. In this article, we will learn how to crea
3 min read
Student record management system using linked list Prerequisites: Linked listProblem: Create a student Record Management system that can perform the following operations: Insert Student recordDelete student recordShow student recordSearch student record The student record should contain the following items Name of StudentRoll Number of StudentCourse
10 min read