#include <iostream.h>
#include <string.h>
#include <ctype.h>
bool IsNumber( const char *number ) {
bool isnumber = true;
int len = strlen( number );
if(len == 0 || len > 9) {
return !isnumber;
}
for( int i = 0; i < len; i++ )
{
if( isdigit( number[i] ) == 0 )
{
if( number[i] == '.' )
{
if( i + 1 > len - 1 )
{
isnumber = false;
break;
}
}
else if( number[i] == '+' || number[i] == '-' )
{
if( i - 1 >= 0 )
{
isnumber = false;
break;
}
}
else
{
isnumber = false;
break;
}
}
}
return isnumber;
}
void main()
{
char number[20] = {0};
cout << "Enter a number: ";
cin >> number;
if(!IsNumber(number))
cout << "This is not a valid number" << endl;
else
cout << "Thanks for entering a number!" << endl;
}