Thread: Handling Extra Characters

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    53

    Handling Extra Characters

    I've been studying C Programming for the past 5 months...I've noticed many people use the fflush(stdin) command to clear any extra characters. This to me is great because people are human and make mistakes.

    My question is how often should you use flush? Would you use it any time you are using a scanf?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You shouldn't use fflush(stdin). Doing so greatly limits where your program can run.
    As for when you should clear out the input buffer: Whenever you use a function that has the possiblity of leaving stuff in the buffer.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    One should refrain from discarding data left in a stream. It severely limits the flexibility that users have in working productively. As an example, say you have a program that reads five numbers and prints the result of an addition on all of them.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int n = 0;
        int number;
        int sum = 0;
    
        printf("Enter five numbers: ");
        fflush(stdout);
        while (scanf("%d", &number) == 1) {
            sum += number;
            if (++n == 5)
                break;
        }
        if (n != 5) {
            fprintf(stderr, "Input error\n");
            return EXIT_FAILURE;
        }
        printf("The sum is %d\n", sum);
    
        return 0;
    }
    By restricting input and discarding anything in the stream, the following input would not work properly.

    Enter five numbers: 1 2 3 4 5

    Instead, it would read one number and discard everything else, thus requiring the user to enter numbers one by one. This would slow down all but novice users of the program. If you must discard left over characters, do so only when there is no possibility that the data would be required for further successful operation.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Edward
    One should refrain from discarding data left in a stream. It severely limits the flexibility that users have in working productively. As an example, say you have a program that reads five numbers and prints the result of an addition on all of them.

    ...

    By restricting input and discarding anything in the stream, the following input would not work properly.

    Enter five numbers: 1 2 3 4 5

    Instead, it would read one number and discard everything else, thus requiring the user to enter numbers one by one.
    Only if you flush within the while loop. I for one would flush after all the inputs or when there is a potential for misreads because of scanf's penchant for leaving crap in the buffer.

    Planning is what makes the flushing work without slowing the user.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting the characters from each word from a text file
    By flipguy_ph in forum C Programming
    Replies: 6
    Last Post: 04-27-2009, 05:56 PM
  2. problem with reading characters
    By csvraju in forum C Programming
    Replies: 4
    Last Post: 03-31-2009, 07:59 AM
  3. ascii characters video displaying on tv screen
    By deian in forum C Programming
    Replies: 6
    Last Post: 10-12-2004, 09:46 PM
  4. printing non-ASCII characters (in unicode)
    By dbaryl in forum C Programming
    Replies: 1
    Last Post: 10-25-2002, 01:00 PM