(c++) 如何编程实现:密码的输入?

结果类似于:请输入密码:**************按任意键继续...(清屏)(自定义的程序模块)按任意键继续...... 结果类似于:

请输入密码:**************
按任意键继续...
(清屏)
(自定义的程序模块)
按任意键继续...
展开
 我来答
四舍五不入6
高粉答主

2020-03-12 · 醉心答题,欢迎关注
知道答主
回答量:147
采纳率:100%
帮助的人:2万
展开全部

可以参考下面的代码:

#include <cstring>

#include <cstdio>

cout<<"Please enter password: ";

gets(user);

if(strcmp(user,"password"/* 随便输入一个初始密码*/))cout<<"error";

else {……}

扩展资料:

C++参考函数

int isupper(int ch)  若ch是大写字母('A'-'Z')返回非0值,否则返回0

int isxdigit(int ch) 若ch是16进制数('0'-'9','A'-'F','a'-'f')返回非0值,否则返回0

int tolower(int ch)  若ch是大写字母('A'-'Z')返回相应的小写字母('a'-'z')

int toupper(int ch)  若ch是小写字母('a'-'z')返回相应的大写字母('A'-'Z')

参考资料来源:百度百科-C++

tattackor
推荐于2017-09-15 · TA获得超过3.5万个赞
知道大有可为答主
回答量:5083
采纳率:94%
帮助的人:857万
展开全部

1、主要使用getch函数即可实现不显示字符,进行密码输入。

函数用途:从控制台读取一个字符,但不显示在屏幕上
函数原型:int getch(void)
返回值:读取的字符
getch与getchar基本功能相同,差别是getch直接从键盘获取键值,不等待用户按回车,只要用户按一个键,getch就立刻返回, getch返回值是用户输入的ASCII码,出错返回-1。输入的字符不会回显在屏幕上。getch函数常用于程序调试中,在调试时,在关键位置显示有关的结果以待查看,然后用getch函数暂停程序运行,当按任意键后程序继续运行。
2、例程:

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
int chcode() {
    char pw[50],ch;
    char *syspw = "abc"; // 原始密码
    int i,m = 0; 
    printf("请输入密码:"); 
    while(m < 3) {
        i = 0;
        while((ch = _getch()) != '\r') {
            if(ch == '\b' && i > 0) {
                printf("\b \b");
                --i;
            }
            else if(ch != '\b') {
                pw[i++] = ch;
                printf("*");
            }
        }
        pw[i] = '\0';
        printf("\n");
        if(strcmp(pw,syspw) != 0) {
            printf("密码错误,请重新输入!\n");
            m++;
        }
        else {
            printf("密码正确!\n");
            system("pause");
            return 1;
        }
    }
    printf("连续3次输入错误,退出!\n");
    system("pause");
    return 0;
}
int main() {
    int login = chcode();
    if(login) printf("登录成功!\n");
    else printf("登录失败!\n");
    return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友fbdce02
推荐于2017-09-15 · 超过46用户采纳过TA的回答
知道小有建树答主
回答量:109
采纳率:0%
帮助的人:108万
展开全部
#include<stdlib.h>
#include<conio.h>
#include<iostream>
#include<cstring>

using namespace std;

int main()
{
int i;char ch;
char str[20];//用来保存密码
char mima[6]={"12345"};//设置初始密码为12345

while(1)
{
i=0;
system("cls");
cout<<"输入密码:";
ch=getch() ;
while(ch!=13) //回车的那个什么码
{
if(ch==8&&i>0)//退格键的ask码
{
i--;
str[i]='\0';
system("cls");//清屏 重新输出*
cout<<"输入密码:";
for(int j=0;j<i;j++)
{
cout<<"*";
}
ch=getch();
}
else
{
printf("*");
str[i]=ch;
i++;
ch=getch();
}
}
str[i]='\0';
if(!strcmp(str,mima))
{
system("cls");
cout<<"正确";
break;
}
else
{
cout<<"\n密码错误!!";
getch();

}
}

cout<<"\t密码是:"<<str;

getch();

return 0;
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
domo5231
2008-08-10 · TA获得超过904个赞
知道小有建树答主
回答量:216
采纳率:0%
帮助的人:130万
展开全部
我有个比较简单的办法,运算量极小。
#include <cstring>
#include <cstdio>

cout<<"Please enter password: ";
gets(user);
if(strcmp(user,"password"/* 随便输入一个初始密码*/))cout<<"error";
else {……}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
ㄜ逍遥ㄆ
2008-08-05 · TA获得超过219个赞
知道小有建树答主
回答量:210
采纳率:0%
帮助的人:168万
展开全部
#include<stdio.h>
#include<conio.h>
void main()
{
int i = 0; char mima[20];
char ch;
while(ch = getch(), ch != '\r')
{
printf("*");
mima[i++] = ch;
}

printf("您输入的密码为:");
for(int j = 0; j < i; j++)
printf("%c", mima[j]);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(6)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式