0% found this document useful (0 votes)
12 views7 pages

Document 5

Uploaded by

iara.cat.bras
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Document 5

Uploaded by

iara.cat.bras
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

#include <iostream>

#include <vector>

#include <string>

using namespace std;

// Student class

class Student {

public:

int id;

string name;

int age;

// Constructor to initialize a new student

Student(int studentId, string studentName, int studentAge) {

id = studentId;

name = studentName;

age = studentAge;

// Method to display student details

void display() {

cout << "ID: " << id << ", Name: " << name << ", Age: " << age << endl;
}

};

// StudentManagementSystem class

class StudentManagementSystem {

private:

vector<Student> students;

public:

// Method to add a student

void addStudent(int id, string name, int age) {

Student newStudent(id, name, age);

students.push_back(newStudent);

cout << "Student added successfully!" << endl;

// Method to display all students

void displayStudents() {

if (students.empty()) {

cout << "No students available!" << endl;

return;

for (const Student &student : students) {


student.display();

// Method to search for a student by ID

void searchStudent(int id) {

bool found = false;

for (const Student &student : students) {

if (student.id == id) {

student.display();

found = true;

break;

if (!found) {

cout << "Student with ID " << id << " not found." << endl;

// Method to delete a student by ID

void deleteStudent(int id) {

auto it = students.begin();

while (it != students.end()) {


if (it->id == id) {

students.erase(it);

cout << "Student with ID " << id << " deleted successfully!" << endl;

return;

++it;

cout << "Student with ID " << id << " not found." << endl;

};

// Main function

int main() {

StudentManagementSystem sms;

int choice, id, age;

string name;

while (true) {

// Display menu

cout << "\nStudent Management System\n";

cout << "1. Add Student\n";

cout << "2. Display All Students\n";

cout << "3. Search Student by ID\n";


cout << "4. Delete Student by ID\n";

cout << "5. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1:

// Add student

cout << "Enter student ID: ";

cin >> id;

cout << "Enter student name: ";

cin.ignore(); // To clear the newline character left by cin

getline(cin, name);

cout << "Enter student age: ";

cin >> age;

sms.addStudent(id, name, age);

break;

case 2:

// Display all students

sms.displayStudents();

break;
case 3:

// Search student by ID

cout << "Enter student ID to search: ";

cin >> id;

sms.searchStudent(id);

break;

case 4:

// Delete student by ID

cout << "Enter student ID to delete: ";

cin >> id;

sms.deleteStudent(id);

break;

case 5:

// Exit

cout << "Exiting system. Goodbye!" << endl;

return 0;

default:

cout << "Invalid choice! Please try again." << endl;

}
return 0;

You might also like