c语言,怎么把输入的数字像密码一样隐藏输出星号啊

如题所述

#include <stdio.h>
#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;  
}

温馨提示:内容为网友见解,仅供参考
第1个回答  2015-11-09
试一下
int i,a;
for(i=0;i<6;i++)
{
    a=getch();
    printf("*");
}

追问

我那个数字是个数组

追答

改一下不就好了么,关键是要用getch( )函数

追问

大神,我蒙了,怎么隐藏啊

追答

追问

那个if(k==10或k==13)是什么意思?

😰

追答

是在输入密码的时候,你不知道它的密码有多长,当它输入完密码按下回车键后,表示它的密码已经输完,开始比较密码,回车换行的ASCII是10,13
另外定义char数组的时候最好把它初始化为\0

追问

天啊。。。好吧

可是为什么我运行就有错误呢

追答

报的什么错?

追问

哦哦

本回答被提问者采纳
第2个回答  2018-01-04
//输入用户名
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;
}
}
}
第3个回答  2018-07-18
这里还有输入退格键可以删除星号的版本,感兴趣的可以去看看
https://blog.csdn.net/y_universe/article/details/77193645
https://blog.csdn.net/czg13548930186/article/details/72847722
相似回答