I am writing a program where I have to verify that the inputted value by the user is a valid float number. So, I've tried this method:
string test;
cin >> test;
float n;
n = atof(test.c_str());
cout << n << endl;
But it doesn't seem to do the job; when it does the conversion, the numbers after the .
are lost. How can I validate that a valid floating point number is used in C++ (not 11)?
Examples:
INPUT: 32.e
OUTPUT: 32 (should be error)
INPUT: 2a.1234
OUTPUT: 2 (should be error)
Copyright Notice:Content Author:「user2943407」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/20912238/validating-and-conversion-of-float