function that catches errors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jmf777
    New Member
    • Aug 2008
    • 19

    function that catches errors

    Hi here is my problem im adding to a program to catch errors.The program below catches errors like integers less than 0 or not equal to 1 but if I put in a number like 223e it doesn't catch the e as an error I know I am missing something in my error = sscanf() line but cant figure it out please help.

    Code:
    char line[MAXLINE];
    int error, n;
    
    do{
    	printf("Input a positive integer: ");
    	fgets(line, MAXLINE, stdin);
    	error = sscanf(line, "%d", &n) != 1 || n<= 0;
    	if (error)
    		printf("\nError: Do it again. \n");
    } while (error);
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    sscanf parses the input string until it reaches the end of the format string or the end of the input string. In your example you cite, the format string ends first. You want to verify that sscanf reached the end of the input string. Take a look at the %n conversion specifier.

    Does your program properly handle leading and trailing whitespace?

    Comment

    • jmf777
      New Member
      • Aug 2008
      • 19

      #3
      the program does handle whitespace correctly.

      Comment

      Working...