C++ Random Password Generator

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
7
down vote

favorite
2












I've created this C++ random password generator. You can set length, you can enable custom symbols. Then I went to this website: http://www.passwordmeter.com/ and started checking some generated passwords of mine.



Based on the formula here, I've created a simplified version of their grading system, and also created a function for it. Now I can just do password_score(generated_password) and set a limit for the security of my password.



Here are some examples and how my program performs:



###
Generated password strength: 100/100
Password has been copied to clipboard!
### 1a,pA!T0c0&7 (PasswordMeter score: 100%)

###
Generated password strength: 100/100
Password has been copied to clipboard!
### 82F^Vh11Gl}1 (PasswordMeter score: 100%)

###
Generated password strength: 98/100
Password has been copied to clipboard!
### !3V44'w1 (PasswordMeter score: 100%)

###
Generated password strength: 64/100
Password has been copied to clipboard!
### $]1V9q (PasswordMeter score: 70%)


Am I doing it right? I know that there's always some room for improvement. I'll take any advice to make it better. Thanks!



string password_generator(const int length_of_password = 12, bool enable_symbols = false, bool copy_to_clipboard = false)

vector<char> password;
srand (static_cast<unsigned int>(time(nullptr)));

//generates lowercase letters
for(auto c = 1; c <= length_of_password; c = c + 4)

const auto v1 = rand() % 26;
password.push_back(v1 + 'a');


//generates uppercase letters
for(auto g = 3; g <= length_of_password; g = g + 4)

const auto v2 = rand() % 26;
password.push_back(v2 + 'A');


//generates numbers
for(auto k = 0; k <= length_of_password; k = k + 2)

const auto v3 = rand() % 10;
password.push_back(v3 + '0');


if(enable_symbols)

//generates symbols
for(auto g = 1; g <= length_of_password; g = g + 4)

const auto choice = rand() % 3;

if(choice == 0)

const auto v4 = rand() % 14;
password.push_back(v4 + '!');


if(choice == 1)

const auto v5 = rand() % 5;
password.push_back(v5 + '[');


if(choice == 2)

const auto v6 = rand() % 4;
password.push_back(v6 + '');




random_device r;
shuffle(password.begin(), password.end(), default_random_engine(r()));

string returning_password;

for(auto i = 0; i < length_of_password; i++)

returning_password.push_back(password[i]);


if(copy_to_clipboard)
to_clipboard(returning_password);

return returning_password;



Edit:



Here's a main file: (I've just started learning to work with argvs to there might be some mistakes.) (Most of the functions in this program are something I've created -made easier-, they're custom. I strongly recommend checking bottom GitHub link first.)



#include <duman.h>
using namespace std;

int main(const int argc, char* argv)

cerr << "###n";
if(argc > 1 && argc <= 2)
string(argv[1]) == "-l")

cerr << "Try to use the command as:n";
cerr << get_file_name(argv[0]) << " -8 -98 # for a password that has length of 8 and security score of minimum 98n";
cerr << "###n";
return 2;


if(argc > 2 && argc <= 3)
string(argv[2]).empty())

cerr << "Missing parameters! Use -h to see how to use this program!n";
cerr << "###n";
return 1;

raw_argument1.erase(raw_argument1.begin());
raw_argument2.erase(raw_argument2.begin());
const auto first_argument = stoi(raw_argument1);
const auto second_argument = stoi(raw_argument2);
auto seconds_since_start = 0;

const auto start = time(nullptr);
auto generated_password = password_generator(first_argument, true, true);

while(password_score(generated_password) < second_argument)

if(password_score(generated_password) >= second_argument)

break;

generated_password = password_generator(first_argument, true, true);
seconds_since_start += static_cast<int>(difftime(time(nullptr), start));
if(seconds_since_start > 50)

cerr << "Request timed out. Couldn't generate a password with the given parameters.n";
cerr << "###n";
return 1;


cerr << "Generated password strength: " << password_score(generated_password) << "/100n";
cerr << "Password has been copied to clipboard!n";
cerr << "###n";
return 0;

if(argc > 3)

cerr << "Unsupported number of parameters!n";
return 1;

