#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;
}