In an attempt to familiarize myself with the fundamentals of C, 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.
However anytime the password fails condition 2-5 my program will crash with the following prompt from the compiler.
Code:
#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 = 0;
int dollar = 0;
int digit = 0;
int upperCase = 0;
int lowerCase = 0;
printf("Requirements for a valid password:nn");
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.nn");
printf("Enter password: ");
scanf(" %s", password);
printf("n");
for (loop = 0; loop < 21; loop++) {
if (password[loop] == '$') {
dollar = 1;
break;
}
}
if (dollar == 0) {
printf("Invalid password, recheck for condition 1.nn");
}
for (loop = 0; loop < 21; loop++) {
if (isdigit (password[loop])) {
digit = 1;
break;
}
}
if (digit == 0){
printf("Invalid password, recheck for condition 2.nn");
}
for (loop = 0; loop < 21; loop++) {
if (isupper(password[loop])) {
upperCase = 1;
break;
}
}
if (upperCase == 0) {
printf("Invalid password, recheck for condition 2.nn");
}
for (loop = 0; loop < 21; loop++) {
if (islower(password[loop])) {
lowerCase = 1;
break;
}
}
if (lowerCase == 0) {
printf("Invalid password, recheck for condition 2.nn");
}
if ((dollar * digit * upperCase * lowerCase) != 0) {
printf("Password saved!nn");
}
system("pause")
return(0);
}