cerr << "Commands:n";
cerr << "1. " << get_file_name(argv[0]) << " -length -security_level # Security level is XYZ out of 100n";
cerr << "2. " << get_file_name(argv[0]) << " -hn";
cerr << "3. " << get_file_name(argv[0]) << " -vn";
cerr << "###n";



duman.h is a header file I've created for myself. So, whenever I solve a problem in someway I turn it into a function and save it in that header. You can reach it here: https://github.com/tkduman/duman.h



As I've written in readme, the solutions inside of that file are not the greatest. Pardon for my mistakes already.







share1 (PasswordMeter score: 100%)

###
Generated password strength: 98/100
Password has been copied to clipboard!
### !3V44'w1 (PasswordMeter score: 100%)

###
Generated password strength: 64/100
Password has been copied to clipboard!
### $]1V9q (PasswordMeter score: 70%)


Am I doing it right? I know that there's always some room for improvement. I'll take any advice to make it better. Thanks!



string password_generator(const int length_of_password = 12, bool enable_symbols = false, bool copy_to_clipboard = false)

vector<char> password;
srand (static_cast<unsigned int>(time(nullptr)));

//generates lowercase letters
for(auto c = 1; c <= length_of_password; c = c + 4)

const auto v1 = rand() % 26;
password.push_back(v1 + 'a');


//generates uppercase letters
for(auto g = 3; g <= length_of_password; g = g + 4)

const auto v2 = rand() % 26;
password.push_back(v2 + 'A');


//generates numbers
for(auto k = 0; k <= length_of_password; k = k + 2)

const auto v3 = rand() % 10;
password.push_back(v3 + '0');


if(enable_symbols)

//generates symbols
for(auto g = 1; g <= length_of_password; g = g + 4)

const auto choice = rand() % 3;

if(choice == 0)

const auto v4 = rand() % 14;
password.push_back(v4 + '!');


if(choice == 1)

const auto v5 = rand() % 5;
password.push_back(v5 + '[');


if(choice == 2)

const auto v6 = rand() % 4;
password.push_back(v6 + '');




random_device r;
shuffle(password.begin(), password.end(), default_random_engine(r()));

string returning_password;

for(auto i = 0; i < length_of_password; i++)

returning_password.push_back(password[i]);


if(copy_to_clipboard)
to_clipboard(returning_password);

return returning_password;



Edit:



Here's a main file: (I've just started learning to work with argvs to there might be some mistakes.) (Most of the functions in this program are something I've created -made easier-, they're custom. I strongly recommend checking bottom GitHub link first.)



#include <duman.h>
using namespace std;

int main(const int argc, char* argv)

cerr << "###n";
if(argc > 1 && argc <= 2)
string(argv[1]) == "-l")

cerr << "Try to use the command as:n";
cerr << get_file_name(argv[0]) << " -8 -98 # for a password that has length of 8 and security score of minimum 98n";
cerr << "###n";
return 2;


if(argc > 2 && argc <= 3)
string(argv[2]).empty())

cerr << "Missing parameters! Use -h to see how to use this program!n";
cerr << "###n";
return 1;

raw_argument1.erase(raw_argument1.begin());
raw_argument2.erase(raw_argument2.begin());
const auto first_argument = stoi(raw_argument1);
const auto second_argument = stoi(raw_argument2);
auto seconds_since_start = 0;

const auto start = time(nullptr);
auto generated_password = password_generator(first_argument, true, true);

while(password_score(generated_password) < second_argument)

if(password_score(generated_password) >= second_argument)

break;

generated_password = password_generator(first_argument, true, true);
seconds_since_start += static_cast<int>(difftime(time(nullptr), start));
if(seconds_since_start > 50)

cerr << "Request timed out. Couldn't generate a password with the given parameters.n";
cerr << "###n";
return 1;


cerr << "Generated password strength: " << password_score(generated_password) << "/100n";
cerr << "Password has been copied to clipboard!n";
cerr << "###n";
return 0;

if(argc > 3)

cerr << "Unsupported number of parameters!n";
return 1;

cerr << "Commands:n";
cerr << "1. " << get_file_name(argv[0]) << " -length -security_level # Security level is XYZ out of 100n";
cerr << "2. " << get_file_name(argv[0]) << " -hn";
cerr << "3. " << get_file_name(argv[0]) << " -vn";
cerr << "###n";



