模拟密码输入(显示星号)

六位数字密码
#include <stdio.h>
#include <conio.h>
void in_password(char*);
int main(void)
{
    char password[10],ch;//密码存进数组 password[7]
    printf("请输入密码:");
    in_password(password);
    printf("\n你的密码是:%s\n", password);
    return 0;
}
void in_password(char *password)
{
    int i,j;
    char ch;
    int password_flag;
    while(1)
	{
        i = 0;
        password_flag=0;
        while(1)
		{
			if (i>6)     //输到第七位就会报错 
                break;
            ch = getch();
            if(ch == '\r' )  //回车 
                break;
            else if(ch == '\b')
			{
                if(i > 0)
				{
                    printf("\b \b");//输错时可删除,重新输入 
                    i--;
                }
                continue;
            }
            printf("*");
            
            password[i++] = ch;
        }
        //password[i] = 0;
        password[i] ='\0';//不加这句会导致1234567↙  123456↙ 输出1234567 
        for(j=0;j<=5;j++)
		{
			if(password[j]>='0' && password[j]<='9')
			password_flag++;
		 } 
		printf("\n数字个数:");
		printf("%d\n",password_flag);
        if(i!=6)         //只需要判断小于6 
            printf("\n长度错误,请重新输入六位密码:");
        else if(password_flag!=6) 
            printf("\n您使用了非数字符号,请重新输入六位纯数字密码:");
        else
            break;
    }
}



---------------------------------------------


输到17位出现百分号????

解决方法恢复//password[i] = 0;
//6-16位密码 
#include <stdio.h>
#include <conio.h>
void in_password(char*,int,int);
int main(void)
{
    int min_len = 6, max_len = 16;
    char password[25],ch;
    printf("请输入密码:");
    in_password(password, min_len, max_len);
    printf("\n你的密码是:%s\n", password);
    return 0;
}
void in_password(char *password,int min,int max)
{
    int i;
    char ch;
    while(1)
	{
        i = 0;
        while(1)
		{
			if(i >= max)
                break;
            ch = getch();
            if(ch == '\r')
                break;
            else if(ch == '\b')
			{
                if(i > 0)
				{
                    printf("\b \b");
                    i--;
                }
                continue;
            }
            printf("*");
            
            password[i++] = ch;
        }
        //password[i] = 0;
        if(i < min || i > max)
            printf("\n长度错误,请重新输入:");
        else
            break;
    }
}








----------------------------------------------------------------

转载

#include<stdio.h>  

#include<conio.h>   
int main()  
{  
    char a[100], c;  
    int i = 0;  
    while (1)  
    {  
        if ((c = getch()) == '\b')  
        {  
            printf("\b \b");  
            --i;  
        }  
        else if (c == '\r' || c == '\n')  
            break;  
        else  
        {  
            a[i] = c;  
            ++i;  
            printf("*");  
        }     
    }  
    a[i] = '\0';  
    printf("\npassword : %s",a);  
    return 0;  

}  

-----------------------

#include <stdio.h>
#include <conio.h>
void in_password(char*,int,int);
int main(void)
{
    int min_len = 6, max_len = 16;
    char password[25],ch;
    printf("Please input password:");
    in_password(password, min_len, max_len);
    printf("\nYour password is:%s\n", password);
    return 0;
}
void in_password(char *password,int min,int max)
{
    int i;
    char ch;
    while(1){
        i = 0;
        while(1){
            ch = getch();
            if(ch == '\r')
                break;
            else if(ch == '\b'){
                if(i > 0){
                    printf("\b \b");
                    i--;
                }
                continue;
            }
            printf("*");
            if(i > max)
                break;
            password[i++] = ch;
        }
        password[i] = 0;
        if(i < min || i > max)
            printf("\nPlease input again:");
        else
            break;
    }
}  

猜你喜欢

转载自blog.csdn.net/tingtingyuan/article/details/80820290