the scanf function

This is a discussion on the scanf function within the C Programming forums, part of the General Programming Boards category; i have a code like this: Code: int store(double a[],int limit) { int i=0; while(i<limit&&scanf("%lf",&a[i])==1) i++; return i; } I ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    21

    the scanf function

    i have a code like this:
    Code:
    int store(double a[],int limit)
    {
         int i=0;
         while(i<limit&&scanf("%lf",&a[i])==1)
          i++;
         return i;
    }
    I only entered '1' , then I hit enter, this value should be read in. I hit enter the second time, the scanf() should return a false and jump out of the function. But it did not work in that way, it only jumps out until I entered a letter. could anyone tell me the reason? thanks.

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    scanf stop reading input when some part of the input read doesn't match its format string (AKA, when you press enter). It then returns the number of input items assigned, in your case, 1. That's why it doesn't break out of the loop.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    21
    Quote Originally Posted by Happy_Reaper
    scanf stop reading input when some part of the input read doesn't match its format string (AKA, when you press enter)
    but when I pressed the second time, scanf() should return '0' and it should break out. how can you explain this?

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    no, you're still in the scanf function until an input item doesn't match the format string.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by timhxf
    but when I pressed the second time, scanf() should return '0' and it should break out. how can you explain this?
    when you use some numeric format scanf ignores whitespaces, so your second \n symbol is just ignored as the first one. and scanf jsut continues to wait for the input.

    but when you enter the letter - it is not whitespace and is not ignored.
    But because this input is not consistent with the format - scanf exits and returnes 0
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    If you want the behaviour you described, use fgets() in conjunction with sscanf().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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