c++ - 如何限制通过 cin 输入的字符数?

标签 c++

我想使用 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/

相关文章:

c++ - 在 C++ 应用程序中可以使用什么未知网页编码的字符集检测器?

c++ - __attribute__((constructor)) 究竟是如何工作的?

c++ - QTreeView 在双击时编辑 UserRole 而不是 DisplayRole

c++在多个进程之间共享单个类对象

c++ - 容器类型转换

c++ - Qtconcurrent - 从 gui 线程启动第二个线程的简单方法

c++ - 如何确定 Qt 驱动器上有多少可用空间?

c++ - 使用模板的选择排序函数没有匹配的函数调用(C++)

c++ - strftime 不能正确显示年份

c++ - std::string::find 为取消引用的迭代器和等效的字符串文字返回不同的值