【问题标题】:How can I replace the input character with the "*" character?如何用“*”字符替换输入字符?
【发布时间】:2020-12-11 16:44:53
【问题描述】:

如何将输入字符替换为“*”字符?一切都应该在进入时发生(比如在网站上注册时输入密码)。 谢谢解答!!!

【问题讨论】:

标签: c++ cin


【解决方案1】:

最简单的方法,我是这样想的:

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

int main()
{
    std::string str;
    char ch;
    std::cout << "enter the text: ";
    while((ch = _getch()) != 13) //enter type
    {
        str+= ch;
        std::cout << '*';
    }
}

【讨论】:

  • 这可以在 Windows 上运行,但可能不适用于其他操作系统。
  • 我没有投反对票,但我认为至少有两个原因有人会投反对票。第一个原因是询问的人从未提到他们使用 MS windows 而不是 Android 或其他一些操作系统。第二个原因是没有代码描述。
猜你喜欢
  • 2021-06-21
  • 2017-10-02
  • 1970-01-01
  • 1970-01-01
  • 2013-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-16
相关资源
最近更新 更多