我正在使用一个函数来检查我的输入是否仅为整数:
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 的形式输入,所以当整数出现在空格之后。