everyone's favorite...fflush
Every time I've posted for help, I get waayyy more comments on the fflush that my professor asks we use than on my actual problem or the rest of the code itself. Unfortunately, the alternatives to fflush seem to be pretty complicated and I can't figure out how to use any of the 5 or so that I have seen. I only ask because I want to make sure I do it right.
So I was thinking, what if I did my normal scanf() and then right after it I use a dummy scanf() that I kept empty to clean out any left over data?
I'm really new to C programming (about 2 months), so my thinking is probably filled with tons of absurdities, but I figured I'd ask.
Re: everyone's favorite...fflush
Quote:
Originally posted by BungleSpice
Every time I've posted for help, I get waayyy more comments on the fflush that my professor asks we use than on my actual problem or the rest of the code itself.
Another instructor that needs to be slapped! There really should be a requirement that "thems that teach should have learned" I'm glad these guys aren't teaching medicine!
Quote:
Originally posted by BungleSpice
Unfortunately, the alternatives to fflush seem to be pretty complicated and I can't figure out how to use any of the 5 or so that I have seen. I only ask because I want to make sure I do it right.
Then take the advice of the experts. That's why you're here I assume.
Quote:
Originally posted by BungleSpice
So I was thinking, what if I did my normal scanf() and then right after it I use a dummy scanf() that I kept empty to clean out any left over data?
Hmmm, kinda like using a hammer to fix the hole in the wall made by a hammer?
(sorry Hammer)
Re: Re: everyone's favorite...fflush
Quote:
Originally posted by WaltP
Another instructor that needs to be slapped! There really should be a requirement that "thems that teach should have learned" I'm glad these guys aren't teaching medicine!
I agree. I have one of those '21 Days' books for programming in C - some of it is pretty good, some of it is not good. When dealing with I/O the authors discuss various methods to get the user input. They definitely pointed out that gets() is bad, and that fgets() is a good alternative to gets(). But then they go on to scanf() and what a good function that is, and that all you have to do to clear the buffer is to use fflush(stdin)! I was under the impression that the manuscripts for these types of books have to undergo some fairly serious scrutiny by experts in that particular field before they go to the press.
Anyway, that is going off topic.
Bunglespice, it is not a bad idea to have yourself a library of collected pieces of code that work well, and that you can re-use. In this case you need a function that will get the user input, safely and intelligently. In other words, a getline function of sorts, tailored to suit your needs.
[excuses]
I have one of my own, but its not 'complete' in that I threw it together and it works fine for my purposes but I still need to add error checking before I let someone else use a program that incorporate it. In that light, I don't have time before work to add that error checking, and then post it here as an example. :)
[/excuses]
Hopefully you can piece some stuff together from this post though.
~/