duman.h is a header file I've created for myself. So, whenever I solve a problem in someway I turn it into a function and save it in that header. You can reach it here: https://github.com/tkduman/duman.h



As I've written in readme, the solutions inside of that file are not the greatest. Pardon for my mistakes already.







share1 (PasswordMeter score: 100%)

###
Generated password strength: 98/100
Password has been copied to clipboard!
### !3V44'w1 (PasswordMeter score: 100%)

###
Generated password strength: 64/100
Password has been copied to clipboard!
### $]1V9q (PasswordMeter score: 70%)


Am I doing it right? I know that there's always some room for improvement. I'll take any advice to make it better. Thanks!



string password_generator(const int length_of_password = 12, bool enable_symbols = false, bool copy_to_clipboard = false)

vector<char> password;
srand (static_cast<unsigned int>(time(nullptr)));

//generates lowercase letters
for(auto c = 1; c <= length_of_password; c = c + 4)

const auto v1 = rand() % 26;
password.push_back(v1 + 'a');


//generates uppercase letters
for(auto g = 3; g <= length_of_password; g = g + 4)

const auto v2 = rand() % 26;
password.push_back(v2 + 'A');


//generates numbers
for(auto k = 0; k <= length_of_password; k = k + 2)

const auto v3 = rand() % 10;
password.push_back(v3 + '0');


if(enable_symbols)

//generates symbols
for(auto g = 1; g <= length_of_password; g = g + 4)

const auto choice = rand() % 3;

if(choice == 0)

const auto v4 = rand() % 14;
password.push_back(v4 + '!');


if(choice == 1)

const auto v5 = rand() % 5;
password.push_back(v5 + '[');


if(choice == 2)

const auto v6 = rand() % 4;
password.push_back(v6 + '');




random_device r;
shuffle(password.begin(), password.end(), default_random_engine(r()));

string returning_password;

for(auto i = 0; i < length_of_password; i++)

returning_password.push_back(password[i]);


if(copy_to_clipboard)
to_clipboard(returning_password);

return returning_password;



Edit:



Here's a main file: (I've just started learning to work with argvs to there might be some mistakes.) (Most of the functions in this program are something I've created -made easier-, they're custom. I strongly recommend checking bottom GitHub link first.)



#include <duman.h>
using namespace std;

int main(const int argc, char* argv)

cerr << "###n";
if(argc > 1 && argc <= 2)
string(argv[1]) == "-l")

cerr << "Try to use the command as:n";
cerr << get_file_name(argv[0]) << " -8 -98 # for a password that has length of 8 and security score of minimum 98n";
cerr << "###n";
return 2;


if(argc > 2 && argc <= 3)
string(argv[2]).empty())

cerr << "Missing parameters! Use -h to see how to use this program!n";
cerr << "###n";
return 1;

raw_argument1.erase(raw_argument1.begin());
raw_argument2.erase(raw_argument2.begin());
const auto first_argument = stoi(raw_argument1);
const auto second_argument = stoi(raw_argument2);
auto seconds_since_start = 0;

const auto start = time(nullptr);
auto generated_password = password_generator(first_argument, true, true);

while(password_score(generated_password) < second_argument)

if(password_score(generated_password) >= second_argument)

break;

generated_password = password_generator(first_argument, true, true);
seconds_since_start += static_cast<int>(difftime(time(nullptr), start));
if(seconds_since_start > 50)

cerr << "Request timed out. Couldn't generate a password with the given parameters.n";
cerr << "###n";
return 1;


cerr << "Generated password strength: " << password_score(generated_password) << "/100n";
cerr << "Password has been copied to clipboard!n";
cerr << "###n";
return 0;

if(argc > 3)

cerr << "Unsupported number of parameters!n";
return 1;

cerr << "Commands:n";
cerr << "1. " << get_file_name(argv[0]) << " -length -security_level # Security level is XYZ out of 100n";
cerr << "2. " << get_file_name(argv[0]) << " -hn";
cerr << "3. " << get_file_name(argv[0]) << " -vn";
cerr << "###n";



duman.h is a header file I've created for myself. So, whenever I solve a problem in someway I turn it into a function and save it in that header. You can reach it here: https://github.com/tkduman/duman.h



