Problems with a password checker

Hi everyone. I'm a beginner at c++ and I have a program due tonight that I've been stuck on for two days now. I have code written and it compiles with no errors or warnings but when I run it, I get an error saying that the string subscript is out of range. I'm going to post the code below.

It is supposed to get input of a password and two security codes. The password must have the first letter capitalized, the next two letters lower case, the fourth character a digit. It must contain a $. It must be an even amount of characters long. It must be within 8-14 characters, and the two security codes have to have different parity. Also, if the user chooses verbose, it must tell what tests the password failed. Sorry it's so long. Any help would be greatly appreciated. Thanks in advance

#include <iostream>
#include <string>

using namespace std;

bool myISdigit(char);
bool myISupper(char);
bool myISlower(char);
int pwTest(string, int, int, bool);
string pwType(int);


int main ()
{
string pw, strength;
char verbose;

int sc1, sc2, length;
int fTests = 0;
cout << "Password Strength Checker!" <<endl<<endl;
cout << "Enter the password to check: ";
getline (cin, pw);
cout <<"Enter the two security codes; ";
cin >> sc1 >> sc2;
cout <<"Verbose? (y/n) ";
cin >> verbose;
cout << "Checking "<< pw <<" "<< sc1 << " "<< sc2;

pwTest (pw, sc1, sc2, verbose);
pwType (fTests);

cout <<"Final result:"<<endl;
cout <<"Your password failed "<<fTests<<" tests."<<endl;
cout << "Your password is a " << strength << " password.";


}

int pwTest (string, int, int, bool)
{
int length, moneylocation, spacelocation;
int sc1 = 0;
int sc2 = 0;
int fTests = 0;
string pw;
char verbose = 'n';


length = pw.length();

if (length < 8 && verbose == 'y')
{
fTests++;
cout <<"The password is too short";
}
else if (length < 8 && verbose == 'n')
{
fTests++;
}
else if (length > 14 && verbose == 'y')
{
fTests++;
cout <<"The password is too long";
}
else if (length > 14 && verbose == 'n')
{
fTests++;
}


if (length % 2 !=0 && verbose == 'y')
{
fTests++;
cout<< "Password length is odd";
}
else if (length % 2 !=0 && verbose == 'n')
{
fTests++;
}

if (myISupper(pw[0]) == false && verbose == 'y')
{
fTests++;
cout<<"The first character is not an upper case letter";
}
if (myISlower(pw[1]) == false && verbose == 'y')
{
fTests++;
cout<<"The second and / or third characters are not lower case letters";
}
else if (myISlower(pw[1]) == false && verbose == 'n')
{
fTests++;
}


if (myISdigit(pw[3]) == false && verbose == 'y')
{
fTests++;
cout<<"The fourth character is not a digit";
}

else if (myISdigit(pw[3]) == false && verbose == 'n')
{
fTests++;
}

moneylocation = pw.find("$");

if (moneylocation == string::npos && verbose == 'y')
{
fTests++;
cout<<"The password does not have a $ in it";
}
else if (moneylocation == string::npos && verbose == 'n')
{
fTests++;
}

spacelocation = pw.find(" ");

if (spacelocation != string::npos && verbose == 'y')
{
fTests++;
cout<<"The password has a space in it";
}
else if (spacelocation != string::npos &&verbose == 'n')
{
fTests++;
}

if (sc1 % 2 == 0 && sc2 %2 == 0 && verbose == 'y')
{
fTests++;
cout<< "Security codes have the same parity";
}
else if (sc1 %2 ==0 && sc2%2 == 0 && verbose == 'n')
{
fTests++;
}

if (sc1 % 2 !=0 && sc2 % 2 != 0 && verbose == 'y')
{
fTests++;
cout<<"Security codes have the same parity";
}
else if (sc1 %2 !=0 && sc2 %2 !=0 && verbose == 'n')
{
fTests++;
}

return fTests;
}


bool myISupper(char)
{
bool upper;
string pw;

if (pw[0] >= 'A' && pw[0] <= 'Z')
upper = true;
else
upper = false;
return upper;
}

bool myISlower(char)
{
bool lower;
string pw;

if (pw[1] >= 'a' && pw[1] <= 'z' && pw[2] >= 'a' && pw[2] <= 'z')
lower = true;
else
lower = false;
return lower;
}
bool myISdigit(char)
{
bool digit;
string pw;

if (pw[3]>= '0' && pw[3] <= '9')
digit = true;
else
digit = false;
return digit;
}

string pwType(int)
{
string strength;
int fTests=0;

if (fTests == 0)
strength = "strong";
if (fTests == 1)
strength = "medium";
if (fTests < 4 && fTests > 1)
strength = "medium weak";
if (fTests >= 4)
strength = "weak";

return strength;
}












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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <iostream>
#include <string>

using namespace std;

