Thread: checking if int when using scanf. Help!

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    9

    checking if int when using scanf. Help!

    Hi, I'm writing a program that requires me to use scanf. Is there a way to check if the user entered a int? Any help would be appreciated.

    I'd be happy to cause a loop for unexpected input because then I could stop the loop with a flush and the user would be asked again to input the proper data. My problem is that after the first time it wont loop for a float. It only loops the first time.
    Last edited by xtrmi; 09-18-2008 at 07:52 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Check the return value.
    Quote Originally Posted by scanf man page
    RETURN VALUES
    These functions return the number of input items assigned,
    which can be fewer than provided for, or even zero, in the
    event of a matching failure. Zero indicates that, while
    there was input available, no conversions were assigned;
    typically this is due to an invalid input character, such
    as an alphabetic character for a `%d' conversion. The
    value EOF is returned if an input failure occurs before
    any conversion such as an end-of-file occurs. If an error
    or end-of-file occurs after conversion has begun, the num-
    ber of conversions which were successfully completed is
    returned.
    Next time you'll look up the help page on your own, using the internet, yes?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    9
    I'm just in my freshman year of ECE. even with your post I can't figure out how this is going to help me if someone inputs 34er%^&&*. Will this return value tell me it was a success because scanf read 34 or will it return something to indicate there was input it couldn't read? Thanks.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    %d%c perhaps?
    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.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    there is a library function called isdigit(). you have to include string.h header file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  4. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  5. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM