Docsity

C++ Password Keychain Manager, Cheat Sheet of Computer Science

Toggle sidebar
bg1
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class PasswordEntry {
public:
string service;
string username;
string password;
PasswordEntry(string svc, string user, string pass)
: service(svc), username(user), password(pass) {}
};
class PasswordKeychain {
private:
vector<PasswordEntry> entries;
public:
void addEntry(const string& service, const string& username, const
string& password) {
entries.emplace_back(service, username, password);
cout << "Entry added successfully!" << endl;
}
void viewEntries() {
if (entries.empty()) {
cout << "No entries to display." << endl;
return;
}
cout << "\nPassword Keychain Entries:" << endl;
for (const auto& entry : entries) {
cout << "Service: " << entry.service
<< ", Username: " << entry.username
<< ", Password: " << entry.password << endl;
}
}
void searchEntry(const string& service) {
for (const auto& entry : entries) {
if (entry.service == service) {
cout << "Service: " << entry.service
<< ", Username: " << entry.username
<< ", Password: " << entry.password << endl;
return;
}
}
cout << "No entry found for the service: " << service << endl;
}
};
int main() {
PasswordKeychain keychain;
int choice;
do {
cout << "\nPassword Keychain Menu:" << endl;
cout << "1. Add Entry" << endl;
cout << "2. View Entries" << endl;
cout << "3. Search Entry" << endl;
cout << "4. Exit" << endl;
cout << "Choose an option: ";
cin >> choice;
switch (choice) {
case 1: {
string service, username, password;
cout << "Enter service name: ";
pf2

Partial preview of the text

Download C++ Password Keychain Manager and more Cheat Sheet Computer Science in PDF only on Docsity!

#include #include #include using namespace std; class PasswordEntry { public: string service; string username; string password; PasswordEntry(string svc, string user, string pass) : service(svc), username(user), password(pass) {} }; class PasswordKeychain { private: vector entries; public: void addEntry(const string& service, const string& username, const string& password) { entries.emplace_back(service, username, password); cout << "Entry added successfully!" << endl; } void viewEntries() { if (entries.empty()) { cout << "No entries to display." << endl; return; } cout << "\nPassword Keychain Entries:" << endl; for (const auto& entry : entries) { cout << "Service: " << entry.service << ", Username: " << entry.username << ", Password: " << entry.password << endl; } } void searchEntry(const string& service) { for (const auto& entry : entries) { if (entry.service == service) { cout << "Service: " << entry.service << ", Username: " << entry.username << ", Password: " << entry.password << endl; return; } } cout << "No entry found for the service: " << service << endl; } }; int main() { PasswordKeychain keychain; int choice; do { cout << "\nPassword Keychain Menu:" << endl; cout << "1. Add Entry" << endl; cout << "2. View Entries" << endl; cout << "3. Search Entry" << endl; cout << "4. Exit" << endl; cout << "Choose an option: "; cin >> choice; switch (choice) { case 1: { string service, username, password; cout << "Enter service name: ";

cin >> service; cout << "Enter username: "; cin >> username; cout << "Enter password: "; cin >> password; keychain.addEntry(service, username, password); break; } case 2: keychain.viewEntries(); break; case 3: { string service; cout << "Enter service name to search: "; cin >> service; keychain.searchEntry(service); break; } case 4: break; default: cout << "Exiting the program." << endl; cout << "Invalid option. Please try again." << endl; } } while (choice != 4); return 0; }