我需要从标准输入读取密码,并且不想std::cin回显用户输入的字符...
如何禁用std :: cin的回声?
这是我目前使用的代码:
string passwd;
cout << "Enter the password: ";
getline( cin, passwd );
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种与操作系统无关的方法来做到这一点. 这里有一些方法可以在Windows和*nix中执行此操作.
我正在编写一个代码来接收密码输入.下面是我的代码...程序运行良好,但问题是除了数字和字母字符之外的其他键也被读取,例如删除,插入等等我能否知道如何避免它?TQ ...
string pw="";
char c=' ';
while(c != 13) //Loop until 'Enter' is pressed
{
c = _getch();
if(c==13)
break;
if(c==8)
{
if(pw.size()!=0) //delete only if there is input
{
cout<<"\b \b";
pw.erase(pw.size()-1);
}
}
if((c>47&&c<58)||(c>64&&c<91)||(c>96&&c<123)) //ASCii code for integer and alphabet
{
pw += c;
cout << "*";
}
}
Run Code Online (Sandbox Code Playgroud)