In this lesson, we will build a simple Student Management System using C++. This project combines several concepts learned throughout the C++ tutorial, including variables, input/output, conditions, loops, functions, structures, vectors, and file handling.
Our project will manage basic student information.
The application will allow the user to:
The project will have a menu-driven interface.
===== Student Management System =====
1. Add Student
2. Display Students
3. Search Student
4. Update Student
5. Delete Student
6. Exit
The user selects an option and the program performs the corresponding operation.
This project combines several C++ concepts:
A structure can be used to group student information together.
struct Student {
int id;
std::string name;
int age;
float marks;
};
Each Student object contains an ID, name, age, and marks.
A vector can store multiple student objects.
std::vector<Student> students;
We can add students dynamically using push_back().
Student s;
s.id = 101;
s.name = "Rahul";
s.age = 20;
s.marks = 85.5;
students.push_back(s);
Create a function to take student information from the user.
void addStudent(
std::vector<Student>& students
) {
Student s;
std::cout <<
"Enter ID: ";
std::cin >>
s.id;
std::cin.ignore();
std::cout <<
"Enter Name: ";
std::getline(
std::cin,
s.name
);
std::cout <<
"Enter Age: ";
std::cin >>
s.age;
std::cout <<
"Enter Marks: ";
std::cin >>
s.marks;
students.push_back(s);
std::cout <<
"Student added successfully.\n";
}
A range-based for loop can display all student records.
void displayStudents(
const std::vector<Student>& students
) {
for(
const Student& s :
students
) {
std::cout <<
"ID: "
<< s.id
<< std::endl;
std::cout <<
"Name: "
<< s.name
<< std::endl;
std::cout <<
"Age: "
<< s.age
<< std::endl;
std::cout <<
"Marks: "
<< s.marks
<< std::endl;
std::cout <<
"-----------------\n";
}
}
We can search for a student using the student ID.
void searchStudent(
const std::vector<Student>& students
) {
int id;
std::cout <<
"Enter Student ID: ";
std::cin >>
id;
for(
const Student& s :
students
) {
if(s.id == id) {
std::cout <<
"Student Found\n";
std::cout <<
"Name: "
<< s.name
<< std::endl;
std::cout <<
"Marks: "
<< s.marks
<< std::endl;
return;
}
}
std::cout <<
"Student not found.\n";
}
The program can search for a student and update the student's information.
void updateStudent(
std::vector<Student>& students
) {
int id;
std::cout <<
"Enter Student ID: ";
std::cin >>
id;
for(
Student& s :
students
) {
if(s.id == id) {
std::cin.ignore();
std::cout <<
"Enter New Name: ";
std::getline(
std::cin,
s.name
);
std::cout <<
"Enter New Age: ";
std::cin >>
s.age;
std::cout <<
"Enter New Marks: ";
std::cin >>
s.marks;
std::cout <<
"Student updated.\n";
return;
}
}
std::cout <<
"Student not found.\n";
}
The erase() function can remove a student from the vector.
void deleteStudent(
std::vector<Student>& students
) {
int id;
std::cout <<
"Enter Student ID: ";
std::cin >>
id;
for(
auto it = students.begin();
it != students.end();
++it
) {
if(it->id == id) {
students.erase(it);
std::cout <<
"Student deleted.\n";
return;
}
}
std::cout <<
"Student not found.\n";
}
The project can store student information in a text file.
#include <fstream>
std::ofstream file(
"students.txt"
);
The file can be used to save student records so that the information is available when the program runs again.
A function can write all student records to a file.
void saveStudents(
const std::vector<Student>& students
) {
std::ofstream file(
"students.txt"
);
for(
const Student& s :
students
) {
file <<
s.id << "|"
<< s.name << "|"
<< s.age << "|"
<< s.marks
<< "\n";
}
file.close();
}
The pipe character is used here as a simple separator between fields.
The saved records can be loaded when the program starts.
void loadStudents(
std::vector<Student>& students
) {
std::ifstream file(
"students.txt"
);
Student s;
std::string separator;
while(
file >>
s.id
) {
file.ignore(
1,
'|'
);
std::getline(
file,
s.name,
'|'
);
file >>
s.age;
file.ignore(
1,
'|'
);
file >>
s.marks;
students.push_back(s);
}
file.close();
}
The menu provides an easy way for the user to select an operation.
void showMenu() {
std::cout <<
"\n===== Student Management System =====\n";
std::cout <<
"1. Add Student\n";
std::cout <<
"2. Display Students\n";
std::cout <<
"3. Search Student\n";
std::cout <<
"4. Update Student\n";
std::cout <<
"5. Delete Student\n";
std::cout <<
"6. Exit\n";
}
A while loop can keep the application running until the
user selects Exit.
int choice = 0;
while(choice != 6) {
showMenu();
std::cout <<
"Enter choice: ";
std::cin >>
choice;
}
The loop continues until choice becomes
6.
A switch statement is suitable for handling the menu
choices.
switch(choice) {
case 1:
addStudent(students);
break;
case 2:
displayStudents(students);
break;
case 3:
searchStudent(students);
break;
case 4:
updateStudent(students);
break;
case 5:
deleteStudent(students);
break;
case 6:
std::cout <<
"Goodbye!";
break;
default:
std::cout <<
"Invalid choice.";
}
The main program can connect the menu, vector, and functions together.
int main() {
std::vector<Student> students;
loadStudents(students);
int choice;
do {
showMenu();
std::cin >>
choice;
switch(choice) {
case 1:
addStudent(students);
break;
case 2:
displayStudents(students);
break;
case 3:
searchStudent(students);
break;
case 4:
updateStudent(students);
break;
case 5:
deleteStudent(students);
break;
case 6:
saveStudents(students);
break;
default:
std::cout <<
"Invalid choice.\n";
}
} while(choice != 6);
return 0;
}
A clean project can be divided into the following parts:
Headers
↓
Student Structure
↓
Add Function
↓
Display Function
↓
Search Function
↓
Update Function
↓
Delete Function
↓
Save Function
↓
Load Function
↓
Menu Function
↓
main()
Dividing the program into functions makes the code easier to read and maintain.
Programs should validate user input whenever possible.
if(s.age < 1 || s.age > 100) {
std::cout <<
"Invalid age.";
return;
}
if(
s.marks < 0 ||
s.marks > 100
) {
std::cout <<
"Invalid marks.";
return;
}
This prevents obviously invalid values from being stored.
Student IDs should normally be unique. We can check whether an ID already exists before adding a new student.
bool idExists(
const std::vector<Student>& students,
int id
) {
for(
const Student& s :
students
) {
if(s.id == id) {
return true;
}
}
return false;
}
The function returns true if the ID already exists.
void addStudent(
std::vector<Student>& students
) {
Student s;
std::cout <<
"Enter ID: ";
std::cin >>
s.id;
if(
idExists(
students,
s.id
)
) {
std::cout <<
"ID already exists.\n";
return;
}
std::cin.ignore();
std::cout <<
"Enter Name: ";
std::getline(
std::cin,
s.name
);
std::cout <<
"Enter Age: ";
std::cin >>
s.age;
std::cout <<
"Enter Marks: ";
std::cin >>
s.marks;
students.push_back(s);
std::cout <<
"Student added successfully.\n";
}
===== Student Management System =====
1. Add Student
2. Display Students
3. Search Student
4. Update Student
5. Delete Student
6. Exit
Enter choice: 1
Enter ID: 101
Enter Name: Rahul Kumar
Enter Age: 20
Enter Marks: 87
Student added successfully.
Enter choice: 2
ID: 101
Name: Rahul Kumar
Age: 20
Marks: 87
-----------------
This is an example of how the menu-driven application can interact with the user.
Enter choice: 3
Enter Student ID: 101
Student Found
Name: Rahul Kumar
Marks: 87
The search function compares the entered ID with the IDs stored in the vector.
Enter choice: 4
Enter Student ID: 101
Enter New Name: Rahul Singh
Enter New Age: 21
Enter New Marks: 91
Student updated.
The existing student object is modified using a reference.
Enter choice: 5
Enter Student ID: 101
Student deleted.
The matching element is removed from the vector using
erase().
The following program combines the main concepts into one complete student management application.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
struct Student {
int id;
std::string name;
int age;
float marks;
};
bool idExists(
const std::vector<Student>& students,
int id
) {
for(
const Student& s :
students
) {
if(s.id == id) {
return true;
}
}
return false;
}
void addStudent(
std::vector<Student>& students
) {
Student s;
std::cout <<
"Enter ID: ";
std::cin >>
s.id;
if(
idExists(
students,
s.id
)
) {
std::cout <<
"ID already exists.\n";
return;
}
std::cin.ignore();
std::cout <<
"Enter Name: ";
std::getline(
std::cin,
s.name
);
std::cout <<
"Enter Age: ";
std::cin >>
s.age;
std::cout <<
"Enter Marks: ";
std::cin >>
s.marks;
students.push_back(s);
std::cout <<
"Student added successfully.\n";
}
void displayStudents(
const std::vector<Student>& students
) {
if(students.empty()) {
std::cout <<
"No students found.\n";
return;
}
for(
const Student& s :
students
) {
std::cout <<
"\nID: "
<< s.id;
std::cout <<
"\nName: "
<< s.name;
std::cout <<
"\nAge: "
<< s.age;
std::cout <<
"\nMarks: "
<< s.marks;
std::cout <<
"\n-----------------\n";
}
}
void searchStudent(
const std::vector<Student>& students
) {
int id;
std::cout <<
"Enter Student ID: ";
std::cin >>
id;
for(
const Student& s :
students
) {
if(s.id == id) {
std::cout <<
"\nStudent Found\n";
std::cout <<
"Name: "
<< s.name
<< std::endl;
std::cout <<
"Age: "
<< s.age
<< std::endl;
std::cout <<
"Marks: "
<< s.marks
<< std::endl;
return;
}
}
std::cout <<
"Student not found.\n";
}
void updateStudent(
std::vector<Student>& students
) {
int id;
std::cout <<
"Enter Student ID: ";
std::cin >>
id;
for(
Student& s :
students
) {
if(s.id == id) {
std::cin.ignore();
std::cout <<
"Enter New Name: ";
std::getline(
std::cin,
s.name
);
std::cout <<
"Enter New Age: ";
std::cin >>
s.age;
std::cout <<
"Enter New Marks: ";
std::cin >>
s.marks;
std::cout <<
"Student updated.\n";
return;
}
}
std::cout <<
"Student not found.\n";
}
void deleteStudent(
std::vector<Student>& students
) {
int id;
std::cout <<
"Enter Student ID: ";
std::cin >>
id;
for(
auto it = students.begin();
it != students.end();
++it
) {
if(it->id == id) {
students.erase(it);
std::cout <<
"Student deleted.\n";
return;
}
}
std::cout <<
"Student not found.\n";
}
void saveStudents(
const std::vector<Student>& students
) {
std::ofstream file(
"students.txt"
);
if(!file) {
std::cout <<
"Unable to save data.\n";
return;
}
for(
const Student& s :
students
) {
file <<
s.id << "|"
<< s.name << "|"
<< s.age << "|"
<< s.marks
<< "\n";
}
}
void loadStudents(
std::vector<Student>& students
) {
std::ifstream file(
"students.txt"
);
if(!file) {
return;
}
Student s;
while(
file >>
s.id
) {
file.ignore(
1,
'|'
);
std::getline(
file,
s.name,
'|'
);
file >>
s.age;
file.ignore(
1,
'|'
);
file >>
s.marks;
students.push_back(s);
}
}
void showMenu() {
std::cout <<
"\n===== Student Management System =====\n";
std::cout <<
"1. Add Student\n";
std::cout <<
"2. Display Students\n";
std::cout <<
"3. Search Student\n";
std::cout <<
"4. Update Student\n";
std::cout <<
"5. Delete Student\n";
std::cout <<
"6. Exit\n";
}
int main() {
std::vector<Student> students;
loadStudents(students);
int choice;
do {
showMenu();
std::cout <<
"Enter choice: ";
std::cin >>
choice;
switch(choice) {
case 1:
addStudent(students);
break;
case 2:
displayStudents(students);
break;
case 3:
searchStudent(students);
break;
case 4:
updateStudent(students);
break;
case 5:
deleteStudent(students);
break;
case 6:
saveStudents(students);
std::cout <<
"Data saved. Goodbye!\n";
break;
default:
std::cout <<
"Invalid choice.\n";
}
} while(choice != 6);
return 0;
}
The complete project follows this basic flow:
Program Starts
↓
Load Existing Students
↓
Display Menu
↓
User Selects Option
↓
Perform Operation
↓
Display Menu Again
↓
User Selects Exit
↓
Save Students
↓
Program Ends
This structure demonstrates how individual C++ concepts can be combined to create a useful application.
The basic project can be expanded with many additional features.
These improvements can turn the basic project into a larger student management application.
After completing this project, you should understand how to combine multiple C++ concepts.
The Student Management System is a practical example of combining fundamental and intermediate C++ concepts.
| Concept | Used For |
|---|---|
| Structure | Representing student information. |
| Vector | Storing multiple students. |
| Functions | Separating application operations. |
| Switch | Handling menu choices. |
| Loops | Repeating the application menu and processing records. |
| File Handling | Saving and loading student records. |
| Search | Finding students by ID. |
| erase() | Deleting a student from the vector. |
The most important lesson is that programming concepts become more useful when they are combined to solve practical problems.
Question: Which C++ container is used in this mini project to store multiple student records dynamically?