Validating and conversion of float
NickName:user2943407 Ask DateTime:2014-01-04T04:33:34

Validating and conversion of float

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

Answers
bolov 2014-01-03T20:48:11

int main() {\n float f;\n cin >> f;\n if (!cin.fail()) {\n cout << f << endl;\n } else {\n cout << \"Invalid input\" << endl;\n }\n // your code goes here\n return 0;\n}\n\n\nThe fail() function checks the failbit and badbit on the stream. failbit signals what you want, if there was a logical error on I/O operation (example: try to read a float, but couldn't parse the token to a float) and badbit signals if there was a read/writing error on i/o operation (could not read/write token at all).",


Dietmar Kühl 2014-01-03T20:51:57

When you use formatted reads combined with a check if everything is read you can determine if the [complete] value is a valid floating point number, e.g.:\n\nint main()\n{\n for (std::string test; std::cin >> test; ) {\n std::istringstream in(test);\n double value;\n if (in >> value >> std::ws && in.eof()) {\n std::cout << \"the string '\" << test << \"' is a valid floating point number\\n\";\n }\n else {\n std::cout << \"the string '\" << test << \"' is not a valid floating point number\\n\";\n }\n }\n}\n\n\nThe test does ignore whitespace surrounding the string. If that isn't desirable, the manipulator std::noskipws can be used and the use of std::ws can be omitted. Alternatively you can use strtod() do determine if the entire value represents a floating point number.\n\nWhat atof() is to simply see if the initial string of its argument is valid floating point number which is the case for the examples you quoted.",


More about “Validating and conversion of float” related questions

Validating and conversion of float

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 &gt;&gt; test; float n; n = atof(test.c_str(...

Show Detail

int to float conversion produces a warning?

It's surprising for me to see that even when the value can be converted, an int to float conversion always give a warning. Why is this? int i = 0; float f = 0; // warning here // I thought this ...

Show Detail

Float to Int vs Int to Float automatic conversion

I'm studying java by Herbert Schildt's "Java for beginners" book. It is said that, being a destiny variable compatible and sufficiently big to store an origin one, an automatic conversion is done. ...

Show Detail

Estimate or bound the float conversion error in Python

I am reading data from a csv file that contain numbers, and convert them to numpy float32 / float64. I'd like to get an estimation (or a bound) of the error that I have when converting. Ideally I'd...

Show Detail

Float to byte array conversion

We use float f= 3.5f ; BitConverter.GetBytes(f); It returns byte array of length 4. But I wonder the math behind this. Anyone will teach the math behind this conversion? Also double conversion i...

Show Detail

float invalid conversion / float unexpected behavior

I'm trying t wrap my mind around the float behavior (in C#). I have take notice of the floating point precision issue thingy. I want to convert an floating point string to an float, add + 1 to it and

Show Detail

How to change the precision of a float conversion?

I am trying to convert float value to 32 bit unsigned long value and facing the problem of loss of value. long v = (long) f; Here when f is 4294967295 ((2^32) -1). The conversion to long returns

Show Detail

float/int implicit conversion

I'm doing multiplication and division of floats and ints and I forget the implicit conversion rules (and the words in the question seem too vague to google more quickly than asking here). If I hav...

Show Detail

conversion to float in R

I have a set of Latitudes stored in VARCHAR format in mysql database- "[26.455359183496455,26.44229519242908,26.437069181137474,26.45489812668682]" I have imported them in R and I want to work with

Show Detail

Conversion of float in if statement

In following code: float x = 0.23; int z; z = x; if (x) printf("float %f will not be converted to 0!\n", x); if (z) printf("this will not print!\n"); I thought that float

Show Detail