如何有呼应与使用星号残培密码() [英] How to have password echoed as asterisks using getch()

查看:111
本文介绍了如何有呼应与使用星号残培密码()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使从用户输入的密码和存储密码由用户输入的字符串,但在显示应包含星号,采用的getch()功能的程序。我被这个迷惑。如果有人可以帮助?

I was trying to make a program that inputs a password from a user and stores the password as the string entered by the user but the display should contain asterisks, using getch() function. i am bamboozled by this. If anyone could help?

推荐答案

假设你在Windows上运行,你的编码是ASCII结果
试试这个:

Assuming you are running on windows and your encoding is ASCII
Try this:

#include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
    char buffer[256] = {0};
    char password[] = "password";
    char c;
    int pos = 0;

    printf("%s", "Enter password: ");
    do {
        c = getch();

        if( isprint(c) ) 
        {
            buffer[ pos++ ] = c;
            printf("%c", '*');
        }
        else if( c == 8 && pos )
        {
            buffer[ pos-- ] = '\0';
            printf("%s", "\b \b");
        }
    } while( c != 13 );

    if( !strcmp(buffer, password) )
        printf("\n%s\n", "Logged on succesfully!");
    else
        printf("\n%s\n", "Incorrect login!");
    return 0;
}

这篇关于如何有呼应与使用星号残培密码()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