As I've written in readme, the solutions inside of that file are not the greatest. Pardon for my mistakes already.







share1 (PasswordMeter score: 100%)

###
Generated password strength: 98/100
Password has been copied to clipboard!
### !3V44'w1 (PasswordMeter score: 100%)

###
Generated password strength: 64/100
Password has been copied to clipboard!
### $]1V9q (PasswordMeter score: 70%)


Am I doing it right? I know that there's always some room for improvement. I'll take any advice to make it better. Thanks!



string password_generator(const int length_of_password = 12, bool enable_symbols = false, bool copy_to_clipboard = false)
{
vector<char> password;
srand (static_cast<unsigned int>(time(nullptr)));

//generates lowercase letters
for(auto c = 1; c <= length_of_password; c = c + 4)

const auto v1 = rand() % 26;
password.push_back(v1 + 'a');


//generates uppercase letters
for(auto g = 3; g <= length_of_password; g = g + 4)

const auto v2 = rand() % 26;
password.push_back(v2 + 'A');


//generates numbers
for(auto k = 0; k <= length_of_password; k = k + 2)

const auto v3 = rand() % 10;
password.push_back(v3 + '0');


if(enable_symbols)

//generates symbols
for(auto g = 1; g <= length_of_password; g = g + 4)

const auto choice = rand() % 3;

if(choice == 0)

const auto v4 = rand() % 14;
password.push_back(v4 + '!');


if(choice == 1)

const auto v5 = rand() % 5;
password.push_back(v5 + '[');


if(choice == 2)

const auto v6 = rand() % 4;
password.push_back(v6 + '');




random_device r;
shuffle(password.begin(), password.end(), default_random_engine(r()));

string returning_password;

for(auto i = 0; i < length_of_password; i++)

returning_password.push_back(password[i]);


if(copy_to_clipboard)
to_clipboard(returning_password);

return returning_password;



Edit:



Here's a main file: (I've just started learning to work with argvs to there might be some mistakes.) (Most of the functions in this program are something I've created -made easier-, they're custom. I strongly recommend checking bottom GitHub link first.)



#include <duman.h>
using namespace std;

int main(const int argc, char* argv)

cerr << "###n";
if(argc > 1 && argc <= 2)
string(argv[1]) == "-l")

cerr << "Try to use the command as:n";
cerr << get_file_name(argv[0]) << " -8 -98 # for a password that has length of 8 and security score of minimum 98n";
cerr << "###n";
return 2;


if(argc > 2 && argc <= 3)
string(argv[2]).empty())

cerr << "Missing parameters! Use -h to see how to use this program!n";
cerr << "###n";
return 1;

raw_argument1.erase(raw_argument1.begin());
raw_argument2.erase(raw_argument2.begin());
const auto first_argument = stoi(raw_argument1);
const auto second_argument = stoi(raw_argument2);
auto seconds_since_start = 0;

const auto start = time(nullptr);
auto generated_password = password_generator(first_argument, true, true);

while(password_score(generated_password) < second_argument)

if(password_score(generated_password) >= second_argument)

break;

generated_password = password_generator(first_argument, true, true);
seconds_since_start += static_cast<int>(difftime(time(nullptr), start));
if(seconds_since_start > 50)

cerr << "Request timed out. Couldn't generate a password with the given parameters.n";
cerr << "###n";
return 1;


cerr << "Generated password strength: " << password_score(generated_password) << "/100n";
cerr << "Password has been copied to clipboard!n";
cerr << "###n";
return 0;

if(argc > 3)

cerr << "Unsupported number of parameters!n";
return 1;

cerr << "Commands:n";
cerr << "1. " << get_file_name(argv[0]) << " -length -security_level # Security level is XYZ out of 100n";
cerr << "2. " << get_file_name(argv[0]) << " -hn";
cerr << "3. " << get_file_name(argv[0]) << " -vn";
cerr << "###n";



duman.h is a header file I've created for myself. So, whenever I solve a problem in someway I turn it into a function and save it in that header. You can reach it here: https://github.com/tkduman/duman.h



As I've written in readme, the solutions inside of that file are not the greatest. Pardon for my mistakes already.









share|improve this question












share|improve this question




share|improve this question








edited Jan 5 at 16:27
























