I have a int var; and using cin, but how to check if user input a float or give me a solution for this problem?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no easy way of doing this. Input via the
>> operatoris really not intended for interaction with humans who may enter the wrong thing. You will have to read the input as a string and then check it using a function like strtol, or your own hand-rolled code.Here’s an outline of an approach using strtol: