I'm trying to create a code for password verification for a class. I thought I had it working but for some reason every time I input a seemingly correct password, it says it must have one lowercase letter. The only error I can find is "not all control paths return a value." Any help please?
#include <iostream>
#include <cstring>
#include <cctype>
usingnamespace std;
bool lengthTest(char []);
bool upTest(char []);
bool lowTest(char []);
bool numTest(char []);
int main()
{
char password[20];
// Explain password criteria and prompt user for password.
cout << "The password must have:\n" <<
" at least 6 characters,\n" <<
" at least one uppercase character,\n" <<
" at least one lowercase character,\n" <<
" at least one numeric digit.\n" <<
"Enter a password:\n";
cin.getline(password,20);
// Verify password.
if (lengthTest(password))
if (upTest(password))
if (lowTest(password))
if (numTest(password))
cout << "The password is valid.\n";
else
cout << "The password is invalid.\n";
return 0;
}
// *******************************************
// Verifies password length.
// *******************************************
bool lengthTest(char custPass[])
{
int length;
length = strlen(custPass);
if (length >= 6)
returntrue;
else
cout << "The password must have a minimum of 6 characters.\n";
returnfalse;
}
// *******************************************
// Verifies uppercase letter.
// *******************************************
bool upTest(char custPass[])
{
for (int i = 0; i < 20; i++)
{
if (isupper(custPass[i]))
returntrue;
else
cout << "Must have at least one uppercase letter.\n";
returnfalse;
}
}
// *******************************************
// Verifies lowercase letter.
// *******************************************
bool lowTest(char custPass[])
{
for (int i = 0; i < 20; i++)
{
if (islower(custPass[i]))
returntrue;
else
cout << "Must have at least one lowercase letter.\n";
returnfalse;
}
}
// *******************************************
// Verifies digit.
// *******************************************
bool numTest(char custPass[])
{
for (int i = 0; i < 20; i++)
{
if (isdigit(custPass[i]))
returntrue;
else
cout << "Must have at least one digit.\n";
returnfalse;
}
}
warning C4715: 'upTest' : not all control paths return a value
warning C4715: 'lowTest' : not all control paths return a value
warning C4715: 'numTest' : not all control paths return a value
It says the build succeeded but it isn't working like how it should.
And by your suggestion, do you mean around the first collection of ifs?
bool upTest(char custPass[])
{
for (int i = 0; i < 20; i++)
{
if (isupper(custPass[i]))
returntrue;
else
cout << "Must have at least one uppercase letter.\n";
returnfalse;
}
}
I add braces to make it more clear what you are doing:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
bool upTest(char custPass[])
{
for (int i = 0; i < 20; i++)
{
if (isupper(custPass[i]))
{
returntrue;
}
else
{
cout << "Must have at least one uppercase letter.\n";
}
returnfalse;
}
}
But why have a loop at all, since there will be a return on first iteration:
1 2 3 4 5 6 7 8 9 10 11 12 13
bool upTest(char custPass[])
{
if (isupper(custPass[0]))
{
returntrue;
}
else
{
cout << "Must have at least one uppercase letter.\n";
}
returnfalse;
}