asked Jan 5 at 16:09









Tuğberk Kaan Duman

1384




1384







  • 1




    Would you please be so kind to extend your code to a compilable and runnable program? You're lacking a lot of things, e.g. includes, a main-function etc.
    – Ben Steffan
    Jan 5 at 16:19










  • @BenSteffan sorry, I'm new here. In stackoverflow if I post long code people get mad. Alright will fix it now.
    – Tuğberk Kaan Duman
    Jan 5 at 16:20










  • I believe, it's fixed now.
    – Tuğberk Kaan Duman
    Jan 5 at 16:27












  • 1




    Would you please be so kind to extend your code to a compilable and runnable program? You're lacking a lot of things, e.g. includes, a main-function etc.
    – Ben Steffan
    Jan 5 at 16:19










  • @BenSteffan sorry, I'm new here. In stackoverflow if I post long code people get mad. Alright will fix it now.
    – Tuğberk Kaan Duman
    Jan 5 at 16:20










  • I believe, it's fixed now.
    – Tuğberk Kaan Duman
    Jan 5 at 16:27







1




1




Would you please be so kind to extend your code to a compilable and runnable program? You're lacking a lot of things, e.g. includes, a main-function etc.
– Ben Steffan
Jan 5 at 16:19




Would you please be so kind to extend your code to a compilable and runnable program? You're lacking a lot of things, e.g. includes, a main-function etc.
– Ben Steffan
Jan 5 at 16:19












@BenSteffan sorry, I'm new here. In stackoverflow if I post long code people get mad. Alright will fix it now.
– Tuğberk Kaan Duman
Jan 5 at 16:20




@BenSteffan sorry, I'm new here. In stackoverflow if I post long code people get mad. Alright will fix it now.
– Tuğberk Kaan Duman
Jan 5 at 16:20












I believe, it's fixed now.
– Tuğberk Kaan Duman
Jan 5 at 16:27




I believe, it's fixed now.
– Tuğberk Kaan Duman
Jan 5 at 16:27










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Advice 1



The most important issue with your password generator function is the fact that you keep creating the character vector over and over again. I suggest you roll a class that has that character vector as a field and construct it only once:



class password_generator 
public:
password_generator()
... // Construct your alphabet.


private:
std::vector<char> m_alphabet;
;


Advice 2



if(choice == 0)

...


if(choice == 1)

...



Just use switch here:



switch (rand() % 2) 
case 0:
...
case 1:
...
case 2:
...



Advice 3



#include "stdafx.h": In the build settings of Visual Studio, search something like "Use precompiled headers"; set to No. This will remove the need for including that header file.



Advice 4



#include <duman.h>: this won't compile on Xcode. The convention is that you use <header> for standard C++ library headers, and "header.h" for your own header files.



Advice 5



using namespace std; This one is a poor practice since it abuses your scope with bunch of identifiers/type names. Use instead:



#include "funky.hpp"
using funky::person;
using funky::darth_vader;


Advice 6



In your main driver you output to cerr. This is not what is expected of a command line program. *nix like OS'es has two "handles" one for standard output and one for standard error stream. Your conventional *nix guru will expect normal output to cout and only error stuff to cerr.






share|improve this answer





















  • 1. Great suggestion! Will do now. 2. Is it better performance wise? 3. That thing was annoying thanks. 4. Oh... didn't know that. I just put the file inside INCLUDE folders of Visual Studio and using it from there. 5. Well... alright. 6. I thought since cerr doesn't have any buffers it could give faster output. Obviously cout is there to "out"put.
    – Tuğberk Kaan Duman
    Jan 5 at 20:36










  • @TuğberkKaanDuman 2.: Most definitely. When there is a match, other two values will not be tested. Also, I believe that for very large switch statement, the compiler may perform binary search over case values.
    – coderodde
    Jan 5 at 20:53










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184374%2fc-random-password-generator%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Advice 1



The most important issue with your password generator function is the fact that you keep creating the character vector over and over again. I suggest you roll a class that has that character vector as a field and construct it only once:



class password_generator 
public:
password_generator()
... // Construct your alphabet.


private:
std::vector<char> m_alphabet;
;


Advice 2



if(choice == 0)

...


if(choice == 1)

...



