1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
#include <iostream> #include <vector> #include <string> class Record { public: int id; std::string name; int age; Record(int i, const std::string &n, int a) : id(i), name(n), age(a) {} }; class Database { private: std::vector<Record> records; public: void addRecord(int id, const std::string &name, int age) { records.emplace_back(id, name, age); std::cout << "Record added successfully!\n"; } void displayRecords() const { if (records.empty()) { std::cout << "No records to display.\n"; return; } std::cout << "ID\tName\t\tAge\n"; std::cout << "--------------------------------\n"; for (const auto &record : records) { std::cout << record.id << "\t" << record.name << "\t\t" << record.age << "\n"; } } void searchRecord(int id) const { for (const auto &record : records) { if (record.id == id) { std::cout << "Record Found:\n"; std::cout << "ID: " << record.id << ", Name: " << record.name << ", Age: " << record.age << "\n"; return; } } std::cout << "Record not found.\n"; } void deleteRecord(int id) { for (auto it = records.begin(); it != records.end(); ++it) { if (it->id == id) { records.erase(it); std::cout << "Record deleted successfully!\n"; return; } } std::cout << "Record not found.\n"; } }; int main() { Database db; int choice, id, age; std::string name; do { std::cout << "\n--- Database Management System ---\n"; std::cout << "1. Add Record\n"; std::cout << "2. Display Records\n"; std::cout << "3. Search Record\n"; std::cout << "4. Delete Record\n"; std::cout << "5. Exit\n"; std::cout << "Enter your choice: "; std::cin >> choice; switch (choice) { case 1: std::cout << "Enter ID: "; std::cin >> id; std::cin.ignore(); // to ignore newline after ID input std::cout << "Enter Name: "; std::getline(std::cin, name); std::cout << "Enter Age: "; std::cin >> age; db.addRecord(id, name, age); break; case 2: db.displayRecords(); break; case 3: std::cout << "Enter ID to search: "; std::cin >> id; db.searchRecord(id); break; case 4: std::cout << "Enter ID to delete: "; std::cin >> id; db.deleteRecord(id); break; case 5: std::cout << "Exiting...\n"; break; default: std::cout << "Invalid choice! Please try again.\n"; } } while (choice != 5); return 0; } |
Explanation
- Record Class:
- The
Record
class represents a single record in the database. It contains three data members:id
Advertisementname
, andage
.
- The
- Database Class:
- The
Database
class contains astd::vector
ofRecord
objects, representing the collection of records in the database. - The class has the following member functions:
addRecord(int id, const std::string &name, int age)
: Adds a new record to the database.displayRecords() const
: Displays all records in the database.searchRecord(int id) const
: Searches for a record by its ID and displays it.deleteRecord(int id)
: Deletes a record from the database by its ID.
Advertisement - The
- Main Function:
- The
main
function provides a menu-driven interface for the user to interact with the database. - The user can add, display, search, and delete records through the menu options.
- The program continues to run until the user chooses to exit.
Advertisement - The