2015-10-15 143 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

相關問題