【发布时间】:2012-12-03 16:42:06
【问题描述】:
获取屏蔽密码输入的技术如下:
【问题讨论】:
标签: c++ windows console passwords
获取屏蔽密码输入的技术如下:
【问题讨论】:
标签: c++ windows console passwords
#include <iostream>
#include<windows.h> // for system("pause")
#include<conio.h> //for getch()
using namespace std;
int main()
{
char x[10];
cout<<"enter a password\n";
for(int i=0; i<10;i++){
x[i]=getch();
cout<<"*";
if(x[i]=='\r') //check if enter key is pressed
break;
else if(x[i]=='\b'){
if(i==0)
cout<<"\b"<<" "<<"\b";
else if(i>=1){
x[i-1]='\0';//make the previous byte null if backspase is pressed
i=i-2;
cout<<"\b"<<" "<<"\b\b"<<" "<<"\b";
}
}
}
cout<<endl<<"the password is :"<<x<<endl;
system("pause");
}
【讨论】:
getch() 已弃用。请改用_getch()。 (见msdn.microsoft.com/de-de/library/ms235446(v=vs.80).aspx)
getch()(或 _getch(),如果您愿意)是 Windows 函数,而不是 C++ 函数。
_getch()是 Windows,而不是 C++。但是,独立于操作系统的解决方案的需求在哪里?这个问题甚至用 Windows 标记。
windows 标签 - 因为解决方案显然是 Windows 的。它执行#include <windows.h>,#include <conio.h> 并使用getch/_getch。所有这些都是非标准的,显然不能在所有平台上工作。而且没有独立于平台的解决方案。