The Student Management System project is a simple implementation using C++ that provides functionalities to manage student records. It includes operations like creating new student records, displaying student details, updating, and deleting records.
Implementation
#include<iostream>
#include<vector>
#include<algorithm> // <-- Include this for std::remove_if
class Student {
private:
int roll_no;
std::string name;
float marks;
public:
Student(int r, std::string n, float m) : roll_no(r), name(n), marks(m) {}
int getRollNo() const { return roll_no; }
std::string getName() const { return name; }
float getMarks() const { return marks; }
void setName(std::string n) { name = n; }
void setMarks(float m) { marks = m; }
};
class StudentManagementSystem {
private:
std::vector<Student> students;
public:
void addStudent(int roll_no, std::string name, float marks) {
Student s(roll_no, name, marks);
students.push_back(s);
}
void displayStudents() {
for(const Student &s : students) {
std::cout << "Roll No: " << s.getRollNo() << ", Name: " << s.getName() << ", Marks: " << s.getMarks() << std::endl;
}
}
void updateStudent(int roll_no, std::string name, float marks) {
for(Student &s : students) {
if(s.getRollNo() == roll_no) {
s.setName(name);
s.setMarks(marks);
}
}
}
void deleteStudent(int roll_no) {
auto it = std::remove_if(students.begin(), students.end(),
[roll_no](const Student &s) { return s.getRollNo() == roll_no; });
students.erase(it, students.end());
}
};
int main() {
StudentManagementSystem sms;
int choice, roll_no;
std::string name;
float marks;
do {
std::cout << "1. Add Student\n2. Display Students\n3. Update Student\n4. Delete Student\n5. Exit\nEnter your choice: ";
std::cin >> choice;
switch(choice) {
case 1:
std::cout << "Enter roll number: ";
std::cin >> roll_no;
std::cin.ignore(); // <-- Ignore the newline character left in the buffer
std::cout << "Enter name: ";
std::getline(std::cin, name);
std::cout << "Enter marks: ";
std::cin >> marks;
sms.addStudent(roll_no, name, marks);
break;
case 2:
sms.displayStudents();
break;
case 3:
std::cout << "Enter roll number to update: ";
std::cin >> roll_no;
std::cin.ignore(); // <-- Ignore the newline character left in the buffer
std::cout << "Enter new name: ";
std::getline(std::cin, name);
std::cout << "Enter new marks: ";
std::cin >> marks;
sms.updateStudent(roll_no, name, marks);
break;
case 4:
std::cout << "Enter roll number to delete: ";
std::cin >> roll_no;
sms.deleteStudent(roll_no);
break;
}
} while(choice != 5);
return 0;
}
Explanation:
We use two classes: Student for representing individual student data and StudentManagementSystem to manage operations on students.Student class has three private attributes and related public functions to get and set these attributes. StudentManagementSystem uses a vector to store Student objects. It has methods to add, display, update, and delete student records.
In the main() function, a simple menu-driven approach is provided to allow users to interact with the system.
Output:
When executed, the program will show a menu allowing the user to:
- Add a student by providing roll number, name, and marks.
- Display all students' details.
- Update a student's name and marks by providing the roll number.
- Delete a student's record using the roll number.
- Exit the application.
Here is the complete Output:
1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 1
Enter roll number: 100
Enter name: Ramesh
Enter marks: 90
1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 2
Roll No: 100, Name: Ramesh, Marks: 90
1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 2
Roll No: 100, Name: Ramesh, Marks: 90
1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 3
Enter roll number to update: 100
Enter new name: Ram
Enter new marks: 80
1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 2
Roll No: 100, Name: Ram, Marks: 80
1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 4
Enter roll number to delete: 100
1. Add Student
2. Display Students
3. Update Student
4. Delete Student
5. Exit
Enter your choice: 5
I hope this gives a clear idea of how to implement a basic Student Management System in C++.
Happy coding!
Comments
Post a Comment