-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpasswordManager.cpp
72 lines (66 loc) · 2.33 KB
/
passwordManager.cpp
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
#include<iostream>
#include<fstream>
#include<limits>
void displayMessage(const std::string& msg){
std::cout<< msg <<std::endl;
}
void displayMenu() {
displayMessage("\nPassword Manager");
displayMessage(" 1. Add Password");
displayMessage(" 2. View Passwords");
displayMessage(" 3. Exit");
displayMessage("Enter your choice: ");
}
void SavePassword(const std::string& site, const std::string& password){
std::ofstream file("passwords.txt", std::ios::app);//append
if (file.is_open()) { // best practice is to test if the file was correctly opened before using
file << site << " " << password << "\n"; // inserts strings into file using space in between to read later
file.close(); // not required, once out of scope the file closes, but its best practice to manually close it
displayMessage("Password saved successfully!");
} else { // if there was an error opening file, we handle it here
std::cerr << "Error: Unable to open file for writing.\n";
}
}
void LoadPasswords() {
std::ifstream file("passwords.txt");
if (file.is_open()) {
std::string site, password;
displayMessage("\nSaved Passwords:");
while (file >> site >> password) {//takes out data from file
std::cout << " Site: " << site << ", Password: " << password << "\n";
}
file.close();
} else {
std::cerr << "\n[System] No passwords saved yet.\n";
}
}
int main(){
int choice;
std::string site, password;
do{
displayMenu();
std::cin >> choice;
if (std::cin.fail()) {
std::cin.clear(); // clears any fail flags
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //
}
switch (choice) {
case 1:
displayMessage("Enter site name: ");
std::cin >> site;
displayMessage("Enter password: ");
std::cin >> password;
SavePassword(site, password);
break;
case 2:
LoadPasswords();
break;
case 3:
displayMessage("\n[System] Exiting...");
break;
default:
displayMessage("\n[System] Invalid choice. Please try again.");
}
} while(choice != 3);
return 0;
}