input validation making sure only an int is entered

This is a discussion on input validation making sure only an int is entered within the C Programming forums, part of the General Programming Boards category; Hey guys how would i make sure that only an int is being entered and not a char. Im using ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    input validation making sure only an int is entered

    Hey guys how would i make sure that only an int is being entered and not a char. Im using sscanf() as the function to enter input as an integer.

    But the thing is it compiles but when i run it and i press a character such as the letter 'c' the program goes bolistic and goes into an inifinate loop how can i stop this with some validation to make sure that only an int is entered?

    Code:
    fscanf(stdin, "%d", &input);
    
    if(isalpha(input))
    {
    
        printf("you have entered a char please enter an integer");
    }
    
    else
    {
    
        printf("You have correctly inputed the integer");
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( fscanf( stdin, "%d", &input ) == 1 )
    {
        ...integer was stored in 'input'...
    }
    else
    {
        ...it wasn't, better clean out your input stream...
    }
    Also consider reading the FAQ on getting input.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    And here is a function I wrote to clean out the input stream
    Code:
    /* begin of flush_stdin 05-8-31 19:30 */
    void flush_stdin( void )
    {
    	if ( !feof(stdin) ) {
                    int c;
    		while( ( c=getchar() ) != '\n' && c != EOF )
    			;
    	}
    } /* end of flush_stdin */
    Last edited by Antigloss; 09-01-2005 at 12:28 AM.

  4. #4
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    thats kewl i understood it thanks for the help guys!

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >if ( !feof(stdin) ) {
    There's no need to call feof. If getchar returns EOF and the loop will terminate as expected without this extra unnecessary test. Though you can more easily display your intentions by reversing the loop conditions:
    Code:
    void flush_stdin ( void )
    {
      int c;
    
      while ( ( c = getchar() ) != EOF && c != '\n' )
        ;
    }
    My best code is written with the delete key.

  6. #6
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    >There's no need to call feof.

    It do need to call feof. Let's look at the follow code first
    Code:
    int i;
    scanf("%d", &i);
    flush_stdin();
    if the user input nothing before pressing ^D(unix like os) or ^Z(windows), and there is no such a test in function flush_stdin, then the program will stop and wait for the user to press ENTER. By calling feof to test stdin, we can avoid this unexpected thing

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >It do need to call feof.
    Barring the fact that the standard makes certain guarantees about the behavior of getchar when the stream is in an error state, I'll humor you. So for the remainder of this post I'll pretend that your example will always behave as you think it does.

    No, it does not need to call feof. That's an error condition that the calling code needs to handle. If the calling code is written properly, the feof call in flush_stdin would be extraneous and unnecessary, therefore it does not need to call feof. Now, if you like having bad code that hides legitimate errors from you, by all means do it your way and be blissfully ignorant. I suppose then you can turn into one of those bitter people that talk about how unsafe C is because they have no idea how to cover their asses against edge cases and got burned.
    My best code is written with the delete key.

  8. #8
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    > the standard makes certain guarantees about the behavior of getchar when the stream is in an error state

    What kind of behavior does it guarantee? I'm not experienced in C, so I don't know it. Please tell me.

    > I'll pretend that your example will always behave as you think it does.

    When will it behave not as I think?

    > That's an error condition that the calling code needs to handle.

    well...if in this case, i'll have to write
    Code:
    if ( scanf("%d", &i) != EOF ) {
        flush_stdin();
    }
    each time i call flush_stdin. that's boring.
    Last edited by Antigloss; 09-01-2005 at 08:06 PM.

  9. #9
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    > the standard makes certain guarantees about the behavior of getchar when the stream is in an error state

    What kind of behavior does it guarantee? I'm not experienced in C, so I don't know it. Please tell me.

    > I'll pretend that your example will always behave as you think it does.

    When will it behave not as I think?

    > That's an error condition that the calling code needs to handle.

    well...if in this case, i'll have to write
    Code:
    if ( scanf("%d", &i) != EOF ) {
        flush_stdin();
    }
    each time i call flush_stdin. that's boring.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Quote Originally Posted by Antigloss
    well...if in this case, i'll have to write
    Code:
    if ( scanf("%d", &i) != EOF ) {
        flush_stdin();
    }
    each time i call flush_stdin. that's boring.
    Boring code is good code. Take your example to the next level:
    Code:
    if (scanf("%d", &i) == 1) {
      /* Lots of code using i */
      flush_stdin();
      /* More input */
    }
    Since it's never a bad idea to check input for success, that idiom makes your code boring, but way more robust.
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21