Validate Float Logic?
I spent the evening trying to figure a good way to validate user input of a floating point number from a string . The following code snippet works, but seems complicated to me. I am sure there is probably a more efficient way. If so could someone please give me some constructive criticism or pointers.
// Validate floating point number.//
int Decimal = 0;
for (int i = 0; i < nLen; i++ )
if ( !isdigit(pbuffer[i]))
if ( Decimal == 0 )
if( pbuffer[i] == '.' )
Decimal = 1;
MessageBox (NULL, TEXT ("Enter a floating point number."),
TEXT ("Invalid input!"), MB_ICONINFORMATION) ;
return TRUE; // break out of switch statement.
// pbuffer is valid so do calculations.
This might help.
EDIT: The link only works sometimes. Once you get in there, look up strtod if it doesn't automatically bring you to it.
Thanks for the info.
Thanks for the info. I'll try it out.