I'm trying to check validity of user input like so:
#include<iostream>
#include<cstdlib>
int main(){
int val =0;
std::cout<<"222 to quit"<<std::endl;
while(true){
if(std::cin>>val)
{
if(val == 222)
break;
std::cout<<"Valid Input"<<std::endl;
}
else
{
std::cout<<"Invalid Input"<<std::endl;
std::cin.clear();
fflush(stdin);
}
}
system("pause");
}
basically when the input is an int, it should be declared valid; and invalid otherwise. It works perfectly for int and char: int and char inputs But when a float is provided, it shows two outputs- both valid and invalid! float input Can someone help me understand what's happening here?
I expected it to say 'invalid' input for floats.