以前做小程序时图好玩在网上找的代码。输入的密码会以星号显示出来,并且输入错了可以删除。因为用了专有库函数,所以只能在Windows平台使用,少用为好,不过可能还有点用。嗯…就这样了
#include <stdio.h> #include <string.h> #include <conio.h> #define MAX_PASSLEN 128 //定义密码长度 char tpass[20]="admin"; void GetPassword(char *szFinalPass) { char chValue,szPassword[MAX_PASSLEN]; int iCounter = 0; //定义计数器 while ( 1 ) { if( ( chValue = getch() ) != ' ' ) //如果输入的不是回车 { if( chValue != '' ) //如果输入的不是退格 { if ( iCounter < MAX_PASSLEN ) //如果长度并未超过密码的最大长度 { szPassword[iCounter] = chValue; putchar( '*' ); //在屏幕上显示星号 iCounter ++; } else { putchar( '7' ); //如果密码已经超过最大长度,则响铃报警 } } else { if( iCounter != 0 ) //如果按了退格,并且当前不是第一个字符 { iCounter --; printf( " " ); //注意两个之间是有个空格的,含义是先退格, //然后打印空白字符将之前的字符覆盖掉,然后再退格使光标退回 } } } else { szPassword[iCounter] = 0; //密码输入结束时将末尾以 结尾! break; } } strcpy( szFinalPass ,szPassword ); //最终将密码复制出来 } int main() { char szPassword[128]; GetPassword(szPassword); if(strcmp(szPassword,tpass)==0) printf(" 输入的密码是:%s ",szPassword); else printf(" 密码错误 "); }