2017-05-30 29 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

已解决。这是数组大小的错误。愚蠢的错误!非常感谢 –