i wanted to ask why fflush(stdin) wrong
i saw faq but i couldnt rlly understand its not so wrong...
that it flushes stream ...
sometimes scanf gives \n how can i bypass this problem without fflush(stdin).
i wanted to ask why fflush(stdin) wrong
i saw faq but i couldnt rlly understand its not so wrong...
that it flushes stream ...
sometimes scanf gives \n how can i bypass this problem without fflush(stdin).
This has been covered 1000's of times. You either use something that eats up characters until the buffer is empty (it's in the FAQ- you should have seen it) or if you're pro, you read the entire line and parse it yourself.
I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo
It gives you undefined behavior which makes your program work at sometime and not work at other times. that's why.
Not everything that can be counted counts, and not everything that counts can be counted
- Albert Einstein.
No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
- Herbert Mayer
Because it's
1. Not compliant with the standards
2a. Non portable if it does work
2b. Undefined if it does "work"
The FAQ explains well why you shouldn't do it, it also contains an alternative.
oh ok thanks
could i also use fgets to read in integer?
You can use it to read in a string, whether the string is numeric or not. I am not sure what exactly that has to do with "flushing" the input buffer since using getchar() in a loop is usually enough.Originally Posted by lolguy
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I talked to a friend who's studying in an institute. I saw that the use of scanf gets and fflush are being taught to them. The book about C that he gave to me is also teaching about that. I have been using that for 8 months of studying C and C++.. and now it's not nice to use it.. I wonder why publisher would sell those kind of books that teach wrong.. or maybe they just wanted to let the reader know that those functions exist and let the reader discover that it's not good to use.
Although gets is beyond redemption, there is nothing wrong about using scanf and fflush. For example, fflush on an output stream is well defined.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>I wonder why publisher would sell those kind of books that teach wrong
A technical review is rather expensive, and a lot of the time if a book looks good to the layman publisher then it'll get published regardless of the technical quality. The sad reality is that most of the authors that write a programming book are not experts on the topic. Herbert Schildt comes to mind. He's a great writer, but not the master programmer his books shamelessly suggest.
A book doesn't have to be perfect to be useful, as long as you don't take what it says as gospel.
My best code is written with the delete key.