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 \b" << flush;
str.erase(str.length() - 1);
}
else {
cout << "*" << flush;
str += init;
}
}
return str;
}
望点赞