Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement
2.1.
Some Example
3.
Program to store the information of a student in a structure in C++
3.1.
Structure in C++
3.2.
Example to store the information of a student
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024

Program to store the information of a student in a structure

Introduction

We may come across situations where we need to store a list of data more complex than a list of numbers or strings in programming. For example, to represent a student, we might want the name, roll number, age, email id, phone number, marks, class, etc. This information relates to a single student entity and should be saved using a special user-defined data type. This data type is called the structure

This blog will discuss the solutions to this problem on how to store the information of a student in a structure in C++. We will code these solutions in the C++ language. We will discuss the solution to this problem.

To learn more about C++ programs, visit the C++ guided path.

Problem Statement

Write a program to store the information of a student in a structure in C++.

Some Example

Input

Enter information,

Enter name: CodingNinja

Enter roll number: 202

Enter marks: 95.6

Output

Displaying information,

Name: CodingNinja

Roll: 202

Marks: 95.6

Explanation

We will take the input in this program and then store it in a custom student structure. Now, we can use this structure to store the information of a student in a structure in C++.

Also Read - C++ Interview Questions

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 readDecimal 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

  1. 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.
     
  2. How to access the structure elements?
    We can access structure members using the dot (.) operator. For example, s.name.
     
  3. 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.
     
  4. 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 PythonC++ DSA CourseDSA in Java Course to give your career an edge over others.

 Happy Coding!

Live masterclass