#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Preventing erronous input


    Hi, im new to C. I have a problem, Im trying to write a program receiving input, consisting of two numbers, divided by doubledot (a:b). I would like to prevent program from working with nondigits.
    So far I was using this : if scanf("%d",&a) != 1 printf ("error"), then i modified it to work with the second number too : if scanf("%d:%d", &a, &b) != 2 printf ("error"). 1 doesnt work too. Thanks for any help.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    I just tried

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int a, b;
    
    	if (scanf("%d:%d", &a, &b) != 2)
    	{
    		printf ("error");
    		return 1;
    	}
    	
    	printf("%d, %d\n", a, b);
    
    	return 0;
    }
    and it worked.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    You need to put the condition inside parenthesis.

    Code:
    if (scanf("%d",&a) != 1) printf("error");
    if (scanf("%d:%d", &a, &b) != 2) printf("error");
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    Oh thank you so much. What a stupid mistake, i couldnt figure it out for at least 4 hours. Thanks.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    Yet, i still have some issues with it I didnt expect.
    So far it looks like this : if ( (scanf("%d:%d",&a, &b)).
    When i type in for example 86:abc, it shows next step : printf ("success") and then shows error: printf ("error"). If i type in abc:86 it shows error immediately, as it should. I need it to show only error output regardless of wrong input position.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    scanf returns the number of successful conversions. You're only testing that at least one conversion is successful. If you're expecting two conversions, then you need to test for scanf not returning a value of 2.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0
    Im already testing with the value 2, typed it wrong in my previous post. Updated : if ((scanf("%d:%d",&a, &b) != 2).
    And there was another error in my previous post, it behaves the other way around, it shows next step if i enter value abc:86, thats wrong. 86:abc does exactly what it should (error).
  14. #8
  15. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,074
    Rep Power
    1802
    You really need to post the code rather than a merely description of the code! :rolleyes: Use code tags too.


    Your problem is likley caused by the fact that console inoput is line buffered. When you convert "%d:%d" the scanf() function consumes only the digit-characters - anything remaining at the end of the line (including the <newline> itself) remains buffered. Because <newline> constitutes a line, the next call to scanf() will return immediately and fail because it is empty. The <newline> is still not consumed, so scanf() will continue to fail until you perform some input operation that clears the line buffer. I suggest:

    Code:
    check = scanf("%d:%d", &a, &b ) ;
    while( getchar() != '\n' ){ /* flushing junk in buffer */ }
    if( check != 2 )
    {
        // error
    }
    else
    {
        // success
    }

    Comments on this post

    • MauroEldritch agrees

IMN logo majestic logo threadwatch logo seochat tools logo