預設密碼 "123456",最多 3 次錯誤鎖定 5 秒。
#include <stdio.h>
#include <string.h>
#include <windows.h> // Sleep()
int main() {
const char *pwd = "123456";
char input[10];
for (int i = 0; i < 3; i++) {
printf("請輸入密碼 (最多9位):");
scanf("%s", input);
if (strcmp(pwd, input) == 0) { puts("取款成功"); return 0; }
puts("密碼錯誤!");
}
puts("三次錯誤,5秒後關閉系統");
Sleep(5000); // 毫秒
return 0;
}
// Linux/Unix
#include <unistd.h>
sleep(5); // 秒
// 跨平台封裝
#ifdef _WIN32
#include <windows.h>
#define MSLEEP(ms) Sleep(ms)
#else
#include <unistd.h>
#define MSLEEP(ms) usleep((ms)*1000)
#endif
scanf("%9s", input) 防緩衝區溢出。