求助:如何在c++中实现密码的输入过程

RookieStar 2003-04-01 12:19:44
如何在c++中实现密码的输入过程?
(即输入时显示*,而实际的输入能得以保存)
能够的话请附上所用的函数代码并给予一定的解释
...全文
167 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
LoveCreatesBeauty 2003-04-01
  • 打赏
  • 举报
回复
conio.h里面有getch(), _getch()在读取console输入的时候不会产生回显。“/* Non-ANSI names for compatibility */”好像针对的是getch(), 不过他们好像不是属于标准库里面的。可能不具有可移植性。在18 Standard C library headers中也没有conio.h。
cxjddd 2003-04-01
  • 打赏
  • 举报
回复
getpass()是TC2支持的,不通用。
  • 打赏
  • 举报
回复
你的编译器不一定支持这个
#include <conio.h>
char* getpass(const char*prompt);

char* password;
password=getpass("enter password:);
if(strcmp(password,"你的密码"))

else


其实可以自己写一个的,多给几分吧:)
diabloqin 2003-04-01
  • 打赏
  • 举报
回复
gz
earthharp 2003-04-01
  • 打赏
  • 举报
回复
getch()
要不就用windows的dialogbox
lwbhero 2003-04-01
  • 打赏
  • 举报
回复
搜索一下,有很多这方面的帖子.
aiyinsitan 2003-04-01
  • 打赏
  • 举报
回复
用库函数getch()读入一个字符并保存,接着在
屏幕上打印一个‘*’号(putchar())
wyqiang 2003-04-01
  • 打赏
  • 举报
回复
在vc下找

有哪个函数
我见过
CEdit::GetPasswordChar

TCHAR GetPasswordChar( ) const
ES_PASSWORD的方式
SetPasswordChar( TCHAR ch );
Jinhao 2003-04-01
  • 打赏
  • 举报
回复
来个黑纸白字的程序

#include<conio.h>
#include<iostream>

bool checkpassword(char *password,char *std)
{
 int len=strlen(password);
 if (len!=strlen(std)) return false;
 for (int i=0;i<=len;i++)
 {
  if (password!=std) return false;
 }
 return true;
}

int main()
{
char *password=new char[17];
char a;
int i=0;
std::cout<<"Please input the password(16 bit Max):";
while((int)(a=getch())!=13)
{
  if ((i++)==16)
  {
  std::cout<<"the password you input is to long,program exit";
  return 0; //这儿太简单了,你自己加强
 }
  *password++=a;
 putchar('*');
}
*password='\0';
password-=i;
if (checkpassword(password,"123")) std::cout<<endl<<"OK";else std::cout<<endl<<"wrong";
delete [] password;
return 0;
}
Bandry 2003-04-01
  • 打赏
  • 举报
回复
如果你在VC中用edit控件,那么可以改属性。如果是控制台程序,下面的可以做一下参考:
#include <stdio.h>
#include <conio.h>

void main()
{
char ch;
char pwd[21] = {0};
int i = 0;

do
{
ch = getch();
if (ch == 13)
continue;
putchar('*');
pwd[i++] = ch;
} while(ch != 13 && i < 20);

printf("\nYour password is: %s\n", pwd);

getchar();

return;
}

没有多做其他的控制,回车或者大于20字符就退出了
Accelerator 2003-04-01
  • 打赏
  • 举报
回复
#define PASSWORDMAXSIZE 20 //用来计数,例如密码最长不能超过20位等

char *strPW=NULL;//用来保存密码,全局或者局部

char * GetPW()
{
int count=0;
char ch;

while(ch=getch()!=13)//得到一个字符,如果用户按键不是回车就不断循环
{
if((ch>='a'&&ch<='z'||
(ch>='A'&&ch<='Z'||
(ch>='0'&&ch<='9'))
{//只需要a到z和0到9这些字符
cout >>"*";
strPW[count++]=ch;
}
}

if(count<=PASSWORDMAXSIZE)
strPW[count]='\0';
else//密码太长了,给点什么提示或者去掉超过的部分
strPW=NULL;

return strPW;
}
gruse 2003-04-01
  • 打赏
  • 举报
回复
#include<conio.h>
#include<stdio.h>
……
……
getch ( );
return 0;
同意micropentium6(小笨)和yuanhen(Thinkpad T40)。
yuanhen 2003-04-01
  • 打赏
  • 举报
回复
#include <conio.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
char a = 0;
while ((a = getch())!=13)
printf("*");

return 0;
}

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