scanf problem

This is a discussion on scanf problem within the C Programming forums, part of the General Programming Boards category; when one scanf returns 0 it doesnt enter to none of the other scanf in the program how to fix ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    scanf problem

    when one scanf returns 0
    it doesnt enter to none of the other scanf in the program

    how to fix it??

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You have 1450 posts - how do you still not understand how this works? Seriously - stop assuming that the problem is C. The problem is that you don't use C correctly.

    POST YOUR CODE! I'll bet you anything there is an error in your code. There is no way for us to know what that error is unless you post your code. scanf returns 0 when it matches 0 items. It probably did exactly what you told it to do. You probably told it to do the wrong thing.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If scanf() doesn't match any items, it leaves the input stream untouched. So if you asked it to read a number, and it couldn't, then it still won't be able to read a number again the next time you ask it, unless you clear the junk out from the input stream. See the FAQ for details (How do I clear the input buffer, or some such).
    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.

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how do i clear the junk from the input stream??
    i tried g=getchar();

    still it skips the other scanf 's

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Try this.
    Code:
    int c;
    while((c = getchar()) != '\n' && c != EOF) {}
    It's in the FAQ, as I mentioned. Cprogramming.com FAQ > Flush the input buffer
    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.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can ignore an element in a scanf with the asterisk (*) flag:
    Code:
    int x;
    scanf("%d%*c",&x);
    Notice there is no variable to receive the second element (%*c), because the asterick (*) means to read this in but ignore it (just like you would use getchar).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    post what you have and someone will point out how scanf() should be used

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by sean View Post
    You have 1450 posts - how do you still not understand how this works? Seriously - stop assuming that the problem is C. The problem is that you don't use C correctly.

    POST YOUR CODE! I'll bet you anything there is an error in your code. There is no way for us to know what that error is unless you post your code. scanf returns 0 when it matches 0 items. It probably did exactly what you told it to do. You probably told it to do the wrong thing.
    Leave him alone, he's writing the FAQ for his book

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Oh no, not again.....
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    .. My God. Another example that, in most cases, posts are meaningless indicators of potential user knowledge.
    Last edited by hauzer; 06-09-2009 at 04:38 PM.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by zacs7 View Post
    Leave him alone, he's writing the FAQ for his book
    Hmm, I should write a book on programming. Might be an easy way to make money... especially if I can convince braindead professors to use it in their courses.

  12. #12
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by hauzer View Post
    .. My God. Another example that, in most cases, posts are meaningless indicators of potential user knowledge.
    It is very meaningful, if someone has such a high post count and asks such stupid questions it means they're retarded
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

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, 05:15 PM
  2. Problem with scanf float..
    By aydin1 in forum C Programming
    Replies: 6
    Last Post: 03-06-2005, 12:35 PM
  3. scanf problem
    By gsoft in forum C Programming
    Replies: 3
    Last Post: 01-04-2005, 11:42 PM
  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, 12:48 PM

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