Just use switch here:



switch (rand() % 2) 
case 0:
...
case 1:
...
case 2:
...



Advice 3



#include "stdafx.h": In the build settings of Visual Studio, search something like "Use precompiled headers"; set to No. This will remove the need for including that header file.



Advice 4



#include <duman.h>: this won't compile on Xcode. The convention is that you use <header> for standard C++ library headers, and "header.h" for your own header files.



Advice 5



using namespace std; This one is a poor practice since it abuses your scope with bunch of identifiers/type names. Use instead:



#include "funky.hpp"
using funky::person;
using funky::darth_vader;


Advice 6



In your main driver you output to cerr. This is not what is expected of a command line program. *nix like OS'es has two "handles" one for standard output and one for standard error stream. Your conventional *nix guru will expect normal output to cout and only error stuff to cerr.






share|improve this answer





















  • 1. Great suggestion! Will do now. 2. Is it better performance wise? 3. That thing was annoying thanks. 4. Oh... didn't know that. I just put the file inside INCLUDE folders of Visual Studio and using it from there. 5. Well... alright. 6. I thought since cerr doesn't have any buffers it could give faster output. Obviously cout is there to "out"put.
    – Tuğberk Kaan Duman
    Jan 5 at 20:36










  • @TuğberkKaanDuman 2.: Most definitely. When there is a match, other two values will not be tested. Also, I believe that for very large switch statement, the compiler may perform binary search over case values.
    – coderodde
    Jan 5 at 20:53














up vote
2
down vote



accepted










Advice 1



The most important issue with your password generator function is the fact that you keep creating the character vector over and over again. I suggest you roll a class that has that character vector as a field and construct it only once:



class password_generator 
public:
password_generator()
... // Construct your alphabet.


private:
std::vector<char> m_alphabet;
;


Advice 2



if(choice == 0)

...


if(choice == 1)

...



Just use switch here:



switch (rand() % 2) 
case 0:
...
case 1:
...
case 2:
...



Advice 3



#include "stdafx.h": In the build settings of Visual Studio, search something like "Use precompiled headers"; set to No. This will remove the need for including that header file.



Advice 4



#include <duman.h>: this won't compile on Xcode. The convention is that you use <header> for standard C++ library headers, and "header.h" for your own header files.



Advice 5



using namespace std; This one is a poor practice since it abuses your scope with bunch of identifiers/type names. Use instead:



#include "funky.hpp"
using funky::person;
using funky::darth_vader;


Advice 6



In your main driver you output to cerr. This is not what is expected of a command line program. *nix like OS'es has two "handles" one for standard output and one for standard error stream. Your conventional *nix guru will expect normal output to cout and only error stuff to cerr.






share|improve this answer





















  • 1. Great suggestion! Will do now. 2. Is it better performance wise? 3. That thing was annoying thanks. 4. Oh... didn't know that. I just put the file inside INCLUDE folders of Visual Studio and using it from there. 5. Well... alright. 6. I thought since cerr doesn't have any buffers it could give faster output. Obviously cout is there to "out"put.
    – Tuğberk Kaan Duman
    Jan 5 at 20:36










  • @TuğberkKaanDuman 2.: Most definitely. When there is a match, other two values will not be tested. Also, I believe that for very large switch statement, the compiler may perform binary search over case values.
    – coderodde
    Jan 5 at 20:53












up vote
2
down vote



accepted







up vote
2
down vote



accepted






Advice 1



The most important issue with your password generator function is the fact that you keep creating the character vector over and over again. I suggest you roll a class that has that character vector as a field and construct it only once:



class password_generator 
public:
password_generator()
... // Construct your alphabet.


private:
std::vector<char> m_alphabet;
;


Advice 2



if(choice == 0)

...


if(choice == 1)

...



Just use switch here:



switch (rand() % 2) 
case 0:
...
case 1:
...
case 2:
...



Advice 3



#include "stdafx.h": In the build settings of Visual Studio, search something like "Use precompiled headers"; set to No. This will remove the need for including that header file.



Advice 4



#include <duman.h>: this won't compile on Xcode. The convention is that you use <header> for standard C++ library headers, and "header.h" for your own header files.



Advice 5



