Introduction:
In this C++ tutorial, we will learn how to read the names and marks of a list of students by using a structure
. We will create one structure
that can be used to hold the name and marks for one student and we will create one array
of that structure to hold the names and marks of a list of students.
This program will take all inputs from the user. We are explaining everything at the bottom of this post. You can try to run this program with different inputs.
C++ program to read the names and marks of a list of students:
#include <iostream>
using namespace std;
// 1
struct student
{
string name;
int totalSubjects;
int marks[20];
};
// 2
void printStudentDetails(student students[], int totalStudents)
{
// 3
for (int i = 0; i < totalStudents; i++)
{
cout << "Student name: " << students[i].name << endl;
cout << "Marks:" << endl;
for (int j = 0; j < students[i].totalSubjects; j++)
{
cout << students[i].marks[j] << endl;
}
}
}
int main()
{
// 4
int totalStudents;
cout << "Enter total number of students: ";
cin >> totalStudents;
// 5
if (totalStudents <= 0)
{
cout << "Please enter a valid number" << endl;
return -1;
}
student studentArray[totalStudents];
// 6
for (int i = 0; i < totalStudents; i++)
{
// 7
cout << "Enter the name of student " << (i + 1) << ":" << endl;
cin >> studentArray[i].name;
cout << "Enter total number of subjects: ";
cin >> studentArray[i].totalSubjects;
// 8
if (studentArray[i].totalSubjects > 20 || studentArray[i].totalSubjects <= 0)
{
cout << "Please enter a valid number" << endl;
return -1;
}
// 9
for (int j = 0; j < studentArray[i].totalSubjects; j++)
{
cout << "Enter marks for subject " << (j + 1) << ":" << endl;
cin >> studentArray[i].marks[j];
}
}
// 10
printStudentDetails(studentArray, totalStudents);
}
Download the program on GitHub
Explanation :
The commented numbers in the above program denote the step numbers below:
- The
student
is a structure. We will create one array of this structure type to hold the information of all students. It has three members: thestring
membername
is used for the name of the student, theint
membertotalSubjects
is used for the total number of subjects for the student, and theint
arraymarks
is used for the marks of each subject. - The
printStudentDetails
function prints the details of astudent
array. It has two parameters: the array of students and the total number of students. It loops through the array of students and prints the contents. - We are using one
for
loop to iterate through the student array. This loop runs fromi = 0
toi = totalStudents - 1
. Inside the loop, we are printing down the name of the student and the marks of the subjects. One more innerfor
loop is used to iterate through all subjects and it will print all marks. For each iteration of the outer loop, the inner loop runs fortotalSubjects
number of times for that student. - Inside the main function, we have one integer variable
totalStudents
to assign the total number of students. This value is entered by the user. - This is a safety check. If the user enters an invalid number for total students, it will quit the program. If the entered number is valid, create one array to hold the information of the students.
- Start one
for
loop to iterate through the array of students one by one. - Inside the loop, ask the user to enter the name of the student. Read and assign it to the
name
member for that specific array element. Similarly, read the total number of subjects. - This is another safety check. The total subjects should be greater than 0 and less than 20 because our
marks
array in the structure can hold a maximum of 20 elements. - Use one
for
loop to read the marks of each student. This is an inner loop. For each student, it will run fortotalSubjects
number of times. Get the marks from the user one by one. - Finally, call the
printStudentDetails
function to print the details.
Sample Output :
Enter total number of students : 2
Enter the name of student 1 :
Alex
Enter total number of subjects : 2
Enter marks for subject 1 :
40
Enter marks for subject 2 :
45
Enter the name of student 2 :
Bob
Enter total number of subjects : 3
Enter marks for subject 1 :
50
Enter marks for subject 2 :
54
Enter marks for subject 3 :
56
Student name : Alex
Marks :
40
45
Student name : Bob
Marks :
50
54
56
Conclusion :
You can add an unlimited number of students and up to 20 subject marks. You can download the program on GitHub and please raise a PR if you have any better solution.
You might also like:
- 3 different C++ program to find the largest of three numbers
- How to convert decimal to binary in C++
- C++ program to find student grades using if-else
- C++ program to find a value in a linkedList
- C++ program to print a triangle with 1 and 0
- C++ program to implement binary search
- C++ program to delete the middle node in a linked list
- C++ program to capitalize first and last character of each word in a string