C++ Program to Store and Display Information Using Structure
In this example, we will learn to Store and Display Information Using structures.
To understand this example, you should have the knowledge of the following C++ programming topics:
Introduction
In this program, a structure, student is created. This structure has three members: name (string), roll (integer) and marks (float). then, we created a structure array of size 10 to store information of 10 students.
Using for loop, the program takes the information of 10 students from the user and displays it on the screen.
Example: Program to Store Information in Structure and Display it
#include <iostream>
using namespace std;
struct student
{
char name[50];
int roll;
float marks;
} s[10];
int main()
{
cout << "Enter information of students: " << endl;
// storing information
for(int i = 0; i < 10; ++i)
{
s[i].roll = i+1;
cout << "For roll number: " << s[i].roll << endl;
cout << "Enter name: ";
cin >> s[i].name;
cout << "Enter marks: ";
cin >> s[i].marks;
cout << endl;
}
cout << "Displaying Information: " << endl;
// Displaying information
for(int i = 0; i < 10; ++i)
{
cout << "\nRoll number: " << i+1 << endl;
cout << "Name: " << s[i].name << endl;
cout << "Marks: " << s[i].marks << endl;
}
return 0;
}
Output
Enter information of students: For roll number: 1 Enter name: Tom Enter marks: 59 For roll number: 2 Enter name: Sam Enter marks: 58 . . . Displaying Information: Roll number: 1 Name: Tom Marks: 59 For roll number: 2 Enter name: Sam Enter marks: 58 . .
Next Example
We hope that this Example helped you develop better understanding of the concept of "Calculate Difference Between Two Time Periods" in C++.
Keep Learning : )
In the next Example, we will learn about C++ Increment ++ and Decrement -- Operator Overloading
.