Program to store the information of a student in a structure in C++
Now, we will solve this problem on how to store the information of a student in a structure in C++ in C++. To solve this problem, we will use structures in c++.
Structure in C++
First, let's start with the understanding of structures in C++. A structure in C++ is a collection of items of multiple data types. We can create complex data structures with multiple data types. We can define a structure with the struct keyword.
Syntax:
struct structureName{
member_1;
member_2;
member_3;
.
.
.
member_N;
};
You can also try this code with Online C++ Compiler
Run Code
Example:
struct studentData{
string name;
int roll_no;
float marks;
}
You can also try this code with Online C++ Compiler
Run Code
The above code is an example of creating a structure in C++ using the struct keyword.
Also read - Decimal to Binary c++
Example to store the information of a student
Here, we will see an example of creating and using a student structure in c++.
Code:
#include <iostream>
using namespace std;
// store the information of a student in a structure in C++
struct studentData{
string name;
int roll_no;
float marks;
};
// Driver Code
int main()
{
studentData s;
// Input student data
cout << "Enter information," << endl;
cout << "Enter name = ";
cin >> s.name;
cout << "Enter roll: ";
cin >> s.roll_no;
cout << "Enter marks: ";
cin >> s.marks;
// Output student data
cout << "\nDisplaying Information," << endl;
cout << "Name= " << s.name << endl;
cout << "Roll= " << s.roll_no << endl;
cout << "Marks= " << s.marks << endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
The above code will store the information of a student in a structure in C++. The output of the above code is displayed below.
Output
Try and compile with online c++ compiler.
Frequently Asked Questions
-
What types of members does a structure in C++ support?
Structures in C++ may contain two types of members:
Data Member: Data members are ordinary C++ variables. We may create a structure with variables of multiple data types in C++.
Member Functions: These members are ordinary C++ functions. Along with variables, we may also add functions inside the structure declaration.
-
How to access the structure elements?
We can access structure members using the dot (.) operator. For example, s.name.
-
Why do we need structures in C++?
We need structures to store variables of multiple data types inside a single container. Structures in C++ can store float value, int value, or whatever data type we want to keep.
-
Can we have an array of structures?
Yes! We can. The structure is just a user-defined data type and we can do all the things with structures as we do with primitive data types.
Conclusion
In this article, we have extensively discussed how to store the information of a student in C++.
We hope that this blog has helped you enhance your knowledge on how to store the information of a student in C++, and if you would like to learn more. You can also consider our Online Coding Courses such as the DSA in Python, C++ DSA Course, DSA in Java Course to give your career an edge over others.
Happy Coding!