c++控制台模拟输入密码出现*号

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

/**//**
 * 秘密在于conio.h中的getch()从键盘中读取字符时,并不会在屏幕上输出已经输入的字符,
 * 而用一个putch('*')来哄骗,代表已经输入一个字符
 * 怪不得这个头文件要叫conio.h, con的意思就有哄骗,看来就是由此而来.
 */

using namespace std;

int main() {
        char* password;
        char* passwordConfirm;

        int length = 4;
        password = new char[length + 1];
        passwordConfirm = new char[length + 1];

        char* p = NULL;
        int count = 0;

        cout << "Input password : ";
        p = password;
        count = 0;
        //fflush(stdin);
        while (((*p = getch()) != 13) && count < length) {
                // 这里不是'\n'(10), new line
                // 而是'\r'(13), reback. 即是按下回车键,好像这个东西是linux的.
                // 主要是与getch这个函数有关.
                putch('*');
                fflush(stdin);

                p++;
                count++;
        }
        password[count] = '\0';

        cout << endl << "Confirm the password : ";
        p = passwordConfirm;
        count = 0;
        //fflush(stdin);
        while (((*p = getch()) != 13) && count < length) {
                putch('*');
                fflush(stdin);

                p++;
                count++;
        }
        passwordConfirm[count] = '\0';

        cout << endl;
        if (strcmp(password, passwordConfirm) == 0) {
                cout << "The password is right." << endl;
                cout << password << endl;
        } else {
                cout << "Confirm password fail." << endl;
                cout << password << endl << passwordConfirm << endl;
        }

        return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值