Please help with writing this code. The language is C++. I\'m using vim. Thanks
ID: 3593659 • Letter: P
Question
Please help with writing this code. The language is C++. I'm using vim. Thanks
Problem Statement A good password has many requirements. Humans have a hard time meeting these requirements left to their own devices. You are tasked with creating a program that will generate a password based on what the user wants in the password The user should be able to choose if they want a password with: letters upper case "lower case *numbers The user should also provide the length of the password and how much of the password should be comprised of their selections. You can generate in order or out of order (see below). For example: Welcome to Password Creator! How long do you want the password to be? 10 Do you want letters (0-no, 1-yes)? 1 Do you want uppercase letters (0-no, 1-yes)? 1 How many characters of the password should be uppercase? 2 Do you want lowercase letters (0-no, 1-yes)? 1 How many characters of the password should be lowercase? 4 Do you want numbers (0-no, 1-yes)? 1 How many characters of the password should be numbers? 4 Your random password is: ACbzdf1254 or AtCde 1z254 (either would be acceptable passwords) Would you like to create another password (0-no, 1-yes)? 0Explanation / Answer
Code:
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
string randomString(int length, string charIndex){
int ri[length];
for (int i = 0; i < length; ++i){
ri[i] = rand() % charIndex.length();
}
string rs = "";
for (int i = 0; i < length; ++i){
rs += charIndex[ri[i]];
}
if (rs.empty()) randomString(length, charIndex);
else return rs;
}
int main()
{
srand(time(NULL));
int length;
int lettersConfirm;
int uppercaseLettersConfirm;
int lowercaseLettersConfirm;
int numbersConfirm;
string finalString="";
cout << "How long do you want the password be: ";
cin >> length;
cout << "Do you want letters (0-No, 1-Yes)? ";
cin >> lettersConfirm;
if(lettersConfirm == 1){
cout << "Do you want uppercase letters (0-No, 1-Yes)? ";
cin >> uppercaseLettersConfirm;
if(uppercaseLettersConfirm == 1){
cout << "How many characters should be upper case?";
int uppercaseCharNum;
cin >> uppercaseCharNum;
finalString += randomString(uppercaseCharNum,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}
cout << "Do you want lowercase letters (0-No, 1-Yes)? ";
cin >> lowercaseLettersConfirm;
if(lowercaseLettersConfirm == 1){
cout << "How many characters should be lower case?";
int lowercaseCharNum;
cin >> lowercaseCharNum;
finalString += randomString(lowercaseCharNum,"abcdefghijklmnopqrstuvwxyz");
}
}
cout << "Do you want numbers (0-No, 1-Yes)? ";
cin >> numbersConfirm;
if(numbersConfirm == 1){
cout << "How many characters should be number?";
int numbersCharNum;
cin >> numbersCharNum;
finalString += randomString(numbersCharNum,"0123456789");
}
cout << "Your random password is: " << finalString << endl;
}
Output: