Atof function returns 0 when there is some error during conversion AND when input string is "0", "0.0", ".0" or whatever. That's what I understand form help. How can I know whether user really inputted zero or there was an error?
Printable View
Atof function returns 0 when there is some error during conversion AND when input string is "0", "0.0", ".0" or whatever. That's what I understand form help. How can I know whether user really inputted zero or there was an error?
well, Im not sure, but atof might set errno to some value if an error occured. I'd check myself, but I recently had some computer trouble, and It would be just a lot of hassle. Try this
Code:#include <stdio.h>
#include <whatever header file atof is in>
#include <iostream.h> //I guess this is the C++ board,
// so i had better include this
main()
{
int err = errno;
atof( "aslfkhas;lf" ); // junk data to see if errno changes
if( err != errno )
cout << "atof sets errno on error!"
else
cout << "nope";
return 0;
}
Why not use strcmp() to test for it?
if(atof(string) || !strcmp(string,"0")
{
/// Process good Data
}
else
{
/// print an error, set flag, etc.
}
Thus, typing "0" into the string will pass the "!strcmp(string,"0")"
test whereas hitting "enter" or typing garbage (which passes the "atof(string)" test ) will not.
Why not use strcmp() to test for it?
if(atof(string) > 0 || !strcmp(string,"0")
{
/// Process good Data
}
else
{
/// print an error, set flag, etc.
}
Thus, typing "0" into the string will pass the "!strcmp(string,"0")"
test whereas hitting "enter" or typing garbage (which passes the "atof(string)" test ) will not.
And what if user enters 0.0, 0e-0, .000 or something? I'd like this all to work, no restrictions in how he specifies zero.