Contradictory viewpoints, confusing the hell outta me

This is a discussion on Contradictory viewpoints, confusing the hell outta me within the C Programming forums, part of the General Programming Boards category; Hey all, I could really use a few viewpoints on using the fflush() function. I'm just starting out with C, ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    55

    Unhappy Contradictory viewpoints, confusing the hell outta me

    Hey all,

    I could really use a few viewpoints on using the fflush() function. I'm just starting out with C, and I'm currently going through C in 21 Days.

    The book states the following:

    There is a second way in which you can clear the extra characters that were typed in. The fflush() function flushes the information in a stream--including the standard input stream. fflush() is generally used with disk files (which are covered on Day 16); however, it can also be used to make Listing 14.7 even simpler.
    Now, the following, is the code that I typed up from the book, to try to use the fflush function, to demonstrate the clearing of stdin:

    Code:
    /* flush.c - Clearing stdin of extra characters, using fflush() */
    
    #include <stdio.h>
    
    main()
    {
    	int age;
    	char name[20];
    
    	puts("Enter your age.");
    	scanf("%d", &age);
    
    	/* Clear stdin of any extra characters. */
    
    	int test = fflush(stdin);
    
    	puts("Enter your first name.");
    	scanf("%s", name);
    
    	printf("Your age is %d.\n", age);
    	printf("Your name is %s.\n", name);
    	printf("%d", test);
    
    	return 0;
    }
    After compiling, I don't get any errors (this is with gcc (dgjpp) on win32). Now, here's what I get after running the program from a DOS window:

    (with correct input)

    Code:
    Enter your age.
    22
    Enter your first name.
    John
    Your age is 22.
    Your name is John.
    (with incorrect input)

    Code:
    Enter your age.
    22 and margh
    Enter your first name.
    John
    Your age is 22.
    Your name is and.
    I remember reading somewhere that perhaps this was a compiler issue, so I compiled the same program, with lcc, which is a full-on win32 compiler, and not command line, and after running the program, it worked...

    However, and this is where I'm not very confused, I talked to a guy on a newsgroup about this, and here's what he's saying:

    Oh dear. No, it is NOT a gcc problem. It is a problem with your BOOK, which has failed to explain to you that some constructs are illegal in C, and that your use of such constructs breaks the contract between you and the implementation. If you break the rules, the compiler is allowed to break the rules too. Using fflush(stdin) breaks the rules. gcc chooses not to play the game by your new rules. lcc /seems/ to play the game, but you have no guarantees that lcc doesn't do something weird under the hood, without telling you.
    Can someone clarify whether I should be using fflush(stdin) at all?

    Thanks in advance (sorry it's long, I wanted to show the route I've taken so far),

    John.
    Last edited by John.H; 08-17-2002 at 09:50 PM.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Can someone clarify whether I should be using fflush(stdin) at all?
    no. it's non-standard at best, supported by only unix compilers (i think...).

    for more info do a search for fflush(stdin). i know i asked this question once before.

  3. #3
    aurė entuluva! mithrandir's Avatar
    Join Date
    Aug 2001
    Posts
    1,209
    This may help you out in answering your question http://www.eskimo.com/~scs/C-faq/q12.26.html

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    Thanks for the replies guys. I guess that's the problem with having to put 'trust' into a book when you're starting out.

    Thanks again,

    John.

  5. #5
    aurė entuluva! mithrandir's Avatar
    Join Date
    Aug 2001
    Posts
    1,209
    Try to find a copy of "C: How to Program" by Deitel and Deitel. It's the very book I used to learn C - trust me, those guys know what they're talking about!

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by ygfperson
    no. it's non-standard at best, supported by only unix compilers (i think...).
    Not that I promote the use of fflush(stdin) (in fact I'm totally against it), here's proof that our dear friends at M$ think it's OK.

    My advice: go with the official standard, and don't use it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    Thanks ever so much for the replies you lot, I'm very grateful.

    I wouldn't usually have been so picky about asking the question on an issue like this, but clearing streams seemed like something that would be used quite regularly, so I needed to know for later use.

    Thanks again, I'm sure I'll be back (regularly) to ask more stupid questions lol.

    John - The newest member of the "How Do I...?" clan.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >here's proof that our dear friends at M$ think it's OK
    Yes, but that isn't saying very much, is it? Look at the example program they give you. I may be mistaken, but if someone showed up here with this code, they would either be run off or forcefully corrected. If not both.
    Code:
    /* FFLUSH.C */
    
    /* Also known as:
    ** FIND_THE_MISTAKES.C
    */
    
    #include <stdio.h>
    #include <conio.h>
    
    void main( void )
    {
       int integer;
       char string[81];
    
       /* Read each word as a string. */
       printf( "Enter a sentence of four words with scanf: " );
       for( integer = 0; integer < 4; integer++ )
       {
          scanf( "%s", string );
          printf( "%s\n", string );
       }
    
       /* You must flush the input buffer before using gets. */
       fflush( stdin );
       printf( "Enter the same sentence with gets: " );
       gets( string );
       printf( "%s\n", string );
    }
    >Thanks again, I'm sure I'll be back (regularly) to ask more stupid questions lol.
    There is only one stupid question: The one you do not ask.

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

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Yes, but that isn't saying very much, is it?
    Actually, it is. It tells me why my Win98 PC won't stay running for more than a few hours without crashing.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >It tells me why my Win98 PC won't stay running for more than a few hours without crashing.
    Win98? You should know better than that. Shame on you.

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

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Win98? You should know better than that. Shame on you.
    Yeah well, it costs money to upgrade, and I don't see that I'll get £180 pounds worth of benefit from it
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Unregistered
    Guest
    Hey all, just to kind of give fflush a little support here, I know it's not widely used, or recommended for it's erratic behavior...

    HOWEVER... while writing an embedded SQL program this past spring, it did not matter what I did, or how I tried to recieve my input, the only way to get the program to work correctly was to use fflush after the user chose a menu item. I even posted the code here and used people's suggestions, to no avail. And it worked error free. So despite it's errors, I had to use it.

  13. #13
    ober5861
    Guest

    Unhappy

    that was me... I got logged out somehow

  14. #14
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429

    Angry

    and yet again, I could not login...
    EntropySink. You know you have to click it.

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You see ober, just even talking about fflush(stdin) is now giving your PC the jitters (log-in hassle)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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

Similar Threads

  1. Living in the 00'S
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-21-2002, 09:31 AM

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