我想检查用户是否输入了有效的浮点数。奇怪的是,这似乎可以为
int
完成,但是当我更改我的代码以改为读取 float
时,它会给出错误消息
error: invalid operands of types 'bool' and 'float' to binary 'operator>>'
这是我的代码:
#include <iostream>
int main()
{
float num; // Works when num is an 'int', but not when its a 'float'
std::cout << "Input number:";
std::cin >> num;
if (!std::cin >> num){
std::cout << "It is not float.";
}
}