Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ways to validate float point datatypes?

Status
Not open for further replies.

Sidro

MIS
Sep 28, 2002
197
US
Hi,
What are some of the ways to validate floating point numbers from a user input? I looked into the functions in c++
but it includes the scientific notation "e", which I dont like.Thanks i advance.
 
If you don't want to write your own (and very fast;) double constant parser (very interesting job;), try this approach:
Code:
bool Validate(const char* s, double& x)
{
  if (!s || !*s)
    return false;
  istrstream val(s,strlen(s));
  val >> x; // let C++ library works...
  return val.good();
}

inline bool Validate(const string& s, double& x)
{
  return Validate(s.c_str(),x);
}

bool Validate(const char* s, double* px = 0)
{
  double x;
  return Validate(s,px?px:&x);
}

inline bool Validate(const string& s, double* px = 0)
{
  return Validate(s.c_str(),px);
}
Alas, old good atof (from <cstdlib>) returns undefined value in overflow case.
C++ streams returns fail() when bad constant occured, but direct stream poll (then clear) is too cumbersome.
Yes, it's not a fastest validator, but an user console is not the fastest input device too...
We need only one Validate() function, but four overloaded functions better than one...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top