c语言 密码验证

密码为1234,怎样使输入的密码显示为"*",并且能与原密码进行比对,如果一致 则通过,不一致将重新输入.

用getch()非回显函数就可以了

#include <stdio.h>
#include <conio.h>
char mypw[10]="123";//预先设定的密码
int check(char a[])
{
int len=0;
while(len<10&&a[len]!=0x0d&&a[len]!='\0')
{
if(mypw[len]!=a[len]) return 0;
len++;
}
return 1;
}

int main()
{
char pw[10];//用户输入的密码
int i;
for(i=0;i<10;i++) pw[i]='\0';
int len=0;
printf("输入密码\n");
while(len<10&&0x0d!=(pw[len]=getch()))//用getch()非回显函数
{
len++;
printf("*");
}
printf("\n");
if(check(pw)) printf("密码正确");
else printf("密码错误");
getchar();
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-12-05
最多可以输入3次,3次错误会退出程序,初始密码和密码最大长度可以在程序中设定。
#include<stdio.h>
#include <conio.h>
#define N 10/*密码最大长度*/

void main()
{
char pass1[N]="1234";/*初始密码*/
char pass2[N]={0};
int i,n=3;
while(n--)
{
printf("请输入密码(最高3次):\n");
for(i=0;i<N;i++)
{
pass2[i]=getch();
if(pass2[i]==13)
{
pass2[i]=0;
break;
}
printf("*");
}
for(i=0;i<N;i++)
if(pass1[i]!=pass2[i])
{
printf("\n密码输入错误,请重新输入:\n");
break;
}
if(i==N)
{
printf("\n密码输入正确,按任意键继续。\n");
getch();
break;
}
if(n==0)
{
printf("密码输入错误3次,按任意键后退出!");
getch();
return;
}
}
printf("\n********欢迎光临XXXXX系统********\n");
/*你的主程序代码*/
getch();
}
第2个回答  推荐于2017-09-08
#include<stdio.h>
void main()
{
char a[10],i,password[10]="1234",j=1;
prinf("输入密码:\n");
while(j)
{scanf("%S",a);
for(i=0;a[i]!='\0';i++)
printf("*");
if(strcmp(password,a)==0)
{printf("通过\n");j=0;相应其他操作;}
else Printf("密码错误!重新输入!\n);
}
}本回答被提问者采纳
第3个回答  推荐于2017-09-21
#include<stdio.h>
void main()
{ int i,s;
  for ( i=0;i<3;i++ )
  { scanf("%d",&s);
    if ( s==12345 ) { printf("Welcome!\n"); break; }
    else if ( i==3 ) printf("Sorry!\n");
    else printf("Error!\n");
  }
}

第4个回答  2008-12-05
<!--#include file="Conn.asp"-->
<!--#include file="../Inc/Config.asp"-->
<!--#include file="inc/XXXXX.asp"-->
<%
'=================================
'
' 君彦网络科技有限公司(企业)网站管理系统
' kencm@163.com kencm@qq.com
'
'
' copyright(c)2007-2008 kencm.cn

'
'=================================
%>
<%
dim sql,rs
dim username,password,CheckCode
username=replace(trim(request("username")),"'","")
password=replace(trim(Request("password")),"'","")
CheckCode=replace(trim(Request("CheckCode")),"'","")
if UserName="" then
FoundErr=True
ErrMsg=ErrMsg & "<br><li>用户名不能为空!</li>"
end if
if Password="" then
FoundErr=True
ErrMsg=ErrMsg & "<br><li>密码不能为空!</li>"
end if
if CheckCode="" then
FoundErr=True
ErrMsg=ErrMsg & "<br><li>验证码不能为空!</li>"
end if
if session("CheckCode")="" then
FoundErr=True
ErrMsg=ErrMsg & "<br><li>你登录时间过长,请重新返回登录页面进行登录。</li>"
end if
if CheckCode<>CStr(session("CheckCode")) then
FoundErr=True
ErrMsg=ErrMsg & "<br><li>您输入的确认码和系统产生的不一致,请重新输入。</li>"
end if
if FoundErr<>True then
password=md5(password)
set rs=server.createobject("adodb.recordset")
sql="select * from Bs_User where password='"&password&"' and username='"&username&"'"
rs.open sql,conn,1,3
if rs.bof and rs.eof then
FoundErr=True
ErrMsg=ErrMsg & "<br><li>用户名或密码错误!!!</li>"
else
if password<>rs("password") then
FoundErr=True
ErrMsg=ErrMsg & "<br><li>用户名或密码错误!!!</li>"
else
rs("LastLoginIP")=Request.ServerVariables("REMOTE_ADDR")
rs("LastLoginTime")=now()
rs("LoginTimes")=rs("LoginTimes")+1
rs.update
session.Timeout=SessionTimeout
session("Name")=rs("username")
session("Aleave")="check"
rs.close
set rs=nothing
call CloseConn()
Response.Redirect "Default.asp"
end if
end if
rs.close
set rs=nothing
end if
if FoundErr=True then
call WriteErrMsg()
end if
call CloseConn()

'****************************************************
'过程名:WriteErrMsg
'作 用:显示错误提示信息
'参 数:无
'****************************************************
sub WriteErrMsg()
dim strErr
strErr=strErr & "<html><head><title>错误信息</title><meta http-equiv='Content-Type' content='text/html; charset=gb2312'>" & vbcrlf
strErr=strErr & "<link href='style.css' rel='stylesheet' type='text/css'></head><body>" & vbcrlf
strErr=strErr & "<table cellpadding=2 cellspacing=1 border=0 width=400 class='border' align=center>" & vbcrlf
strErr=strErr & " <tr align='center'><td height='22' class='title'><strong>错误信息</strong></td></tr>" & vbcrlf
strErr=strErr & " <tr><td height='100' class='tdbg' valign='top'><b>产生错误的可能原因:</b><br>" & errmsg &"</td></tr>" & vbcrlf
strErr=strErr & " <tr align='center'><td class='tdbg'><a href='Login.asp'><< 返回登录页面</a></td></tr>" & vbcrlf
strErr=strErr & "</table>" & vbcrlf
strErr=strErr & "</body></html>" & vbcrlf
response.write strErr
end sub

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网