Using Structure To Display and Store Information in C++

Exercise:

Write a C++ program that can save information of a student with the help of structures.

Click Here to View the Solution:
#include <iostream>
using namespace std;
struct stdinfo
{
    char name[30];
    float marks;
    int rollno;
} 
stdt[5];
int main()
{
    cout << "Information of the student ";
    for(int x = 0; x < 5; ++x)
    {
        stdt[x].rollno = x+1;
        cout << "with roll number " << stdt[x].rollno << ":" << endl;
        cout << "Insert name: ";
        cin >> stdt[x].name;
        cout << "Insert marks: ";
        cin >> stdt[x].marks;
        cout << endl;
    }
    cout << "Showing Information: " << endl;
    for(int x = 0; x < 5; ++x)
    {
        cout << "\nroll number: " << x+1 << endl;
        cout << "Name: " << stdt[x].name << endl;
        cout << "Marks: " << stdt[x].marks << endl;
    }
    return 0;
}
Click Here to View the Output:
Information of the student with roll number 1:
Insert name: john
Insert marks: 80

with roll number 2:
Insert name: marie
Insert marks: 86

with roll number 3:
Insert name: samantha
Insert marks: 72

with roll number 4:
Insert name: bob
Insert marks: 94

with roll number 5:
Insert name: cody
Insert marks: 70

Showing Information:
roll number: 1
Name: john
Marks: 80

roll number: 2
Name: marie
Marks: 86

roll number: 3
Name: samantha
Marks: 72

roll number: 4
Name: bob
Marks: 94

roll number: 5
Name: cody
Marks: 70
Click Here to View the Explanation:
  • A structure stdinfo is char type variable name, int type rollno, and float type variable marks. A structure variable stdt is created and its string is limited to 5 elements.
  • Roll number, name and marks are entered by the user and stored in the structure variable stdt [x].
  • for loop is entered to display all the information on the screen.
%d bloggers like this: