I have the following code:
int check_for_non_number(char *input, int lineNum)
{
errno = 0;
char *endptr;
printf("%s\n",input);
double xnum = strtod(input, &endptr);
// IF endptr FOUND A NON-VALID ENTRY AND THAT ENTRY IS NOT THE NEW LINE CHARACTER THEN ITS AN ERROR
if((*endptr) && (*endptr != '\n'))
{
return 1;
}
// We still want to process this number, even if it is represented as inf, so inform the user in the terminal and then return 0
else if (errno == ERANGE)
{
printf("\nOPERAND IS OUT OF RANGE ON LINE %d\n",lineNum);
return 0;
}
// ELSE IF endptr FOUND A NON-VALID ENTRY AND THAT ENTRY IS THE NEW LINE CHARACTER THEN RETURN 2 TO CHECK IF IT SHOULD BE A NEW LINE
else if((*endptr) && (*endptr == '\n'))
{
return 2;
}
else
{
return 0;
}
}
the value of input
came from strtok()
and was passed into the check_for_non_number
function...
The problem is when strtok reads "inf" from the text file, my function returns 1, implying the first argument was true... For clarity, the "inf" in the text file is located in the middle of the line in the text file so there is text before and after it and strtok is being used before and after it.
If anyone could shed some light on why strtod()
is not handling "inf" as an input to that would be greatly appreciated!