bool myISdigit(char);
bool myISupper(char);
bool myISlower(char);
int pwTest(string, int, int, bool); 
string pwType(int);


int main ()
{
string pw, strength;
char verbose;

int sc1, sc2, length;
int fTests = 0;
cout << "Password Strength Checker!" <<endl<<endl;
cout << "Enter the password to check: ";
getline (cin, pw);
cout <<"Enter the two security codes; ";
cin >> sc1 >> sc2;
cout <<"Verbose? (y/n) ";
cin >> verbose;
cout << "Checking "<< pw <<" "<< sc1 << " "<< sc2;

pwTest (pw, sc1, sc2, verbose);
pwType (fTests);

cout <<"Final result:"<<endl;
cout <<"Your password failed "<<fTests<<" tests."<<endl;
cout << "Your password is a " << strength << " password.";


}

int pwTest (string, int, int, bool)
{
int length, moneylocation, spacelocation;
int sc1 = 0;
int sc2 = 0;
int fTests = 0;
string pw;
char verbose = 'n';


length = pw.length();

if (length < 8 && verbose == 'y')
{
fTests++;
cout <<"The password is too short";
}
else if (length < 8 && verbose == 'n')
{
fTests++;
}
else if (length > 14 && verbose == 'y')
{
fTests++;
cout <<"The password is too long";
}
else if (length > 14 && verbose == 'n')
{
fTests++;
}


if (length % 2 !=0 && verbose == 'y')
{
fTests++;
cout<< "Password length is odd";
}
else if (length % 2 !=0 && verbose == 'n')
{
fTests++;
}

if (myISupper(pw[0]) == false && verbose == 'y')
{ 
fTests++;
cout<<"The first character is not an upper case letter";
}
if (myISlower(pw[1]) == false && verbose == 'y')
{
fTests++;
cout<<"The second and / or third characters are not lower case letters";
}
else if (myISlower(pw[1]) == false && verbose == 'n')
{
fTests++;
}


if (myISdigit(pw[3]) == false && verbose == 'y')
{
fTests++;
cout<<"The fourth character is not a digit";
}

else if (myISdigit(pw[3]) == false && verbose == 'n')
{
fTests++;
}

moneylocation = pw.find("$");

if (moneylocation == string::npos && verbose == 'y')
{
fTests++;
cout<<"The password does not have a $ in it";
}
else if (moneylocation == string::npos && verbose == 'n')
{
fTests++;
}

spacelocation = pw.find(" ");

if (spacelocation != string::npos && verbose == 'y')
{
fTests++;
cout<<"The password has a space in it";
}
else if (spacelocation != string::npos &&verbose == 'n')
{
fTests++;
}

if (sc1 % 2 == 0 && sc2 %2 == 0 && verbose == 'y')
{
fTests++;
cout<< "Security codes have the same parity";
}
else if (sc1 %2 ==0 && sc2%2 == 0 && verbose == 'n')
{
fTests++;
}

if (sc1 % 2 !=0 && sc2 % 2 != 0 && verbose == 'y')
{
fTests++;
cout<<"Security codes have the same parity";
}
else if (sc1 %2 !=0 && sc2 %2 !=0 && verbose == 'n')
{
fTests++;
}

return fTests;
}


bool myISupper(char)
{
bool upper;
string pw;

if (pw[0] >= 'A' && pw[0] <= 'Z')
upper = true;
else
upper = false;
return upper;
}

bool myISlower(char)
{
bool lower;
string pw;

if (pw[1] >= 'a' && pw[1] <= 'z' && pw[2] >= 'a' && pw[2] <= 'z')
lower = true;
else
lower = false;
return lower;
}
bool myISdigit(char)
{
bool digit;
string pw;

if (pw[3]>= '0' && pw[3] <= '9')
digit = true;
else
digit = false;
return digit;
}

string pwType(int)
{
string strength;
int fTests=0;

if (fTests == 0)
strength = "strong";
if (fTests == 1)
strength = "medium";
if (fTests < 4 && fTests > 1)
strength = "medium weak";
if (fTests >= 4)
strength = "weak";

return strength;
}
I haven't looked at your code properly but the string "subscript" doesn't exist...
I'm not quite sure what you mean. Whenever I run this code, I get an error box that reads
Debug Assertion Failed!
String subscript is out of range.

Are you saying that it could be out of range because it doesn't exist?
I can't tell you where your problem is because you did not use the code blocks. But in whatever line 'int pwTest (string, int, int, bool)' starts at, you screwed everything up by taking your arguments passed to pwTest and setting them to 0. When you test for the password length use it like this:

if(pw.length() < 8 && verbose == 'y')

There really are too many issues with this code to get it to work, but as far as compile and running it works fine on Dev C++. It just doesn't work right.

M$ Debug features have always been buggy, if you are using their product then disable the debugger.
Last edited on
Topic archived. No new replies allowed.