我想使用 cin
限制用户可以输入的字符数。例如,我可能希望将其限制为两个字符。我该怎么做?
我的代码是这样的:
cin >> var;
最佳答案
你可以使用setw()
cin >> setw(2) >> var;
http://www.cplusplus.com/reference/iostream/manipulators/setw/
Sets the number of characters to be used as the field width for the next insertion operation.
@chris 提供的工作示例:http://ideone.com/R35NN
关于c++ - 如何限制通过 cin 输入的字符数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10770870/