Thread: fgets buffer

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    6

    fgets buffer

    This is my code from searching on the forums!!

    Code:
     printf("Enter Message in text:");		
    		fgets (message, 500, stdin ); 
    		printMessage ();
    		fflush(stdout);
    		clearBuffer ();
    
    void clearBuffer()
    {
    	while((c = getchar()) != EOF && c != '\n') {
    	     ;
    		}
    }
    However it is not clearing the buffer and is displaying the same message as i typed in previously, where have i gone wrong!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    1. Post the whole program - like how did you declare buffer anyway
    2. You seldom need your flush buffer code when using fgets, since fgets reads a newline if there is room to store it (so unlike scanf, a newline is seldom ever left for later)

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by PING
    Code:
    c=getchar();
    while(c!=EOF || c!= '\n');
    Nice. A correction that is incorrect.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    Yes, sorry for that one, the getchar should have been included inside the while loop as well.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    Code:
    do
    		{
    		clearBuffer ();
    		printf("Enter Message in text:");		
    		fgets (message, 500, stdin ); 
    		printMessage ();
    		fflush(stdout);
    		clearBuffer ();
    		printf("\n");
    		printf("Your message is %s\n",Result);
    		printf("\n");
    		printf("Would you like to do this again(Y/N):");
    		fflush(stdout);
    		scanf("%c", &select);
    		select = toupper(select);
    		printf("\n");
    		}while(select != 'N');

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > fgets (message, 500, stdin );
    HOW DID YOU DECLARE message??????????

    > scanf("%c", &select);
    You only need your hacky buffer flush after scanf() calls, not before fgets() calls.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by PING
    Yes, sorry for that one, the getchar should have been included inside the while loop as well.
    And what value would break the loop?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    6
    char message[500];

    Sorry salem

  9. #9
    old man
    Join Date
    Dec 2005
    Posts
    90
    Considering how fgets() works, the \n will either be in your buffer (which has become a string) or stdin. If it's in your buffer, then there's nothing to clear from stdin.

    So try something like this:

    Code:
    #include <string.h>    /* for strchr() */
    
    void
    check_input (char *s)
    {
      char *t;
      if ((t = strchr (s, '\n')) == NULL)
      {
        while (getchar() != '\n')
          ;
      }
      else
        *t = '\0';
    }
    This will also kill the \n if it's in your string, since it's common not to want it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Excess buffer using fgets
    By $l4xklynx in forum C Programming
    Replies: 20
    Last Post: 06-25-2009, 03:56 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Confused about fgets, clearing the buffer, etc
    By caduardo21 in forum C Programming
    Replies: 1
    Last Post: 06-13-2005, 11:03 AM
  4. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM