/*This code is designed to validate a signed real number. It emulates the way
that the compiler uses the BNF notation*/
#include
#include
#include
#include
void validate();
char string[20] = { '\0' }; //Where the input will be stored
//Defiination of decimal digits
char digit[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
enum flag{ yes, no };
void main()
{
clrscr();
cout << "Enter the string : ";
cin >> string;
validate();
getch();
}
void validate()
{
flag sign = no;
flag point = no;
flag found = no;
int i;
//Validates the condition that the sign comes first
if( string[0] == '+' || string[0] == '-' )
{
sign = yes;
}
else
{
cout << "\a sign(+/-) must come first for signed number";
return;
}
//Validates that input has a decimal point
for( i=0; i { if( string[i] == '.' ) { point = yes; break; } } if( point == no ) { cout << "\aString must have point for floating number"; return; } //Validates that all other characters are digits for( int j=1; j< ( strlen(string) - 1 ); j++ ) { found = no; //Matches input character with each digit for( int k=0; k<10; k++ ) { if( string[j] == digit[k] ) { found = yes; break; } } if( found == no && j!=i) { cout << "\aNon integer found at "<< j; return; } } cout << "\n Valid String"; }