Thread: Problem with scanf

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    1

    Problem with scanf

    I need to get input a double, and if the user will press "ENTER" it will continue.. when I use scanf(%lf,&num); the program waits that there will be some input, and if I press "ENTER" it doesn't continue.. (I'm not using files..)

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what you want is that if the user DOES NOT enter any numeric value, some "previous" or "default" value is used, is that about right?

    You could use fgets() to read a string in, then if the string isn't empty [note that fgets includes the newline in the string - so an empty string is "\n"], convert it to a double [using e.g. strtod() or sscanf()].

    If that's not what you want, please clarify what you DO want.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    hmm i think you forgot the quotation marks around %lf, so it should be:
    scanf ("%lf" , &num)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with RPC and scanf
    By Ricardo_R5 in forum C Programming
    Replies: 11
    Last Post: 01-08-2007, 06:15 PM
  2. Problem with scanf float..
    By aydin1 in forum C Programming
    Replies: 6
    Last Post: 03-06-2005, 01:35 PM
  3. scanf problem
    By gsoft in forum C Programming
    Replies: 3
    Last Post: 01-05-2005, 12:42 AM
  4. problem with looping scanf
    By crag2804 in forum C Programming
    Replies: 6
    Last Post: 09-12-2002, 08:10 PM
  5. scanf problem
    By Flikm in forum C Programming
    Replies: 2
    Last Post: 11-05-2001, 01:48 PM