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.
Code://////////////////////////////////// // 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; continue; } } 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.