我需要从标准输入读取密码,并且不想std::cin回显用户输入的字符...
如何禁用std :: cin的回声?
这是我目前使用的代码:
string passwd;
cout << "Enter the password: ";
getline( cin, passwd );
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种与操作系统无关的方法来做到这一点. 这里有一些方法可以在Windows和*nix中执行此操作.
我正在寻找一种限制可见用户输入的方法std::cin.
#include <iostream>
int main()
{
std::cout << "Enter your planet:\n";
string planet;
std::cin >> planet; // During the prompt, only "accept" x characters
}
Run Code Online (Sandbox Code Playgroud)
在输入earth之前,用户输入的内容或超过4个字符的任何其他单词:
Enter your planet:
eart
Run Code Online (Sandbox Code Playgroud)
这假设字符限制为4,请注意'h'缺少.一旦超出字符限制,控制台不会显示任何其他字符.这是在你按下回车键之前.
有点像在输入框中键入密码字段,但它只允许5个字符,因此输入任何其他字符都不会引起注意
更好的类比是maxlengthHTML中文本输入的属性.