C++ 在控制台下运行程序,我要进行密码验证

来源:百度知道 编辑:UC知道 时间:2024/06/09 10:39:35
密码事先已保存,但输入密码时显示的是掩码。如何编程实现???

//1L的正确但是好像退格符没处理
#include <conio.h>
#include <iostream>
#include <string>
using namespace std;

int main()
{
string password="";
char c;
cout<<"输入密码:";
while((c=_getch())!='\r'){
if(c=='\b'){
if( password == "") continue;//密码已经为空就忽略退格操作
password.erase(password.size()-1,1);//去掉最后一个字符
cout<<"\b \b";
}else{
cout<<'*';
password+=c;
}
}
cout<<"\n你输入的密码: "<<password<<endl;
return 0;
}

#include "stdafx.h"
#include "conio.h"
#include <iostream>
#include <string>

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string password="";
char c;
while((c=_getch())!=13)
{
password+=c;