This repository has been archived by the owner on Dec 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
PasswordGenerator.cpp
62 lines (46 loc) · 1.67 KB
/
PasswordGenerator.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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
string generatePassword(int length, bool useLower, bool useUpper, bool useNumbers, bool useSpecial) {
const string lowercase = "abcdefghijklmnopqrstuvwxyz";
const string uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string numbers = "0123456789";
const string specialChars = "!@#$%^&*()_-+=<>?";
string characterSet = "";
if (useLower) characterSet += lowercase;
if (useUpper) characterSet += uppercase;
if (useNumbers) characterSet += numbers;
if (useSpecial) characterSet += specialChars;
string password;
int charSetSize = characterSet.size();
srand(time(0));
for (int i = 0; i < length; i++) {
int randomIndex = rand() % charSetSize;
password += characterSet[randomIndex];
}
return password;
}
int main() {
int length;
bool useLower, useUpper, useNumbers, useSpecial;
cout << "Welcome to the Cool Password Generator" << endl;
cout << "Password Length: ";
cin >> length;
cout << "Include lowercase letters? (1/0): ";
cin >> useLower;
cout << "Include uppercase letters? (1/0): ";
cin >> useUpper;
cout << "Include numbers? (1/0): ";
cin >> useNumbers;
cout << "Include special characters? (1/0): ";
cin >> useSpecial;
if (useLower || useUpper || useNumbers || useSpecial) {
string password = generatePassword(length, useLower, useUpper, useNumbers, useSpecial);
cout << "Your generated password is: " << password << endl;
} else {
cout << "At least one character set must be included in the password." << endl;
}
return 0;
}