#include <stdio.h>
#include <string.h>
int main() {
const char *password = "123456";
char input[20];
int attempts = 3;
while (attempts > 0) {
printf("輸入密碼 (剩餘 %d 次): ", attempts);
if (scanf("%19s", input) != 1) { puts("輸入錯誤"); continue; }
if (strcmp(input, password) == 0) {
puts("登入成功!");
break;
} else {
--attempts;
if (attempts > 0) puts("密碼錯誤,請重試");
else puts("嘗試次數用盡,帳號鎖定");
}
}
return 0;
}
strcmp:== 比較指標位址,非內容。%19s 防止溢位 (留 1 位給 \0)。break,失敗遞減,歸零鎖定。此範例展示基本 I/O、字串操作、流程控制整合,適合入門練習。