C++ Program to Store Information of 10 Students using Structure
C++ program to store information of 10 students using structure.
In this article, you will learn how to make a c++ program to store information of 10 students using structure.
Example
Enter the number of students: 10
Enter the students' information:
Information for Roll Number: 1
Enter the first name: Student-1
Enter the last name: Test-1
Enter the marks: 50
Information for Roll Number: 2
Enter the first name: Student-2
Enter the last name: Test-2
Enter the marks: 55
.
.
.
Information for Roll Number: 9
Enter the first name: Student-9
Enter the last name: Test-9
Enter the marks: 90
Information for Roll Number: 10
Enter the first name: Student-10
Enter the last name: Test-10
Enter the marks: 95
Display the student's information:
The Roll Number: 1
The First Name: Student-1
The Last Name: Test-1
The Marks: 50.0
The Roll Number: 2
The First Name: Student-2
The Last Name: Test-2
The Marks: 55.0
.
.
.
The Roll Number: 9
The First Name: Student-9
The Last Name: Test-9
The Marks: 90.0
The Roll Number: 10
The First Name: Student-10
The Last Name: Test-10
The Marks: 95.0
You should have knowledge of the following topics in c++ programming to understand this program:
- C++ Structure DataType
- C++
for
loop statement - C++
cin
object - C++
cout
object - C++ Structure
Source Code
// C++ Program to Store Information of 10 Students using Structure
#include <bits/stdc++.h>
using namespace std;
struct Student {
char first_name[50];
char last_name[50];
int roll_number;
float marks;
};
int main() {
int x, i;
// x - To store number of students
cout << "Enter the number of students: ";
cin >> x;
struct Student s[x + 1];
// It will store the student's information
cout << "\nEnter the students' information:\n";
for (i = 0; i < x; i++) {
s[i].roll_number = i + 1;
cout << "\nInformation for Roll Number:\t" << s[i].roll_number << "\n";
cout << "Enter the first name: ";
cin >> s[i].first_name;
cout << "Enter the last name: ";
cin >> s[i].last_name;
cout << "Enter the marks: ";
cin >> s[i].marks;
}
// It will display the student's information
cout << "\n\nDisplay the student's information:\n";
for (i = 0; i < x; i++) {
cout << "\nThe Roll Number:\t" << (i + 1) << "\n";
cout << "The First Name: ";
puts(s[i].first_name);
cout << "The Last Name: ";
puts(s[i].last_name);
cout << "The Marks: " << s[i].marks << "\n";
cout << endl;
}
return 0;
}
Output
Enter the number of students: 10
Enter the students' information:
Information for Roll Number: 1
Enter the first name: Student-1
Enter the last name: Test-1
Enter the marks: 50
Information for Roll Number: 2
Enter the first name: Student-2
Enter the last name: Test-2
Enter the marks: 55
Information for Roll Number: 3
Enter the first name: Student-3
Enter the last name: Test-3
Enter the marks: 60
Information for Roll Number: 4
Enter the first name: Student-4
Enter the last name: Test-4
Enter the marks: 65
Information for Roll Number: 5
Enter the first name: Student-5
Enter the last name: Test-5
Enter the marks: 70
Information for Roll Number: 6
Enter the first name: Student-6
Enter the last name: Test-6
Enter the marks: 75
Information for Roll Number: 7
Enter the first name: Student-7
Enter the last name: Test-7
Enter the marks: 80
Information for Roll Number: 8
Enter the first name: Student-8
Enter the last name: Test-8
Enter the marks: 85
Information for Roll Number: 9
Enter the first name: Student-9
Enter the last name: Test-9
Enter the marks: 90
Information for Roll Number: 10
Enter the first name: Student-10
Enter the last name: Test-10
Enter the marks: 95
Display the student's information:
The Roll Number: 1
The First Name: Student-1
The Last Name: Test-1
The Marks: 50.0
The Roll Number: 2
The First Name: Student-2
The Last Name: Test-2
The Marks: 55.0
The Roll Number: 3
The First Name: Student-3
The Last Name: Test-3
The Marks: 60.0
The Roll Number: 4
The First Name: Student-4
The Last Name: Test-4
The Marks: 65.0
The Roll Number: 5
The First Name: Student-5
The Last Name: Test-5
The Marks: 70.0
The Roll Number: 6
The First Name: Student-6
The Last Name: Test-6
The Marks: 75.0
The Roll Number: 7
The First Name: Student-7
The Last Name: Test-7
The Marks: 80.0
The Roll Number: 8
The First Name: Student-8
The Last Name: Test-8
The Marks: 85.0
The Roll Number: 9
The First Name: Student-9
The Last Name: Test-9
The Marks: 90.0
The Roll Number: 10
The First Name: Student-10
The Last Name: Test-10
The Marks: 95.0
Explanation
In this program, we asked the user to enter the 10
number of students to add to the student structure variable.
After that, we filled in 10 students' input data like First Name, Last Name, and Marks of the students.
Then applied for
loop statement to make this calculation and It returned the 10 students' records as output.
Also, visit these links
C Program to Find Factors of a Number