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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validate input data type

Status
Not open for further replies.

wwworlds

Technical User
Jun 4, 2003
2
CA
i'm new to C. does anyone knows if the following syntax validate input data type ?
i'm trying to validate if the input - float/integer (a pointer that send to a function) is a float or an integer.......
if input is an integer....return the sum of two input
else....doesn't return

if (int isdigit(*ptr1)) || (int isdigit(*ptr2)) ...........seems doesn't work this way
thanks
 
No way to tell from that small fragment of code - you need to paste the whole function.
An example of how you call it would probably help as well

 
i have modified my code a little bit, still doesn't validate correct. details as follows
int success;
int sum;
int SumOnlyWholeNums(float *ptr1, float *ptr2, int *ptr_sum);

int main(void)
{
float FL1 = 10.0f, FL2 = 5.0f;
int *ptr_sum;
float *ptr1;
float *ptr2;
ptr1 = &FL1;
ptr2 = &FL2;
ptr_sum = ∑

printf("Please enter two numbers : ");
scanf("%f %f", &FL1, &FL2);

SumOnlyWholeNums(&FL1, &FL2, &sum); //pass to the function

if (success == 1) //if it's (an integer) calculation
printf("The sum is %d\n", sum);
else
printf("%.2f and %.2f must be whole values\n", FL1, FL2);
return(0);
}

int SumOnlyWholeNums(float* ptr1, float* ptr2, int *ptr_sum)
{
if (*ptr1 != 1 || *ptr2 != 1)
{
success = 0;
return success; //do not perform calculation
}
else
{
success = 1;
sum = *ptr1 + *ptr2;
return success, sum;
}
}
 
My understanding is that you are accepting two float types and wnat to tell whether these are whole numbers of contain some fractional part. In this case the logic you want is as follows:

int is_it_a_whole_number(float * fptr)
{
float frac_part;
float int_part;

/* Get the part prior to the decimal point */
int_part=fabs(*fptr);
/* Take that away from the entire number to get the
fractional part */
frac_part=*fptr-int_part;
/* test the fractional part equal to zero */
if(frac_part!=0)
return 1; /* We have a fraction */
else
return 0; /* We have no fraction */
}

Obviously this can be made simpler but I left it as a step by step thing to show what I was doing.

You could end up with something like

int is_it_a_whole_number(float * fptr)
{
if(*fptr-fabs(*fptr)!=0)
return 1; /* We have a fraction */
else
return 0; /* We have no fraction */
}
 
I really don't like testing floating point numbers to see if they are integers. The difficulty is that tiny, tiny little errors creep into floating point arithmetic. They are usually unnoticeable because they only affect the 15th decimal place, and you'll normally be rounding them off somewhere. But it's a real pity if something that ought to be an integer gets treated as a floating point number because it's 1.0000000000001 instead of a clean 1. You might get away with this, or you might have disasters. I've usually met it in excel spreadsheets, where testing if one cell equals another can be very frustrating where floats are involved. The two cells clearly ARE equal, but the wretched program thinks otherwise... (and it's right).

Better, test if difference between x and fabs(x) is less than some fraction of x dependant on your precision.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top