小编Djb*_*och的帖子

C++ Integer 后跟 char 被接受为输入

我正在使用一个函数来检查我的输入是否仅为整数:

    int input;
    while (true)
    {
        std::cin >> input;
        if (!std::cin)
        {
            std::cout << "Bad Format. Please Insert an integer! " << std::endl;
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            continue;
        }
        else
            return input;
    }
Run Code Online (Sandbox Code Playgroud)

但是,当输入一个整数后跟一个字符时,例如。3s,整数被接受并打印消息。

我怎样才能确保这种格式的输入不被接受,也不以4s 5 的形式输入,所以当整数出现在空格之后。

c++ validation cin

1
推荐指数
1
解决办法
62
查看次数

标签 统计

c++ ×1

cin ×1

validation ×1