gpt4 book ai didi

c - 如何使用 getch() 将密码回显为星号

转载 作者:太空宇宙 更新时间:2023-11-04 02:56:07 25 4
gpt4 key购买 nike

我试图制作一个程序,该程序使用 getch() 函数从用户输入密码并将密码存储为用户输入的字符串,但显示应包含星号。我被这个弄糊涂了。如果有人可以帮忙?

最佳答案

假设您在 Windows 上运行并且您的编码是 ASCII
试试这个:

#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;
}

关于c - 如何使用 getch() 将密码回显为星号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17130979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com