0

My program, which is for generating an acceptable password, is not displaying any output aside from input prompt and it lets me input the password but the program immediately ends. I get no errors on my end or warnings on my debugger. Was wondering if someone could give me some input.

#include "stdafx.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>


int main()
{
    char password[15] = {'\0'};
    char search[15] = { '\0' };
    int a, b, c, d, e;
    char punct[] = { '!','@','#','$','%','^','&','*','?','_', '\0' };

    printf("Enter your test password: \n");
    fgets(password, 15, stdin );

    a = strnlen(password, 15);//judges length of search between the numbers 2 and 15
    b = strncmp(password, punct, 10);
    c = isalpha(strnlen(password,15));
    d = isdigit(strnlen(password, 15));
    e = a + b + c + d;

    if (a < 2 || a>15) {

        printf("Must have exactly 2-15 characters.\n");

        if (strncmp(password, punct, 10) == false) {//must have one of the characters included in password
            printf("Must have character punctutation\n");
        }
        if (isdigit(strnlen(password, 15)) == false) {
            printf("Must consist of at least one number 0-9.\n");
        }
        if (isalpha(strnlen(password, 15)) == false) {
            printf("Must consist of at least one letter a-z\n");
        }
        else {
            printf("You have inputted a legal password!\n");
        }

    }
        return 0;
    }
Chris
  • 65
  • 1
  • 6
  • your input length is >= 2 or <= 15? – Les Oct 15 '15 at 20:14
  • why are you doing isalpha on a length? and isdigit? (unrelated) – Les Oct 15 '15 at 20:14
  • I thought if it ran array with isalpha or digit that it would give me a int that i in turn could use for a true false statement – Chris Oct 15 '15 at 20:35
  • isalpha and isdigit test a character, not a string, search for each to get their proper usage – Les Oct 15 '15 at 21:17
  • `char password[15]` --> `char password[15+2]` as `fgets()` needs room for the `'\n'` and `'\0'`. Use `fgets(password, sizeof passowrd, stdin );` and strip trailing `'\n'` off after `fgets()`. – chux - Reinstate Monica Oct 15 '15 at 23:46
  • Anyone have an idea how to have punct array scan through passwords to have it return an integer or a true statement if possibleif one of the symbols is detected in passwords array – Chris Oct 16 '15 at 11:44

1 Answers1

0

You are not getting an output because strnlen(password, 15) is between 2 and 15, use an else part:

if (a < 2 || a > 15) {
   ...
} else {
   /* your output here */
}

On the other hand:

if (isdigit(strnlen(password, 15)) == false) {

is a nonsense, isdigit checks if a character is a decimal digit, but you are passing a size_t instead of a char, same for isalpha.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • Is there a command or loop that will let do something that will let me scan the inputted char. I just thought the isdigit would return a 0 (false) which could translate into comparing it with a true false statement – Chris Oct 15 '15 at 20:33
  • did not help and I am possibly more confused now than before – Chris Oct 15 '15 at 21:40