Thread: Validating Numbers only

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What do you all think about using 'rewind(stdin)' to clear the keyboard buffer?
    Seeking isn't required to be supported on terminal streams, so rewinding stdin is non-portable, just like fflushing stdin. Have you considered not cluttering up the input stream in the first place? Then the issue is moot and you can move on to more interesting things.
    My best code is written with the delete key.

  2. #17
    Registered User
    Join Date
    Sep 2004
    Posts
    44
    Prelude,

    You asked:
    Have you considered not cluttering up the input stream in the first place?
    The problem is that it can get cluttered up, with fgets with scanf with any of the input functions.

    How would you do this?

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > How would you do this?
    Well you only ever use fgets() for reading input.

    After that, one of the things you can use is sscanf(), but since the input is now in an array, there are many possibilities for extracting information.

  4. #19
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >The problem is that it can get cluttered up, with fgets with scanf with any of the input functions.
    Input in C is line oriented. When you try to do something different such as with scanf, extraneous characters make their way into the stream and you're stuck with this classic problem. But if you use fgets or something similar to always read a line and then parse it in memory, input is line oriented and everything works smoothly. There's no need to clean up a mess that you don't make in the first place.

    >How would you do this?
    I would either use fgets, or write my own getline function, then use one of several parsing methods once I have a string in memory to play with.
    My best code is written with the delete key.

  5. #20
    Registered User
    Join Date
    Sep 2004
    Posts
    44
    The problem with fgets is that if the input exceeds the destination buffer size the remaining input remains in the keyboard buffer. If you make your destination buffer sufficiently large this would rarely, if ever, happen. But it still could happen. You could also do something like this:
    Code:
    fgets(buf, sizeof(buf), stdin);
    if( strlen(buf)+1 == sizeof(buf) )
    {
        while ( (ch = getchar() ) != EOF && ch != '\n' );
    }
    which cleans up the keyboard buffer if the destination buffer length is exceeded.

    [edit]
    This is better:
    Code:
    fgets(buf, sizeof(buf), stdin);
    if( strchr(buf, '\n') == NULL )
    {
        while ( (ch = getchar() ) != EOF && ch != '\n' );
    }
    Last edited by n7yap; 12-11-2004 at 06:50 PM.

  6. #21
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >if the input exceeds the destination buffer size the remaining input remains in the keyboard buffer
    Yes, though what you described is almost always a critical error. Either the program in question would test for such an occurance and terminate with an error message (or recover if possible) or it would guarantee that a line is always read by concatenating buffers into dynamic memory until either a newline or EOF is reached.

    In naive toy programs, flushing the stream is a reasonable solution. In Real Code(TM), throwing away input is stupid and dangerous. If you can't process it or safely ignore it then something is seriously wrong.
    My best code is written with the delete key.

  7. #22
    Registered User
    Join Date
    Sep 2004
    Posts
    44
    Prelude,

    throwing away input is stupid and dangerous
    That's not always true, it depends on what you are doing.

    "Real Code(TM)" could be many different things with many different requirements. In my context it is Windows programming, using any compiler and OS specific functions I may need, i.e. Win32 API. And not really being concerned with portability. I have been programming for years. I have recently developed a renewed interest in Standard C. I have also developed an interest in 'safe string handling' in Standard C. That is why I have been showing up here. But with the condescending attitude around here, I probably won't be staying around.

  8. #23
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >That's not always true, it depends on what you are doing.
    As with everything, it's not always true. But I would be more suspicious of code that does so than code that doesn't. You'll find that just about everyone else who knows what they're doing will react the same way. Flushing the input stream suggests that you didn't get input right the first time, and now you're trying to put a Band-Aid on the problem.

    >"Real Code(TM)" could be many different things with many different requirements.
    Yes.

    >In my context it is Windows programming, using any compiler
    >and OS specific functions I may need, i.e. Win32 API. And not
    >really being concerned with portability.
    I'll keep that in mind, but it really doesn't change the principles of good design.

    >I have recently developed a renewed interest in Standard C.
    Great! We always welcome people with an interest in learning more.

    >I have been programming for years.
    Well, we have something in common then.

    >But with the condescending attitude around here, I probably won't be staying around.
    I don't recall seeing anyone act condescending toward you. You misinterpreted a neutral correction as being patronizing. Unfortunately, that's your problem, and there's nothing I can do about it. If you want to leave after an imagined insult then that's your prerogative.
    My best code is written with the delete key.

  9. #24
    Registered User
    Join Date
    Sep 2004
    Posts
    44
    Prelude,

    I get your point about not discarding the remaining input. I am now trying to come up with a bullet-proof input function using fgets.
    it would guarantee that a line is always read by concatenating buffers into dynamic memory until either a newline or EOF is reached.
    Sounds like the way to go.

    Thanks,

  10. #25
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Sounds like the way to go.
    Possibly. The problem with concatenating buffers is that the code is a bit more obscure than using fgetc and growing the memory incrementally. Since it's not as easy to check for correctness, you should implement both and test religiously. But that's an implementation detail. You'll find that both are comparable in speed.
    My best code is written with the delete key.

  11. #26
    Registered User
    Join Date
    Sep 2004
    Posts
    44
    Here's what I came up with this evening:

    http://cboard.cprogramming.com/showthread.php?t=59670

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM