2017-05-30 60 views
-2

我寫了一個簡單的代碼,它接受來自用戶的密碼並檢查它是否包含UpperCase字母,數字和特殊字符。密碼長度應該3.C編程 - 操作員

問題:

用戶應該獲得三項試驗之前結束。如果密碼正確,代碼就可以很好地工作,但如果密碼錯誤,它會持續無限地提問。我認爲增加運營商增加嘗試是行不通的(或者我犯了錯誤)。

感謝您的幫助。

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 

int main() 
{ 
    int i=0, letter=0, digit=0, character=0; 
    int attempt=0; 
    char password[3]; 

    while(attempt<3) 
    { 
     printf("\nAttempt: %d\n", attempt); 
     printf("\nPlease enter the 3 character password containing Letter in upper case, digit & special character: \n"); 
     scanf(" %s", password); 

     for(i=0; i<=2; i++) 
     { 
      if(isalpha(password[i])) 
      { 
       if(isupper(password[i])) 
       { 
        letter=1; 
        continue; 
       } 
       else 
       { 
        printf("Wrong Password (Upper case letter needed)\n"); 
        break; 
       } 
      } 
      else if(isdigit(password[i])) 
      { 
       digit=1; 
       continue; 
      } 
      else 
      { 
       character=1; 
      } 
     } 
     if(letter==1 && digit==1 && character==1) 
     { 
      printf("You have entered correct password\n"); 
      break; 
     } 
     else 
     { 
      printf("You have entered wrong password Try again\n"); 
      attempt++; 
     } 
    } 
    return 0; 
} 
+5

要長3,你需要聲明'字符密碼[4];'而不是'字符密碼[3]; ',同樣在'scanf'中使用''%3s'''以避免緩衝區溢出。 –

回答

0

首先你已經因爲你已經在你的話的末尾\0字符申報尺寸4的按鍵序列。這是代碼,我只是改了幾行。

int main(){ 
int i=0, letter=0, digit=0, character=0; 
int attempt=0; 
char password[4]; /*For a string of 3 character you must declare it of 4 because '\0' */ 
while(attempt<3){ 
    printf("\nAttempt: %d\n", attempt); 
    printf("\nPlease enter the 3 character password containing Letter in upper case, digit & special character: \n"); 
    scanf(" %3s", password); 
    for(i=0; i<=2; i++){ 
     if(isalpha(password[i])){ 
      if(isupper(password[i])){ 
       letter=1; 
       continue; 
      }else{ 
      printf("Wrong Password (Upper case letter needed)\n"); 
      break; 
      } 
     }else if(isdigit(password[i])){ 
      digit=1; 
      continue; 
     }else{ 
      character=1; 
     } 
    } 
    if(letter==1 && digit==1 && character==1){ 
     printf("You have entered correct password\n"); 
     break; 
    } 
    printf("You have entered wrong password Try again\n"); 
    attempt++; /*You can increase it here because if you enter the correct password you'll exit*/ 
} 
return 0; 

}

+0

解決。這是數組大小的錯誤。愚蠢的錯誤! 非常感謝 –

0

你在你的程序一個非常嚴重的錯誤。 password數組太短而無法保存3個字符的字符串。請記住C字符串需要NUL終止。這意味着要存儲字符串「Ab!」你真的需要4個字節的內存,因爲它將被存儲爲'a''b''!''\0'。由於您只保留了3個字節的密碼,因此如果用戶輸入3個字符,您的代碼將寫入保留內存之外。這是未定義的行爲。

這能解釋爲什麼你的代碼會無限延續嗎?

那麼,如果你有未定義的行爲,那麼可以說任何事情都可能發生 - 所以是的!可能發生的是,當在password之外寫入'\0'(即0)時,它可以覆蓋(部分)attempt並將其設置回零。很難說這是否會發生,因爲它取決於具體的系統。

因此,重要的是:

char password[3]; --> char password[4]; 

接下來的事情:

您需要在每次嘗試計數器復位。那就是:

while(attempt<3) 
{ 
    letter=0; 
    digit=0; 
    character=0; 
    .... 

如果不這樣做,你可能會得到誤報從一個嘗試到下一個被執行的結果。

最後 - 永遠不會做的事:

scanf(" %s", password); 

用戶可以通過按下回車鍵之前鍵入超過3個字母溢出輸入緩衝區(即password)。請使用fgets。如果你真的想scanf至少把在限制,如輸入的長度:

scanf(" %3s", password); 
+0

已解決。這是數組大小的錯誤。愚蠢的錯誤!非常感謝 –