aaaaaa(六位)* 隱藏明文getch() / _getch():無回顯讀取單一字元(需 conio.h,Windows 專用)putchar('*'):即時回顯星號ASCII 13 結束輸入#include <stdio.h>
#include <string.h>
#include <conio.h> // Windows 專用
int verifyPassword();
int main()
{
if (verifyPassword() == 0)
printf("\n登入成功!\n");
else
printf("\n帳號已鎖定,請至銀行櫃台解鎖。\n");
return 0;
}
int verifyPassword()
{
char correct[7] = "aaaaaa";
char input[7];
int attempts = 3;
while (attempts-- > 0) {
printf("請輸入6位密碼: ");
int i = 0;
char ch;
while ((ch = _getch()) != '\r' && i < 6) { // Enter=13='\r'
putchar('*');
input[i++] = ch;
}
input[i] = '\0';
printf("\n");
if (strcmp(input, correct) == 0)
return 0; // 成功
printf("密碼錯誤,剩餘嘗試 %d 次\n", attempts);
}
return 1; // 失敗
}
conio.h,需用 termios.h 關閉回顯模式真實銀行系統:
本程式僅供教學示範 getch 與流程控制。