How to make cin >> not convert float to integer?
问题 I have the following simple code: #include <iostream> int main() { int a; std::cout << "enter integer a" << std::endl; std::cin >> a ; if (std::cin.fail()) { std::cin.clear(); std::cout << "input is not integer, re-enter please" <<std::endl; std::cin >>a; std::cout << "a inside if is: " << a <<std::endl; } std::cout << "a is " << a <<std::endl; std::cin.get(); return 0; } When I run the above code and input: 1.5 , it outputs: a is 1 . FYI: I compile and run the code with gcc 4.5.3. This means