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
|
#include <iostream>
#include <string>
#include <random>
#include <chrono>
#include <algorithm>
#include <iterator>
static const std::vector<char> alphanum {'1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','p','q','r','s','t','u','v','w','x','y','z'};
static const std::vector<char> upperCase{alphanum.begin()+9, alphanum.begin()+34};
constexpr auto passwordSize = 10;
int main()
{
auto seed = std::chrono::system_clock::now().time_since_epoch().count();//seed
std::default_random_engine dre(seed);//engine
std::uniform_int_distribution<int> d_all(0, alphanum.size()-1);//distribution for all characters
std::uniform_int_distribution<int> d_upper(0, upperCase.size()-1);//distribution for uppercase only
std::string password(passwordSize, ' ');//string to hold password
auto it = upperCase.cbegin();
std::advance(it, d_upper(dre));//advance iterator by random number b/w 0 and size of uppercase
password[0] = *it;//choose the value of iterator after random advance as password[0]
std::generate(password.begin()+1, password.end(), [&]{ return alphanum[d_all(dre)];});
//http://en.cppreference.com/w/cpp/algorithm/generate
//generate the rest of the characters for the password string
std::cout << password << "\n";
bool match = false;
while (!match)
{
std::cout << "Now enter the password yourself: \n";
std::string enteredPassword;
getline(std::cin, enteredPassword);
if (enteredPassword == password)
{
std::cout << "Thank you, acces granted \n";
match = true;
}
else
{
std::cout << "Wrong entry, try again \n";
}
}
}
|