I want to check if the user input a valid float or not. Curiously, it seems that this can be done for an int
, but when I change my code to read a float
instead, it gives the error message
error: invalid operands of types 'bool' and 'float' to binary 'operator>>'
This is my code:
#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.";
}
}