everyone's favorite...fflush

This is a discussion on everyone's favorite...fflush within the C Programming forums, part of the General Programming Boards category; Every time I've posted for help, I get waayyy more comments on the fflush that my professor asks we use ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    26

    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.

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    If you have to use scanf that will work ex:
    Code:
    scanf("%d",&x):
    scanf ( "%*[^\n]" );
    I suggest you read this forum thread here

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156

    Re: everyone's favorite...fflush

    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!

    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.

    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)
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528

    Re: Re: everyone's favorite...fflush

    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.

    ~/

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >scanf("%d",&x):
    >scanf ( "%*[^\n]" );
    Don't forget a getchar(); or a scanf ( "%*c" ); after that to take care of the newline itself.

    >Hmmm, kinda like using a hammer to fix the hole in the wall made by a hammer?
    What better way to fill a hammer shaped hole than a hammer?
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Since the topic of evil instructors has been brought up several times, I ask your indulgence for a brief editorial. (Skip the rest of this post if you are not interested.)

    Part of your University experience is learning neat ways to do things. Some of these things you get from instructors; some of them from outside experts with different points of view.

    Another part of your Educational experience is "getting along in this world". I don't mean compromising your integrity for the sake of a grade or a paycheck; I mean integrating your experience into a context where you are not at the top of the food chain.

    If you have a particularly obnoxious instructor, too bad; we've all had them from time to time. Consider that sometime in the future you may have an obnoxious boss who insists on doing things "his way", in spite of expert advice to the contrary, and in spite of your superior knowledge of how things should be done.

    In case of the instructor: he may or may not have the inclination or the time to consider objections or suggestions for alternative solutions. I suggest that you learn everything possible about what fflush() does and does not do. Make up examples. Is there any case where it screws up the works?

    In case of the boss: do his equirements lead to a less stable product; could following his bad advice make bad things happen. Do you have a better solution? Document it and try to get him to consider your point(s).

    Use your head. Try to keep your emotions out of your arguments.

    Been there; done that; still looking for a perfect world (but not holding my breath).

    Dave

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    26
    Dave - Who is your editorial in response to? I do hope it wasn't concerning my original post. I know about bad bosses, and bad professors. I'm a working guy in a corporate world and know all about "getting along in the world". Been doing it for over a decade. You asked me to keep my emotions out of the arguments and to use my head. Well, the reason for this post was me using my head for greater experience and to learn it the right way. No emotion there. Just wanting to better myself. In fact, I don't think anyone here had any attitude or emotion in their posts except for you.
    I wasn't complaining about my professor. In fact, I'm very please with my professor, even if he does have a flaw in his logic in that he believes that fflush is the way to go. Doesn't mean he is a bad professor. It just means I can ignore that part of his teaching. The reason for my post is that I had a possible alternative solution and wanted to know if you, the professionals, thought it would work. That's all.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    I'm really sorry if there was a question as to my intent.

    A previous post (not yours, obviously) mentioned that the instructor deserved to be slapped. This was quoted in a subsequent post.

    I respect everyone's right to express an opinion, and I was expressing mine. I would find it very frightening to live in a world where everyone's opinion is the same as mine.

    (That's just my opinion, of course.)

    Cheers!

    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Your favorite TV show...
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 53
    Last Post: 04-25-2007, 06:40 AM
  2. To fflush, or Not to fflush...
    By bjgough in forum C Programming
    Replies: 32
    Last Post: 08-19-2005, 12:56 AM
  3. When and where to use fflush (stdout)?
    By Micko in forum C Programming
    Replies: 8
    Last Post: 02-18-2005, 10:58 AM
  4. What's Your Favorite Empire?
    By Unregd in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 03-28-2003, 11:55 AM
  5. Favorite Genre: Tv/Movies/Books
    By drdroid in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 02-21-2003, 07:03 PM

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