using namespace std; This one is a poor practice since it abuses your scope with bunch of identifiers/type names. Use instead:



#include "funky.hpp"
using funky::person;
using funky::darth_vader;


Advice 6



In your main driver you output to cerr. This is not what is expected of a command line program. *nix like OS'es has two "handles" one for standard output and one for standard error stream. Your conventional *nix guru will expect normal output to cout and only error stuff to cerr.






share|improve this answer













Advice 1



The most important issue with your password generator function is the fact that you keep creating the character vector over and over again. I suggest you roll a class that has that character vector as a field and construct it only once:



class password_generator 
public:
password_generator()
... // Construct your alphabet.


private:
std::vector<char> m_alphabet;
;


Advice 2



if(choice == 0)

...


if(choice == 1)

...



Just use switch here:



switch (rand() % 2) 
case 0:
...
case 1:
...
case 2:
...



Advice 3



#include "stdafx.h": In the build settings of Visual Studio, search something like "Use precompiled headers"; set to No. This will remove the need for including that header file.



Advice 4



#include <duman.h>: this won't compile on Xcode. The convention is that you use <header> for standard C++ library headers, and "header.h" for your own header files.



Advice 5



using namespace std; This one is a poor practice since it abuses your scope with bunch of identifiers/type names. Use instead:



#include "funky.hpp"
using funky::person;
using funky::darth_vader;


Advice 6



In your main driver you output to cerr. This is not what is expected of a command line program. *nix like OS'es has two "handles" one for standard output and one for standard error stream. Your conventional *nix guru will expect normal output to cout and only error stuff to cerr.







share|improve this answer













share|improve this answer



share|improve this answer











answered Jan 5 at 20:28









coderodde

15.5k533114




15.5k533114











  • 1. Great suggestion! Will do now. 2. Is it better performance wise? 3. That thing was annoying thanks. 4. Oh... didn't know that. I just put the file inside INCLUDE folders of Visual Studio and using it from there. 5. Well... alright. 6. I thought since cerr doesn't have any buffers it could give faster output. Obviously cout is there to "out"put.
    – Tuğberk Kaan Duman
    Jan 5 at 20:36










  • @TuğberkKaanDuman 2.: Most definitely. When there is a match, other two values will not be tested. Also, I believe that for very large switch statement, the compiler may perform binary search over case values.
    – coderodde
    Jan 5 at 20:53
















  • 1. Great suggestion! Will do now. 2. Is it better performance wise? 3. That thing was annoying thanks. 4. Oh... didn't know that. I just put the file inside INCLUDE folders of Visual Studio and using it from there. 5. Well... alright. 6. I thought since cerr doesn't have any buffers it could give faster output. Obviously cout is there to "out"put.
    – Tuğberk Kaan Duman
    Jan 5 at 20:36










  • @TuğberkKaanDuman 2.: Most definitely. When there is a match, other two values will not be tested. Also, I believe that for very large switch statement, the compiler may perform binary search over case values.
    – coderodde
    Jan 5 at 20:53















1. Great suggestion! Will do now. 2. Is it better performance wise? 3. That thing was annoying thanks. 4. Oh... didn't know that. I just put the file inside INCLUDE folders of Visual Studio and using it from there. 5. Well... alright. 6. I thought since cerr doesn't have any buffers it could give faster output. Obviously cout is there to "out"put.
– Tuğberk Kaan Duman
Jan 5 at 20:36




1. Great suggestion! Will do now. 2. Is it better performance wise? 3. That thing was annoying thanks. 4. Oh... didn't know that. I just put the file inside INCLUDE folders of Visual Studio and using it from there. 5. Well... alright. 6. I thought since cerr doesn't have any buffers it could give faster output. Obviously cout is there to "out"put.
– Tuğberk Kaan Duman
Jan 5 at 20:36












@TuğberkKaanDuman 2.: Most definitely. When there is a match, other two values will not be tested. Also, I believe that for very large switch statement, the compiler may perform binary search over case values.
– coderodde
Jan 5 at 20:53




@TuğberkKaanDuman 2.: Most definitely. When there is a match, other two values will not be tested. Also, I believe that for very large switch statement, the compiler may perform binary search over case values.
– coderodde
Jan 5 at 20:53












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184374%2fc-random-password-generator%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation