fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5.  
  6. using namespace std;
  7. struct PersonalRecord
  8. {
  9. string name;
  10. string dob;
  11. string phone;
  12.  
  13. bool operator<(const PersonalRecord& other)const
  14. {
  15. return name < other.name;
  16. }
  17. };
  18.  
  19. void displayRecords(const vector<PersonalRecord>& records)
  20. {
  21. cout << "\nPersonal Records:\n";
  22. cout << "--------\n";
  23. for (const auto& record : records)
  24. {
  25. cout << "Name: " << record.name << ", DOB: " << record.dob<< ", Phone: " << record.phone << endl;
  26. }
  27. cout << "------------------\n";
  28. }
  29.  
  30. void searchRecord(const vector<PersonalRecord>& records, const string& name)
  31. {
  32. auto it = find_if(records.begin(), records.end(),[&name](const PersonalRecord& record)
  33. {
  34. return record.name == name;
  35. });
  36.  
  37. if (it != records.end())
  38. {
  39. cout << "\nRecord Found:\n";
  40. cout << "Name: " << it->name << ", DOB: "<< it->dob<< ", Phone: "<< it->phone<< endl;
  41. }
  42. else
  43. {
  44. cout << "\nRecord not found.\n";
  45. }
  46. }
  47.  
  48. int main()
  49. {
  50. vector<PersonalRecord> records;
  51. int n;
  52.  
  53. cout << "Enter the number of records: ";
  54. cin >> n;
  55. cin.ignore();
  56. for (int i = 0; i < n; ++i) {
  57. PersonalRecord record;
  58. cout << "\nEnter details for record " << i + 1 << ":\n";
  59. cout << "Name: ";
  60. getline(cin, record.name);
  61. cout << "DOB (DD/MM/YYYY): ";
  62. getline(cin, record.dob);
  63. cout << "Phone: ";
  64. getline(cin, record.phone);
  65. records.push_back(record);
  66. }
  67. sort(records.begin(), records.end());
  68. displayRecords(records);
  69. string searchName;
  70. cout << "\nEnter a name to search: ";
  71. getline(cin, searchName);
  72. searchRecord(records, searchName);
  73.  
  74. return 0;
  75. }
  76.  
Success #stdin #stdout 0.02s 25696KB
stdin
Standard input is empty
stdout
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;
struct PersonalRecord
{
    string name;
    string dob;
    string phone;

    bool operator<(const PersonalRecord& other)const
    {
        return name < other.name;
    }
};

void displayRecords(const vector<PersonalRecord>& records)
{
    cout << "\nPersonal Records:\n";
    cout << "--------\n";
    for (const auto& record : records)
    {
        cout << "Name: " << record.name << ", DOB: " << record.dob<< ", Phone: " << record.phone << endl;
    }
    cout << "------------------\n";
}

void searchRecord(const vector<PersonalRecord>& records, const string& name)
{
    auto it = find_if(records.begin(), records.end(),[&name](const PersonalRecord& record)
    		{
    	return record.name == name;
    		});

    if (it != records.end())
    {
        cout << "\nRecord Found:\n";
        cout << "Name: " << it->name << ", DOB: "<< it->dob<< ", Phone: "<< it->phone<< endl;
    }
    else
    {
        cout << "\nRecord not found.\n";
    }
}

int main()
{
    vector<PersonalRecord> records;
    int n;

    cout << "Enter the number of records: ";
    cin >> n;
    cin.ignore();
    for (int i = 0; i < n; ++i) {
        PersonalRecord record;
        cout << "\nEnter details for record " << i + 1 << ":\n";
        cout << "Name: ";
        getline(cin, record.name);
        cout << "DOB (DD/MM/YYYY): ";
        getline(cin, record.dob);
        cout << "Phone: ";
        getline(cin, record.phone);
        records.push_back(record);
    }
    sort(records.begin(), records.end());
    displayRecords(records);
    string searchName;
    cout << "\nEnter a name to search: ";
    getline(cin, searchName);
    searchRecord(records, searchName);

    return 0;
}