//It it gave an error message here unless i used NULL
char passwd[20]={"\0"};
int x = 0;
bool good[4]={false}; // Here i was just confused. Does {} set all variables in an array to a set value?
bool GreatPass = false;
Are you coding in C or C++?
An empty set of braces just initializes all elements of an array/struct/class/union to 0.
In C you have to do
int myArray[20] = {0}; // or apparently something similar as you did {"\0"} and {false}
In C++ you can do
int myArray[20]{};
or
int myArray[20] = {};
// Iff the string was less than 18 then i assumed that it would just waste time and not do anything.
// No where in this loop does it have the option to set any Boolean to false.
}while(x <= strlen(passwd));
Yeah, it would just waste time. Btw, you can just put strlen(passwd) in a var before the second do/while stuff since it doesn't change during the inner do loop. That would also help out in this part
Code: Select all
if( isdigit(passwd[7]) || isalpha(passwd[7]))
{
good[4] = true;
}
You could just do:
Code: Select all
if( lengthOfString >= stringMin )
{
good[4] = true;
}
Of course, you'd also have to create a var named stringMin and set it to 8 or whatever.
The inner do/while loop is a good case for a for loop since you can get the length of a string and know how many iterations to do, it would also take care of incrementing and resetting the X variable.
const int lengthOfString = strlen(passwd);
for(int x = 0; x < lengthOfString; x += 1)
{
// do your checks for isdigit and isalpha
}
//I added a break statement so it didn't print out useless information.
if(good[0] != true && good[2] != true){printf("You need a letter in your password. \n"); break;};
I would consider using continue instead of break. Break will break out of the do loop so it never asks for the password again, whereas I think you are wanting it to ask again and it still skips all the unnecessary printf statements.
Here's how it all would look
Code: Select all
int main()
{
// Initialize arrays to 0's
char passwd[ 20 ] = { 0 };
bool good[ 4 ] = { 0 };
// It's a good idea to use the sizeof operator.
// I didn't know if bool was a typedef for int or _Bool
const int goodSize = sizeof( good );
bool GreatPass = false;
// Use variables instead of magic numbers when possible.
// Makes changing and testing a lot easier
const int minStringLength = 8;
do
{
// Zero all elements in the good array each failed password attempt
memset( good, 0, goodSize );
printf( "Type a password: \t" );
scanf( " %s", &passwd );
printf( "\n" );
// Cache the length of the string for multiple uses
const int lengthOfString = strlen( passwd );
// You can use a for loop to track and increment the index
for( int i = 0; i < lengthOfString; i += 1 )
{
if( isalpha( passwd[ i ] ) )
{
if( isupper( passwd[ i ] ) )
{
good[ 0 ] = true;
}
else
{
good[ 1 ] = true;
}
}
// If isalpha, it can't be a digit, so else if
else if( isdigit( passwd[ i ] ) )
{
good[ 2 ] = true;
}
}
// Now you can just check the length of the string using the cached version
if( lengthOfString >= minStringLength )
{
good[ 3 ] = true;
}
if( !good[ 0 ] && !good[ 1 ] )
{
printf( "You need a letter in your password. \n" );
// Use continue to skip the rest of the printf statements,
// but still loop back and ask for a different password
continue;
}
if( !good[ 0 ] )
{
printf( "You need an uppercase letter in your password. \n" );
}
if( !good[ 1 ] )
{
printf( "You need a lowercase letter in your password. \n" );
}
if( !good[ 2 ] )
{
printf( "You need a number in your password. \n" );
}
if( !good[ 3 ] )
{
printf( "You need a longer password. \n" );
}
// Assign bool values directly by logical and (&&) or, bitwise and (&)
// GreatPass = good[ 0 ] & good[ 1 ] & good[ 2 ] & good[ 3 ];
// Saves you from having to write the long if condition statement
GreatPass = good[ 0 ] && good[ 1 ] && good[ 2 ] && good[ 3 ];
} while( GreatPass != true );
}
I still notice a few mistakes in your update, like accessing the elements past the size of the good array, remember, the first index starts at 0 and goes to size -1. If size = 4, then the indices are 0,1,2,3 not 1,2,3,4.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com