Thread: Can someone xplain FLUSH in strings?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    63

    Question Can someone xplain FLUSH in strings?

    Hi,
    I know the basic purpose of FLUSH but want to have a deeper understanding of it..the code for FLUSH (ing) in C is this..can someone please explain..
    {
    #define FLUSH while(getchar() != '\n')
    char month[10];

    printf("Please enter a month");
    scanf("%9s", month);
    FLUSH;
    }

    I understand everything but the definition and as to how does the call work..please help..thanks
    A

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    when you complie your code the compiler will replace the word FLUSH with it's definition.
    So there is no call, your code will look like
    Code:
    printf("Please enter a month"); 
    scanf("%9s", month); 
    while(getchar() != '\n');
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    14
    #define FLUSH while(getchar() != '\n')
    thats ugly, use a function designed for flushing scanf instead
    PHP Code:
    #define     FLUSH fflush(stdin) 

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This is undefined, and therefore not guaranteed to work:
    >#define FLUSH fflush(stdin)

    Read about it here
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    >thats ugly

    no need to call names...

    That is one of the most common defines I have seen in programming. Not to mention it always works.
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    Umm..thanks for your varied answers but could someone stick w/ the code I've mentioned and explain what
    while(!(getchar()....) does?? Please..Thanks..
    Honestly, your answers confused me a bit more..it would be great if someone explained it clearly..
    Thanks again
    A

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >explain what while(!(getchar()....) does??
    while ( getchar() != '\n' );

    What this does is read characters from stdin until a newline is reached. Since a newline usually means the end of input it is assumed that once this loop exits there will be nothing left in the input buffer. It's very useful for clearing the newline left in the buffer by calls to scanf.

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

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    { 
    #define FLUSH while(getchar() != '\n') 
    char month[10]; 
    
    printf("Please enter a month"); 
    scanf("%9s", month); 
    FLUSH; 
    }
    When scanf() reads input, it will take upto 9 characters into the variable month. If the user enter 50 characters, the remaining 41 stay in the input buffer. scanf() will also leave the new line character in the buffer.

    The while loop containing getchar() simply eats the characters in the buffer until a new line is reached. That way all the extra characters are ignored.

    If you didn't do the FLUSH call, the next time your prog did a read from the keyboard, it would get the users input from earlier, which would probably give undesired results.

    [EDIT] Doh! beaten again. [/EDIT]
    Last edited by Hammer; 05-12-2002 at 06:19 PM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Talking

    Go go go!!!

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    63
    Thanks guys SOOOOOOOOOO much!
    The last 3 (umm..maybe not the one before this but the 2 before that one) helped me a lot..thanks!!
    A

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM