1. Introduction
Creating a Student Management System in C++ is an excellent way to apply basic C++ concepts and understand how to manage and manipulate data. This blog post demonstrates how to build a basic Student Management System in C++ that incorporates CRUD (Create, Read, Update, Delete) operations and search functionality. The system manages student information, including ID, name, roll number, and age.
2. Program Steps
1. Define a Student class with properties: ID, name, roll number, and age.
2. Implement functions for adding a new student, displaying student information, updating student details, deleting a student record, and searching for students by ID.
3. Create a menu-driven interface to interact with the system.
3. Code Program
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Student {
public:
int id;
string name;
int rollNo;
int age;
Student(int id, string name, int rollNo, int age) {
this->id = id;
this->name = name;
this->rollNo = rollNo;
this->age = age;
}
};
vector<Student> students;
void addStudent() {
int id, rollNo, age;
string name;
cout << "Enter student ID: ";
cin >> id;
cout << "Enter student name: ";
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter age: ";
cin >> age;
students.push_back(Student(id, name, rollNo, age));
cout << "Student added successfully.\n";
}
void displayStudents() {
for (Student &s : students) {
cout << "ID: " << s.id << ", Name: " << s.name << ", Roll No: " << s.rollNo << ", Age: " << s.age << endl;
}
}
void updateStudent() {
int id, newAge, newRollNo;
string newName;
cout << "Enter student ID to update: ";
cin >> id;
for (Student &s : students) {
if (s.id == id) {
cout << "Enter new name: ";
cin >> newName;
s.name = newName;
cout << "Enter new roll number: ";
cin >> newRollNo;
s.rollNo = newRollNo;
cout << "Enter new age: ";
cin >> newAge;
s.age = newAge;
cout << "Student updated successfully.\n";
return;
}
}
cout << "Student not found.\n";
}
void deleteStudent() {
int id;
cout << "Enter student ID to delete: ";
cin >> id;
for (auto it = students.begin(); it != students.end(); ++it) {
if (it->id == id) {
students.erase(it);
cout << "Student deleted successfully.\n";
return;
}
}
cout << "Student not found.\n";
}
void searchStudent() {
int id;
cout << "Enter student ID to search: ";
cin >> id;
for (Student &s : students) {
if (s.id == id) {
cout << "ID: " << s.id << ", Name: " << s.name << ", Roll No: " << s.rollNo << ", Age: " << s.age << endl;
return;
}
}
cout << "Student not found.\n";
}
int main() {
int choice;
do {
cout << "\nStudent Management System\n";
cout << "1. Add Student\n";
cout << "2. Display Students\n";
cout << "3. Update Student\n";
cout << "4. Delete Student\n";
cout << "5. Search Student\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: addStudent(); break;
case 2: displayStudents(); break;
case 3: updateStudent(); break;
case 4: deleteStudent(); break;
case 5: searchStudent(); break;
case 6: cout << "Exiting system.\n"; break;
default: cout << "Invalid choice. Please try again.\n";
}
} while (choice != 6);
return 0;
}
Output:
Student Management System 1. Add Student 2. Display Students 3. Update Student 4. Delete Student 5. Search Student 6. Exit Enter your choice: 1 Enter student ID: 1 Enter student name: Ramesh Enter roll number: 100 Enter age: 20 Student added successfully. Student Management System 1. Add Student 2. Display Students 3. Update Student 4. Delete Student 5. Search Student 6. Exit Enter your choice: 1 Enter student ID: 2 Enter student name: Pramod Enter roll number: 101 Enter age: 20 Student added successfully. Student Management System 1. Add Student 2. Display Students 3. Update Student 4. Delete Student 5. Search Student 6. Exit Enter your choice: 2 ID: 1, Name: Ramesh, Roll No: 100, Age: 20 ID: 2, Name: Pramod, Roll No: 101, Age: 20 Student Management System 1. Add Student 2. Display Students 3. Update Student 4. Delete Student 5. Search Student 6. Exit Enter your choice: 3 Enter student ID to update: 1 Enter new name: Ram Enter new roll number: 100 Enter new age: 20 Student updated successfully. Student Management System 1. Add Student 2. Display Students 3. Update Student 4. Delete Student 5. Search Student 6. Exit Enter your choice: 5 Enter student ID to search: 1 ID: 1, Name: Ram, Roll No: 100, Age: 20 Student Management System 1. Add Student 2. Display Students 3. Update Student 4. Delete Student 5. Search Student 6. Exit Enter your choice: 4 Enter student ID to delete: 1 Student deleted successfully. Student Management System 1. Add Student 2. Display Students 3. Update Student 4. Delete Student 5. Search Student 6. Exit Enter your choice: 3 Enter student ID to update: 2 Enter new name: Suresh Enter new roll number: 101 Enter new age: 20 Student updated successfully. Student Management System 1. Add Student 2. Display Students 3. Update Student 4. Delete Student 5. Search Student 6. Exit Enter your choice: 2 ID: 2, Name: Suresh, Roll No: 101, Age: 20 Student Management System 1. Add Student 2. Display Students 3. Update Student 4. Delete Student 5. Search Student 6. Exit Enter your choice: 5 Enter student ID to search: 2 ID: 2, Name: Suresh, Roll No: 101, Age: 20 Student Management System 1. Add Student 2. Display Students 3. Update Student 4. Delete Student 5. Search Student 6. Exit Enter your choice: 6 Exiting system.
Explanation:
1. The Student class defines the structure for storing student information.
2. A vector named students serves as the database to store student records.
3. CRUD operations are implemented through functions: addStudent, displayStudents, updateStudent, deleteStudent, and a search operation through searchStudent.
4. The main function provides a menu-driven interface for users to interact with the system, invoking the appropriate functions based on user input.
5. This tutorial exemplifies handling basic operations in a simple management system, showcasing object-oriented programming and standard library usage in C++.
Comments
Post a Comment
Leave Comment