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
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";
}
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++;
}
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.