Reputation: 59
In an attempt to familiarize myself with fundamentals, I've been trying to write a simple program for choosing a password. The password is suppose to conform to the 5 listed conditions. The code is designed to loop through the password to determine if the conditions are satisfied and prompt users of any issues.
If the conditions are satisfied its coinciding variable is set to 1. Any variable left 0 is intended to prompt an invalid password. Unfortunately, it would appear that only the first character is being identified. All other conditions, aside from the first character, will fail regardless of the string.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main(void) {
char password[21];
int loop;
int dollar = 0;
int digit = 0;
int upperCase = 0;
int lowerCase = 0;
printf("Requirements for a valid password:\n\n");
printf("1. Contain at least one $ sign.\n");
printf("2. Contain at least one number.\n");
printf("3. Contain at least one uppercase letter.\n");
printf("4. Contain at least one lowercase letter.\n");
printf("5. Contain no more than 20 characters.\n\n");
printf("Enter password: ");
scanf(" %s", &password);
printf("\n");
for (loop = 0; loop < 21; loop++) {
if (password[loop] == '$') {
dollar = 1;
break;
}
else {
printf("Invalid password, recheck for condition 1.\n\n");
break;
}
}
for (loop = 0; loop < 21; loop++) {
if (isdigit(password[loop])) {
digit = 1;
break;
}
else {
printf("Invalid password, recheck for condition 2.\n\n");
break;
}
}
for (loop = 0; loop < 21; loop++) {
if (isupper(password[loop])) {
upperCase = 1;
break;
}
else {
printf("Invalid password, recheck for condition 3.\n\n");
break;
}
}
for (loop = 0; loop < 21; loop++) {
if (islower(password[loop])) {
lowerCase = 1;
break;
}
else {
printf("Invalid password, recheck for condition 4.\n\n");
break;
}
}
if ((dollar * digit * upperCase * lowerCase) != 0) {
printf("Password saved!");
}
system("pause");
return(0);
}
Upvotes: 1
Views: 115
Reputation: 445
So, let's do a simple dry run here. Let's take your code for the first condition (and as remaining all same in logic, so that should do).
for (loop = 0; loop < 21; loop++) {
if (password[loop] == '$') {
dollar = 1;
break;
}
else {
printf("Invalid password, recheck for condition 1.\n\n");
break;
}
}
Let's begin with loop
value equals zero. Now the following conditions exist:
$
. In this case the if
condition is satisfies, and we exit out after setting the dollar
flag.$
(this is where we primarily go wrong). The if
condition fails, as expected - and so we move to the else
straightaway. Here we go on to print the error message and break out of the loop, without checking the remaining characters! Logically, we should wait to print the error till we have processed ALL the characters and not found the condition to be satisfied.So, now we understand the problem - we are printing the error and breaking out after just checking for the first character. But, how can we fix this?
Well, for starters we should wait till we search through all the characters. So the else part must move out of the loop. Also, we know that if we do indeed have the condition satisfied, we will set the dollar
variable, and exit the loop. What if we don't find the condition satisfied? Well, in that case dollar
will remain zero - and that's how we detect the error!
So, we could possibly do something like:
// Loop through the characters, and as soon as we find $ we set the dollar variable and break
for (loop = 0; loop < 21; loop++) {
if (password[loop] == '$') {
dollar = 1;
break;
}
}
// If dollar is still zero, we didn't encounter the $ character
if (dollar == 0) {
printf("Invalid password, recheck for condition 1.\n\n");
}
There are a couple of other simple mistakes in the code you posted, but primarily the logical flaw is the above. Look out for the comments to understand the other possible loopholes. Cheers!
Upvotes: 3