2015-10-15 151 views
0

我的程序用于生成可接受的密码,除了输入提示之外,它不显示任何输出,它允许我输入密码,但程序立即结束。我在调试器上没有发现错误或警告。想知道是否有人可以给我一些意见。命令提示符中没有输出

#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; 
    } 
+0

您的输入长度是> = 2还是<= 15? – Les

+0

你为什么要在一段长度上做isalpha?和isdigit? (无关) – Les

+0

我想如果它运行与isalpha或数字数组,它会给我一个int,我反过来可以用于一个真正的虚假陈述 – Chris

回答

0

你没有得到的输出,因为strnlen(password, 15)215之间,使用else部分:

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

在另一方面:

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

是无稽之谈,isdigit检查一个字符是否是十进制数字,但哟你通过size_t而不是charisalpha相同。

+0

是否有一个命令或循环,让我做一些让我扫描输入的字符。我只是认为isdigit会返回一个0(false),这可能会转化为将其与真实的虚假陈述进行比较 – Chris

+0

检查此问题[答案](http://stackoverflow.com/a/16644950/1606345) –

+0

没有帮助,我现在可能比以前更困惑了 – Chris