#include
#define MAX_STR_LEN 32
char passwd[MAX_STR_LEN] = {0};
char *GetPasswd(void)
{
char c;
int i = 0;
int len = MAX_STR_LEN-1;
while ((c=getch()) != '\r')
{
passwd[i] = c;
putchar('*');
i++;
if (i >= len)
{
break;
}
}
return passwd;
}
int main(void)
{
char *dispstr = NULL;
dispstr = GetPasswd();
printf("\nthe password is : %s\n", dispstr);
return 0;
}
试一下
int i,a;
for(i=0;i<6;i++)
{
a=getch();
printf("*");
}
//输入用户名
int login()
{
char tmpname[20],tmppwd[20];
char c;
int i=0,j;
while(i<3)
{
system("cls");
printf("请输入用户名:\n");
scanf("%s",tmpname);
printf("请输入密码:\n");
j=0;
while(1)//循环读单个字符
{
c=getch();
if(c=='\r')
{
tmppwd[j]=0;
break;
}
printf("*");
tmppwd[j]=c;
j++;
}
if(strcmp(tmpname,name)==0 && strcmp(tmppwd,pwd)==0)
return 1;
else
{
i++;
printf("用户名或者密码错误!还有%d次机会。\n",3-i);
system("pause");
if(i==3)
return 0;
}
}
}
这里还有输入退格键可以删除星号的版本,感兴趣的可以去看看
https://blog.csdn.net/y_universe/article/details/77193645
https://blog.csdn.net/czg13548930186/article/details/72847722