fflush(stdin)

This is a discussion on fflush(stdin) within the C Programming forums, part of the General Programming Boards category; well, i think i am going to write a small tutorials including all the frequently asked questions on this board... ...

  1. #16
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    well, i think i am going to write a small tutorials including all the
    frequently asked questions on this board... really every one!

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Hammer,
    Im honored to be the one you decided to slam for your 1000th post. Congrats.

    Rye
    Hi Rye, I wasn't meaning to slam you, only have a little fun No hard feelings, eh

    And dammit, I didn't even notice my 1000th post mark, and I was trying to remember it too

    Ask us any question you like... they're all fun to answer, even ones that get asked too many times! (as you can tell by this thread )
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    none taken

    no offense taken... I know its all in fun and some of the questions I do ask are in the book/FAQs but sometime I have difficulty understanding or implementing it in my specific program.

    Speaking for us n00bs... we really do appreciate all the time and help you guys provide to us people who have noone else to ask.

  4. #19
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >can someone please explain why someone would put fflush(stdin) after every user input?
    fflush ( stdin ) is undefined, don't use it. But to answer your question, there's no need to flush the input stream after every call. The problem is with some functions which leave unread data in the stream, this unread data will cause problems with the next call for input since whatever is in the buffer must be processed before anymore user input will be seen.

    >I'm not sure why it's undefined.
    Excuse me while I have a heart attack. You should know this quzah.

    Aside from the fact that the ISO C standard specifies that if the stream is not an output or update stream the behavior is undefined, you have to consider that the definition of fflush is to write unwritten data from the stream, not discard unread data which is a completely different meaning and function.

    >Or am I about to get smacked for my ignorance?
    That's about the size of it, input streams and output streams are handled differently. Having one function manage both the flushing of output and the discarding of input is silly since the two operations are wildly different. At best this would require two flush functions, one for input and one for output. Also consider that not all input systems are cooked by default, what would happen if you tried to use an input buffer flush on a raw input stream with no buffer? That seems like a prime candidate for undefined behavior to me.

    -Prelude
    My best code is written with the delete key.

  5. #20
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    Prelude

    I might be beating a dead horse on this but:

    >fflush ( stdin ) is undefined, don't use it.
    Could you recommend something else that would be better?
    Code:
    // GATHER DATA	
    	for (e = 0; e < maxnum; e++)
    	{
    		printf("Enter number %d : ", e + 1);
    		scanf("%lf", &a[e]);
    		fflush(stdin);
    	}
    You helped me previously when I asked about only allowing particular integers and you were very helpful by posting this
    Code:
    bool validate ( std::string val )
    {
      int i, j;
      const std::string valid = "0123456789-.";
      for ( i = 0; i < val.length(); i++ ) {
        for ( j = 0; j < valid.length(); j++ )
          if ( val[i] == valid[j] ) 
            break;
        if ( j == valid.length() )
          return false;
      }
      return true;
    }
    But the only problem with that is that I can still enter multiple (. and - characters) So I started playing with printf instead of cout and it looked like
    Code:
    scanf("%lf", &a[e]);
    printf("\n  %g", a[e]);
    was a better alternative.

    It just seems that I keep taking 1 step forward and 3 steps back!

  6. #21
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Try the following as an alternative to fflush(stdin).

    Code:
    void clearInput()
    {
           char junk[81];
           fgets(junk , 80, stdin);
    }

  7. #22
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Could you recommend something else that would be better?
    I prefer some variation of

    while ( getchar() != '\n' );

    >But the only problem with that is that I can still enter multiple (. and - characters)
    Of course, my function was just a basic validation to get you on your way. It's up to you to handle the required special cases.

    >Try the following as an alternative to fflush(stdin).
    That seems a bit wasteful of storage since you have no intention of using the data. Especially since most floating data is considerably less than 80 characters. And you don't have to worry about making the second argument to fgets one less than the buffer, it already reads N - 1 characters and places a nul in the last spot.

    -Prelude
    My best code is written with the delete key.

  8. #23
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Especially since most floating data is considerably less than 80 characters
    I forgot to read one of the posts, so I didn't know that he/she was working with floats.
    That seems a bit wasteful of storage since you have no intention of using the data
    Well, I'm a very generous programmer.

  9. #24
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >so I didn't know that he/she was working with floats
    I didn't mean floating-point values, I meant extra characters floating around in the input stream.

    >Well, I'm a very generous programmer.
    I can't argue with that I suppose. But you may want to update your function to handle a case where the extra input is actually greater than 80 characters.

    -Prelude
    My best code is written with the delete key.

  10. #25
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Ooooooohhh I seeeeeeee.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. fread/fwrite
    By chopficaro in forum C Programming
    Replies: 6
    Last Post: 05-11-2008, 01:48 AM
  3. Searching Into Files
    By St0rM-MaN in forum C Programming
    Replies: 12
    Last Post: 04-26-2007, 09:02 AM
  4. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 11:13 AM
  5. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:57 PM

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