Mini Password Generator Problem

Hello. I have created a mini password generator (that only creates passwords 6 - 12 characters long :P) and I need a bit of help on it.

Firstly, when I generate the password and it is put in a text file, it is always the same for some reason. I don't understand why and I need some help lol.

Secondly, I just need help with any bad things I've put (I know i've got a few). Any suggestions would be nice.

Here are my codes:

Main Password Generator Program
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include "randChar.h"
using namespace std;

randomCharacter RC;

int main()
{
    string input = "";
    int length;
    int x = 0;
    int wantedLength;
    char password_array[0];
    cout << "**** PASSWORD GENERATOR ****" << endl;
    cout << "Please enter how long you want your password (6-12 characters)." << endl;
    while (true)
    {
        getline(cin, input);
        stringstream myStream(input);
        if (myStream >> length)
        break;
        cout << "You must've entered something that is not an integer. Please try again." << endl;
    }
    if (length < 6)
    {
        cout << "A password with less than 6 characters! We don't support that. Let's make it 6 characters." << endl;
        wantedLength = 6;
    }
    else if (length > 12)
    {
        cout << "A password is " << length << " characters? That's a bit too many. Let's make it 12 instead." << endl;
        wantedLength = 12;
    }
    else
    {
        cout << "That's good! A password with " << length << " characters." << endl;
        wantedLength = length;
    }
    switch (wantedLength)
    {
        case 6:
        while (x < 6)
        {
            password_array[x] = RC.randChar();
            x++;
        }
        break;
        case 7:
        while (x < 7)
        {
            password_array[x] = RC.randChar();
            x++;
        }
        break;
        case 8:
        while (x < 8)
        {
            password_array[x] = RC.randChar();
            x++;
        }
        break;
        case 9:
        while (x < 9)
        {
            password_array[x] = RC.randChar();
            x++;
        }
        break;
        case 10:
        while (x < 10)
        {
            password_array[x] = RC.randChar();
            x++;
        }
        break;
        case 11:
        while (x < 11)
        {
            password_array[x] = RC.randChar();
            x++;
        }
        break;
        default:
        while (x < 12)
        {
            password_array[x] = RC.randChar();
            x++;
        }
        break;
    }
    cout << "Your password will be created in a text file named 'password.txt'." << endl;
    cout << "Thank you for using **** PASSWORD GENERATOR ****!" << endl;
    remove("password.txt");
    ofstream password_file;
    password_file.open ("password.txt", ios::trunc | ios::in | ios::out);
    if (password_file.is_open())
    {
        password_file << password_array << "\n\n";
        password_file << "That is your password. Please use **** PASSWORD GENERATOR **** again!";
        password_file.close();
        std::cout << "Press ENTER to continue...";
        std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    }
    else
    {
        cout << "Unable to create the text file! Exiting the program.." << endl;
        std::cout << "Press ENTER to continue...";
        std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
        return(0);
    }
}


RANDOM CHARACTER HEADER FILE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <ctime>
class randomCharacter
{
    public:

    unsigned char randChar(void)
    {
            int random_number, answer;
            unsigned char return_value;
            random_number = (int) rand();
            answer = 40 + (random_number % 60);
            return_value = (unsigned char) answer;
            if (return_value <= 57) return return_value;
            return_value + rand() % 22 + 8;
            if (return_value <= 90) return return_value;
            return_value + rand() % 23 + 7;
            if (return_value <= 122) return return_value;
            return_value = 0;
            return return_value;
    }
};


Thanks for any help!
Last edited on
You're declaring an array[0]. Declare it after you know the password length.

int password_array[wantedLength];

Also, try adding srand(time(0)); at the beginning of your code. That might fix the problem where it's generating the same password everytime.


And why not remove the switch block and replace it with

1
2
3
4
while (x < wantedLength) {
      password_array[x] = RC.randChar();
      x++;
}
Last edited on
Thanks for your suggestions and comments. I'll try them out tomorrow (it's late now, at nighttime.)

I have a question thought - why do I need to add srand(time(0)); again? I thought it's enough when it was included in the header file.
You're welcome.

Calling srand(something) sets your random numbers to follow a specifiq sequence everytime you run your program.
So srand(1); will always result in the same sequence, and so will srand(2) etc.

Passing time(0) or time(NULL) as an argument when calling srand() will cause your program to generate a different sequence of numbers everytime, since time() will always return a different value.

But make sure you call srand() only once in your program.

http://www.cplusplus.com/reference/clibrary/cstdlib/srand.html
Last edited on
It worked now!

P.S. Thanks for your alternative method (
1
2
3
4
while (x < wantedLength) {
      password_array[x] = RC.randChar();
      x++;
}
)! I can't believe I'm so stupid that I can't even think of that! That is so convenient I can even do passwords up to lots of characters, lol.

Thanks for your help scythe!
Topic archived. No new replies allowed.