C++輸入密碼回顯星號

C++輸入密碼回顯星號

password.cpp

//本代碼不支持除退格外其它特殊按鍵!!! 
#include <iostream>
#include <string>
#include "password.h"
using namespace std;
string pwd = "\0";
int main(int argc,char *argv[]) {
 pwd = getpassword();
 cout << "Your password is:\n" << pwd << endl;
 return 0;
}

password.h

//本頭文件不支持除退格外其它特殊按鍵!!!
#include <conio.h>
#include <windows.h>
#include <string>
#include <iostream>
using namespace std;
string getpassword() {
 string str = "";
 char init = '\0';
 for (;;) {
  init = getch();
  if (init == VK_RETURN) {
   cout << endl;
   break;
  }
  else if (init == VK_BACK) {
   cout << '\b' << flush;
   str += '\b';
  }
  else {
   cout << "*" << flush;
   str += init;
  }
 }
 return str;
}

望點贊

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章