我是 C++ 的新手,我想提示用户准确输入 10 个字符,更多的字符将被忽略,例如:
- 请输入 10 个字符:
123412341234
- 您输入的是:1234123412
//而34会被忽略,因为他们输入的多于10
我现在到了这里:
string userInput;
cout << "Please enter 10 characters!\n";
cin >> userInput;
cout << "You entered: "<< userInput << endl;
谢谢大家。希望我尽可能准确。
最佳答案
只要求用户输入一个字符串,然后取其子串
std::string userInput;
std::cout << "Please enter 10 characters: ";
std::cin >> userInput;
if(userInput.length() > 10)
{
userInput = userInput.substr(0, 10);
}
std::cout << "You entered: " << userInput << std::endl;
关于c++ - 向用户询问指定数量的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53254476/