Password gets longer on the loop

I can not figure out on how to fix my password generator on a do-while loop. I been trying to solve this for hours now but i can not break it. Everytime i go into the loop the password just gets longer than orignal plan i wanted it to be 10 characters long.

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
 #include <iostream>
#include <string>
#include <ctime>
using namespace std;

static const char alphanum[] =
"0123456789"
"!@#$%^&*()"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";



int main() {


    
    string first_name, last_name, full_name, username, last, genRandom, password;
    char ans;
    int length = 10, stringLength = sizeof(alphanum), rndnum;
    double empty;
    
    srand((unsigned)time(0));
    //seed pseudo random generator
    
    
    
    do {
        cout << "Please enter your first name and last name. ";
        getline(cin, full_name);
        //User input full name
        
        empty = full_name.find(" ");
        first_name = full_name.substr(0, empty);
        //Assigns first name
        
        
        if(empty > 0)
            last_name = full_name.substr(empty + 1);
        //Assigns last name
        
        
        rndnum = rand() % 100;
        last = to_string(rndnum);
        username = first_name[0] + last_name.substr(0, 4) + last;
        //Generates a username using first name and last name then a random two digit number
        
        
        
        for(int i = 0; i < length; i++){
           
            password += alphanum[rand() % stringLength - 1];
            random_shuffle(password.begin(), password.end());
        }
        //Generates a random password
        
        cout << "The program has automatically created a username and password for you.\n"
             << "Your username will be: "
             << username
             << "\nYour password will be: "
             << password;
        
        cout << "\nWould you like to repeat the process?\n"
             << "Please enter [Y/N] : ";
        
        cin >> ans;
        
        cin.ignore(44,'\n');
        //Fixes getline problem when doing do-while loop
        
        
    }
    while (ans == 'Y' || ans =='y');
    
    cout << "The code has ended.\n";
    
    
    return 0;
}
Hello ihtsbryan,

Easy fix. First you are missing the header files:

1
2
#include <algorithm>    // std::random_shuffle
#include <cstdlib>      // std::rand, std::srand 


Just before the close of the do/while loop use either password.clear(); or password = ""; to clear the variable "password" otherwise you continue to add to what you have in the variable.

Hope that helps,

Andy
Hello ihtsbryan,

After some thinking and testing you might want to consider this:

1
2
3
4
5
6
do
{
	rndnum = rand() % 100;
	last = to_string(rndnum);

} while (rndnum < 10);


With your code "rndnum" will become less than 10 at some point and that is not what you want.

Hope that helps,

Andy
Thank you so much Andy for the reply it really helped out. I was stuck trying to figure it out for 12 hours till i decided to post it.
Also if i wanted to be more specific generating the password like it must have 1 uppercase 1 lowercase 1 number and 1 special character how would i go about it?
Hello ihtsbryan,

Digging into your program while thinking a solution to your request I noticed some things that could be done better like:static const char alphanum[] = "static" is not needed here. I believe that a global variable is "static" by default. Also you could just as easily use
const std::string alphanum{ "0123456789!@#$%^&*()ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" }; Then when you define the variable stringLength = sizeof(alphanum) you would write that as stringLength = alphanum.length(). Later on tn the program where you use "stringLength" you could use "alphanum.length()".

In line 52 remove the "- 1" otherwise you will never choose "z" from "alphanum" as you are cutting yourself short one position in the string or the array.

Thinking about the verification of the password my first thought is a do/while loop containing a for loop to check each character of the string. This is where the string would work better than the character array. In the for loop you can use the member functions of string like: if (isdigit(password[i])
http://www.cplusplus.com/reference/cctype/isdigit/
see the window or frame on the left side for other "is" functions. Looks like you will need to include the header file "cctype" to use these functions. Inside the for loop it would look something like this:
1
2
3

if (isdigit(passwork[i])
    digit = true;  //  digit is defined as a bool 


You will need other "if" statements for "isupper", "is lower" etc. The "isgraph" I have not used till now, so I am not sure if it will work for all the special characters. You might have to use "ispunct" to catch any character that "isgraph" does not.

I would define all the bool variables out side the do/while loop and at the start of the do/while I would set all the bool variables to false before you check the "password" string.

After the for loop check all the bool variables, this can be done with one "if" statement. Then if it fails generate a new "password" and start the do/while loop over. If it is true then break out of the do/while and continue on with the program.

With the testing I have done so far this verification was not really needed, but it is a good idea.

Hope that helps,

Andy
Topic archived. No new replies allowed.