如何解决我如何显示星号*代替密码c ++的纯文本
|| 我该怎么做才能在C ++中显示star(*)而不是纯文本作为密码。 我要求输入密码,这是在屏幕上的简单密码。 如何将它们转换为star(*),以便用户在输入时看不到密码。 这就是我目前所拥有的 char pass[10]={\"test\"};
char pass1[10];
textmode(C40);
label:
gotoxy(10,10);
textcolor(3);
cprintf(\"Enter password :: \");
textcolor(15);
gets(pass1);
gotoxy(10,11);
delay(3000);
if(!(strcmp(pass,pass1)==0))
{
gotoxy(20,19);
textcolor(5);
cprintf(\"Invalid password\");
getch();
clrscr();
goto label;
}
谢谢
解决方法
您需要使用无缓冲输入功能,例如curses库提供的
getch ()
,或操作系统的控制台库。调用此函数将返回按下的键字符,但不会回显。阅读带有ѭ1read的每个字符后,可以手动打印*
。另外,如果按了退格键,还需要编写代码,并正确更正插入的密码。
这是我曾经用诅咒写过的代码。用gcc file.c -o pass_prog -lcurses
编译
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#define ENOUGH_SIZE 256
#define ECHO_ON 1
#define ECHO_OFF 0
#define BACK_SPACE 127
char *my_getpass (int echo_state);
int main (void)
{
char *pass;
initscr ();
printw (\"Enter Password: \");
pass = my_getpass (ECHO_ON);
printw (\"\\nEntered Password: %s\",pass);
refresh ();
getch ();
endwin ();
return 0;
}
char *my_getpass (int echo_state)
{
char *pass,c;
int i=0;
pass = malloc (sizeof (char) * ENOUGH_SIZE);
if (pass == NULL)
{
perror (\"Exit\");
exit (1);
}
cbreak ();
noecho ();
while ((c=getch()) != \'\\n\')
{
if (c == BACK_SPACE)
{
/* Do not let the buffer underflow */
if (i > 0)
{
i--;
if (echo_state == ECHO_ON)
printw (\"\\b \\b\");
}
}
else if (c == \'\\t\')
; /* Ignore tabs */
else
{
pass[i] = c;
i = (i >= ENOUGH_SIZE) ? ENOUGH_SIZE - 1 : i+1;
if (echo_state == ECHO_ON)
printw (\"*\");
}
}
echo ();
nocbreak ();
/* Terminate the password string with NUL */
pass[i] = \'\\0\';
endwin ();
return pass;
}
, C ++本身没有任何东西可以支持这一点。示例代码中的函数建议您使用的是curses
或类似名称;如果是这样,请检查cbreak
和nocbreak
功能。叫cbreak
后,就由您来回显字符,并且您可以回显任何您喜欢的字符(如果愿意,也可以不回声)。
, #include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char a[10];
for(int i=0;i<10;i++)
{
a[i]=getch(); //for taking a char. in array-\'a\' at i\'th place
if(a[i]==13) //cheking if user press\'s enter
break; //breaking the loop if enter is pressed
printf(\"*\"); //as there is no char. on screen we print \'*\'
}
a[i]=\'\\0\'; //inserting null char. at the end
cout<<endl;
for(i=0;a[i]!=\'\\0\';i++) //printing array on the screen
cout<<a[i];
sleep(3); //paused program for 3 second
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。