In this article, you will learn how to make a C++ program to store information of a student in a structure with for loop in the c++ programming language.
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++ Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | // C++ program to store information of a student in a structure with for loop #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 is the 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's informations:\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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | Enter the number of students: 10 Enter the students's informations: 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 |
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 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.