我寫了一個簡單的代碼,它接受來自用戶的密碼並檢查它是否包含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;
}
要長3,你需要聲明'字符密碼[4];'而不是'字符密碼[3]; ',同樣在'scanf'中使用''%3s'''以避免緩衝區溢出。 –