#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
// Instructions:
// 1. Read through the program and run it to make sure you understand what it's
// doing. This is a modified version of the code we went over last week, so
// the code works slightly differently.
// 2. Implement a new function called generatePassword, which should generate
// a password of a given length by sampling from a given string of
// characters
// - think about what parameters this function will need to do its work
// - think about what it should return, if anything
// - think about what what code from main should be incorporated into the
// function (hint: it's 4 lines of code)
// 3. Replace the password generating code with an invocation of your new
// function
// 4. Test your program to make sure it works as expected.
int main(){
string charsToInclude;
vector<string> passwords;
vector<string> usernames;
int passwordLength;
string input;
srand(time(0));
cout << "Please enter a string containing all of the characters "
<< "you'd like to sample passwords from: ";
getline(cin, charsToInclude);
cout << "How long should the password be? ";
cin >> passwordLength;
cin.ignore();
cout << "Enter usernames; use -1 to stop" << endl;
// Read in the usernames.
do {
cout << "> ";
// Read in the username.
getline(cin, input);
if(input != "-1"){
// Add the username to the end of the vector of usernames.
usernames.push_back(input);
// Add a blank password for the user -- we'll update this later.
passwords.push_back("");
}
} while(input != "-1");
cout << usernames.size() << " usernames were successfully read in." << endl;
// Generate one password per user.
for(int i = 0; i < passwords.size(); i++){
string password = "";
for(int j = 0; j < passwordLength; j++){
password.push_back(charsToInclude[rand()%charsToInclude.size()]);
}
passwords.at(i) = password;
}
// Print out each of the username/password pairs.
cout << "username/passwords:" << endl;
for(int i = 0; i < usernames.size(); i++){
cout << usernames.at(i) << "\t" << passwords.at(i) << endl;
}
return 0;
}