我正在寻找一种限制可见用户输入的方法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中文本输入的属性.
我知道这是一个奇怪的问题,但是有没有办法从控制台读取以前的输入?
就像是:
The fox is brown // line 1
The duck is yellow // line 2
Here where the control is right now_ // but I want to read line 2
Run Code Online (Sandbox Code Playgroud)
PS:我正在使用窗户