我如何告诉cin不允许输入空格?(C ++)

特里斯坦

运行此命令时,我希望不能以cin哪种格式说明符输入任何空格

他们的一切<string>让我做到了吗?

#include <iostream>
#include <string>
using namespace std;
int main(){
    string yy;
    cin >> string;

    /*when this runs I want to not be able to enter any spaces in `cin`
    what format specifier can I use for this? is their anything in      `<string>` that lets me do this?
    */
    return 0;
}
迪特玛·库尔(DietmarKühl)

您可以使用std::noskipws禁用在格式化输入之前自动跳过空格的操作:

if (std::cin >> std::noskipws >> yy) {
    std::cout << "read '" << yy << "'\n";
}

如果用户在字符串之前输入空格,则会出现错误,因为无法成功读取任何单词。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章