Thread: scanf() error checking

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    44

    scanf() error checking

    Currently if this code is run and the user inputs the number 1 or 2 followed by any amount of characters it will take the value 1 or 2 and run. I would like ths case (Number,Char) to be returned as an error. How might this be achieved?


    Code:
    do
             {
                    printf("Please Enter Your Choice From The Menu: ");
                    error = scanf("%d",&menu_choice);
                    fflush(stdin);
             }      while ( error == 0 || menu_choice < 1 || menu_choice > 2 );
    This code is asking the user for a menu selection of either 1 or 2, scaning it into the int menu_choice and then checking it for errors.

    I know fflush(stdin); is not the correct way to flush the input but i am willing to run with it.


    Thanks in advance!
    Dangerous Dave

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I would like ths case (Number,Char) to be returned as an error.
    It's usually better to handle such things gracefully. Returning an error is tantamount to saying "I can't fulfill my promise because something is stopping me". If you have the correct menu option, the function can fulfill its promise, so you have an unusual condition that should be handled, not an error.

    However, if you really want to return an error, grab the next character after your number. If scanf returns a real error or the number is not valid you can die with an exception, otherwise everything is still kosher so you check the next character from the stream. If that character is a newline then everything is just fine. Otherwise there is extraneous data, which matches your error condition:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int i;
      char c;
    
      if ( scanf ( "%d%c", &i, &c ) != 2 )
        printf ( "Some other error\n" );
      else if ( i != 1 && i != 2 )
        printf ( "Invalid option\n" );
      else if ( c != '\n' )
        printf ( "Extraneous input\n" );
      else
        printf ( "Good\n" );
    
      return 0;
    }
    >I know fflush(stdin); is not the correct way to flush the input but i am willing to run with it.
    That's your problem then. Just be sure to comment your use of it when posting code here so that we don't waste our time telling you something you already know but choose to ignore.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Prelude, it looks like you wasted your time two years ago as well.

    He doesn't want to learn, and seems to enjoy falling into the same old traps over and over.
    So for one, I'll leave him be from now on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Prelude, it looks like you wasted your time two years ago as well.
    Wow, what an old thread. When I first started coming here I lacked fundamental knowledge despite working professionally. Scary, isn't it?

    >So for one, I'll leave him be from now on.
    Good idea.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    That is an old post! I was doing a course on Computer Sicence 2 years ago and i droped out and took a year off. I just checked to see if my old login name was still working and it was!

    Well Im working your sugestion into my program at the moment and it was a great help.

    Thanks,

    And i hope ur not serious about ignoring me cos i forgot all the stuff i had learned b4.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >And i hope ur not serious about ignoring me
    Not really. I only ignore real idiots. You haven't given me any reason to think that you're a real idiot, so you're safe.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    44
    This is what im going to run with now i think. Thanks For all the help guys.

    Code:
    do
    {
        printf("Please Enter Your Choice From The Menu: \t");
        error = scanf("%d%c",&menu_choice, &c);
        fflush(stdin);      /* I know it's not correct */
    }   while ( error != 2 || menu_choice < 1 || menu_choice > 2 || c != '\n' );
    Last edited by Dangerous Dave; 11-26-2003 at 05:50 PM.
    Dangerous Dave

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You could always just consult the FAQ with regards to reading numbers effectively. Input buffer flushing is in there as well, if you'd only look for it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  3. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM