I'm creating a password verification program (using c-strings) with one of the criteria to have a number within it. My function is causing a fatal assertion error.
1 2 3 4 5 6 7 8 9 10 11 12 13
bool digit(char str[], constint LEN)
{ int count = 0;
for(int i = 0; i < LEN; i++)
{ if(isdigit(str[i]))
count++;
}
if(count > 0)
returntrue;
else
{ cout << "Password must have at least one number.\n";
returnfalse;
}
}
Why do you pass an uninitialized array? Either way, you should pass LENGTH-1 -- you don't want to check the zero character at the end, do you?
Also... the error output really shouldn't be inside the digit function. You return a bool, so the client code can tell whether the check was successful, and decide how to handle that case.
Anyways, you are using VC++ right? Maybe you should just run the debugger and see what exact line causes the error.
The array isn't uninitialized. Here is my code from main:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
constint LENGTH = 21;
char input[LENGTH];
bool flag = false;
cout << "Please enter a password: ";
cin.getline(input, 20);
do
{ if(length(input) && capital(input) && digit(input, LENGTH) && symbol(input) && space(input))
flag = true;
else
{ cout << "Please enter a password: ";
cin.getline(input, 20);
}
}while(flag == false);
cout << "You have entered a successful password. Thank you, goodbye.\n";
return 0;
As you can see, if I put all my bool functions in an if statement, it doesn't leave room for error reporting unless I call the function again. I wanted to have criteria specific prompts for why the password isn't sufficient since I don't list them all out at the beginning.
I don't have any debugger code. The program compiles fine and then crashes.
int _tmain(int argc, _TCHAR* argv[])
{
constint LENGTH = 21;
char input[ LENGTH ];
std::cout << "Enter text: ";
std::cin.getline( input, LENGTH );
int count = 0;
int tempCount = strlen( input );
for( int i = 0; i < tempCount; i++ )
{
if( isdigit( input[ i ] ) )
count++;
}
std::cout << "There were " << count << " digits.\n\n";
return 0;
}
The above code fixed my assertion failure. The use of strlen( input ) to get the actual size of the text entered rather than the size of the array and itterating through that ammount to check it all.