c++コンソールシミュレーション入力パスワードに*番号が表示されます

2122 ワード

#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) {
                //     '
'(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; }