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 |
#include <iostream> #include <fstream> #include <string> #include <map> void addPassword(std::map<std::string, std::string>& passwords) { std::string account, password; std::cout << "Enter account name: "; std::cin.ignore(); std::getline(std::cin, account); std::cout << "Enter password: "; std::getline(std::cin, password); passwords[account] = password; std::cout << "Password added successfully.\n"; } void viewPasswords(const std::map<std::string, std::string>& passwords) { if (passwords.empty()) { std::cout << "No passwords stored.\n"; return; } std::cout << "Stored passwords:\n"; for (const auto& entry : passwords) { std::cout << "Account: " << entry.first << " | Password: " << entry.second << '\n'; } } void savePasswords(const std::map<std::string, std::string>& passwords) { std::ofstream file("passwords.txt"); if (!file) { std::cerr << "Error opening file for writing.\n"; return; } for (const auto& entry : passwords) { file << entry.first << '\n' << entry.second << '\n'; } file.close(); std::cout << "Passwords saved successfully.\n"; } void loadPasswords(std::map<std::string, std::string>& passwords) { std::ifstream file("passwords.txt"); if (!file) { std::cerr << "Error opening file for reading.\n"; return; } std::string account, password; while (std::getline(file, account) && std::getline(file, password)) { passwords[account] = password; } file.close(); } int main() { std::map<std::string, std::string> passwords; loadPasswords(passwords); int choice; while (true) { std::cout << "Password Manager\n"; std::cout << "1. Add Password\n"; std::cout << "2. View Passwords\n"; std::cout << "3. Save and Exit\n"; std::cout << "Enter your choice: "; std::cin >> choice; switch (choice) { case 1: addPassword(passwords); break; case 2: viewPasswords(passwords); break; case 3: savePasswords(passwords); return 0; default: std::cout << "Invalid choice. Please try again.\n"; } } return 0; } |
Explanation:
- Include Headers:
#include <iostream>
: For input and output operations.#include <fstream>
: For file operations.#include <string>
: For string manipulations.#include <map>
: For using thestd::map
container to store account-password pairs.
- Functions:
addPassword
: Prompts the user to enter an account name and password, then stores these in thepasswords
map.viewPasswords
: Displays all stored account-password pairs. If no passwords are stored, it informs the user.savePasswords
: Saves the account-password pairs from thepasswords
map to a text file namedpasswords.txt
.loadPasswords
: Loads account-password pairs frompasswords.txt
into thepasswords
map. This allows the program to persist passwords between runs.
Advertisement - Main Function:
- Load Passwords: Calls
loadPasswords
to populate thepasswords
map from the file at the start. - Menu Loop: Displays a menu for the user to choose between adding passwords, viewing passwords, or saving and exiting.
- Choice Handling: Uses a
switch
statement to handle user choices. Calls appropriate functions based on user input.
Advertisement - Load Passwords: Calls
- File Format:
- Passwords are saved in a text file with each account and its corresponding password on separate lines. This format is simple and easy to read/write, but not secure. For better security, consider using encryption.
Compilation and Execution:
Compile the Program:
1 |
g++ -o password_manager password_manager.cpp |
Run the Program:
1 |
./password_manager |