when one scanf returns 0
it doesnt enter to none of the other scanf in the program
how to fix it??
when one scanf returns 0
it doesnt enter to none of the other scanf in the program
how to fix it??
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.
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.
how do i clear the junk from the input stream??
i tried g=getchar();
still it skips the other scanf 's
Try this.
It's in the FAQ, as I mentioned. Cprogramming.com FAQ > Flush the input bufferCode:int c; while((c = getchar()) != '\n' && c != EOF) {}
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.
You can ignore an element in a scanf with the asterisk (*) flag:
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).Code:int x; scanf("%d%*c",&x);
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
post what you have and someone will point out how scanf() should be used
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.
.. 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.