instead of atoi()/atof() try strtol()/strtod(). They take an additional parameter that tells where the conversion stops. With that info, you can determine if the input is valid.

Here's a demo:
Code:
#include <stdlib.h>
#include <string.h>

int main()
{
  char test1[]="1.0225";
  char test2[]="1.0225 with junk";
  char test3[]="All junk";
  
  double d;
  char *p;
  int len;

  len=strlen(test1);
  d=strtod(test1,&p);
  if(p==test1){
    printf("No conversion tool place. Garbage: %s\n",p);
  }else if((p-test1)<len){
    printf("partial converions took place.  Value: %lf, garbage: %s\n",d,p);
  }else{
    printf("Complete conversion took place: %lf\n",d);
  }

  len=strlen(test2);
  d=strtod(test2,&p);
  if(p==test2){
    printf("No conversion tool place. Garbage: %s\n",p);
  }else if((p-test2)<len){
    printf("partial converions took place.  Value: %lf, garbage: %s\n",d,p);
  }else{
    printf("Complete conversion took place: %lf\n",d);
  }

  len=strlen(test3);
  d=strtod(test3,&p);
  if(p==test3){
    printf("No conversion tool place. Garbage: %s\n",p);
  }else if((p-test3)<len){
    printf("partial converions took place.  Value: %lf, garbage: %s\n",d,p);
  }else{
    printf("Complete conversion took place: %lf\n",d);
  }


  return 0;
}
abubacker1 like this