Password program errors

I need to write a password program in c++ that has the user make a password and i need to check for the following limitations.
Must be 5-12 characters long.
must contain letters and numbers.
sequences cannot repeat: such as 11, ndnd 123123, etc.
I don't know if i am approaching this the correct way, and i could use some tips.
The problem with the following code is that when i pass in the vector password into the function check_pass, its values turn into strange symbols, can anyone help me out here?
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int get_pass(vector<char> password, int plength);
int check_pass(vector<char> password, int plength);
int main()
{
    int cnt, plength;
    vector<char> password;
    plength=get_pass(password, plength);
    plength=check_pass(password, plength);
    system("pause");
    return 0;
}
int get_pass(vector<char> password, int plength)
{
    int cnt;
    cout<<"Please enter the length of your password"<<endl;
    cin>>plength;
    if((plength<5)||(plength>12))
    {
        cout<<"invalid length"<<endl;
        return -1;
    }
    password.resize(plength);
    cout<<plength<<endl;
    cout<<"Now enter the password you want to use."<<endl;
    for(cnt=0; cnt<plength; cnt++)
        cin>>password[cnt];
    for(cnt=0; cnt<plength; cnt++)
    {
        cout<<password[cnt];
    }
    return plength;      
}
int check_pass(vector<char> password, int plength)
{
    int length=0, cnt;
    for(cnt=0; cnt<plength; cnt++)
    {
        switch (password[cnt])
        {
            case 0:
                 length++;
                 break;
            case 1:
                 length++;
                 break;
            case 2:
                 length++;
                 break;
            case 3:
                 length++;
                 break;
            case 4:
                 length++;
                 break;
            case 5:
                 length++;
                 break;
            case 6:
                 length++;
                 break;
            case 7:
                 length++;
                 break;
            case 8:
                 length++;
                 break;
            case 9:
                 length++;
                 break;
            case 'a':
                length++;
                break;
            case 'b':
                 length++;
                 break;
            case 'c':
                 length++;
                 break;
            case 'd':
                 length++;
                 break;
            case 'e':
                 length++;
                 break;
            case 'f':
                 length++;
                 break;
            case 'g':
                 length++; 
                 break;
            case 'h':
                 length++;
                 break;
            case 'i':
                 length++;
                 break;
            case 'j':
                 length++;
                 break;
            case 'k':
                 length++;
                 break;
            case 'l':
                 length++;
                 break;
            case 'm':
                 length++;
                 break;
            case 'n':
                 length++;
                 break;
            case 'o':
                 length++;
                 break;
            case 'p':
                 length++;
                 break;
            case 'q':
                 length++;
                 break;
            case 'r':
                 length++;
                 break;
            case 's':
                 length++;
                 break;
            case 't':
                 length++;
                 break;
            case 'u':
                 length++;
                 break;
            case 'v':
                 length++;
                 break;
            case 'w':
                 length++;
                 break;
            case 'x':
                 length++;
                 break;
            case 'y':
                 length++;
                 break;
            case 'z':
                 length++;
                 break;
        };
    }
    cout<<"length"<<length<<endl;
    return plength;    
}
Last edited on
Hi infuser21,

vector<char> password; is a local variable, so when you send it to get_pass it will modify the local copy of get_pass and not the main's copy, which probably you expect to change. So if you want to change the main's copy then pass it by reference.

In int get_pass(vector<char> password, int plength) You take plength as input from user and then return it at
plength=get_pass(password, plength); . I don't know why you are passing it in the first place. Can't you make your function just like plength=get_pass(password); and for function int get_pass(vector<char>& password);

I don't understand, why you are using this password.resize(plength); Note: Vector is a dynamic array so you don't need to specify it's size.

In check_pass, I have no idea why you are using 'a', 'b' ... etc. can't you use password.size(); ?

Hope this helps !
Topic archived. No new replies allowed.