Thread: Flushing buffer

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    115

    Flushing buffer

    I have a problem with flushing the buffer, I don't wanna use fflush( stdin ) for many said that it's providing abnormal behavior. I have the program below, if I exceeded the size of the buffer I am not able to enter any input for the salary. thanks.

    Code:
    struct Employee enterEmpInfo()
    {
       struct Employee enterEmp;
       int length = 0;
       
       printf( "Employee name: " );
       fgets( enterEmp.empName, 48, stdin );
       length = strlen( enterEmp.empName );
       enterEmp.empName[ length - 1] = '\0';
       
       printf( "SSS Number   : " );
       fgets( enterEmp.empSSS, 14, stdin );
       length = strlen( enterEmp.empSSS );
       enterEmp.empSSS[ length - 1 ] = '\0';
       
       printf( "Salary       : " );
       scanf( "%f", &enterEmp.empSalary );
       
       return enterEmp;
    }
    edit: is there any way that we could do to flush the remaining buffer without using the fflush( stdin )?
    Last edited by $l4xklynx; 03-02-2009 at 11:53 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    simplest way - do not use scanf - use fgets+sscanf and you do not need flushing at all
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by vart View Post
    simplest way - do not use scanf - use fgets+sscanf and you do not need flushing at all
    Code:
    struct Employee enterEmpInfo()
    {
       struct Employee enterEmp;
       char salary[ 10 ];
       int length = 0;
       
       printf( "Employee name: " );
       fgets( enterEmp.empName, 48, stdin );
       length = strlen( enterEmp.empName );
       enterEmp.empName[ length - 1] = '\0';
       
       printf( "SSS Number   : " );
       fgets( enterEmp.empSSS, 14, stdin );
       length = strlen( enterEmp.empSSS );
       enterEmp.empSSS[ length - 1 ] = '\0';
       
       printf( "Salary       : " );
       fgets( salary, 7, stdin );
       length = strlen( salary );
       //salary[ length - 1 ] = '\0';
       enterEmp.empSalary = atof( salary );
       //scanf( "%f", &enterEmp.empSalary );
       
       return enterEmp;
    }
    I guess this would be ok?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if you are sure that salary line is not more than 5 digits (including decimal point) always

    you think too low about salaries?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I wish my salary was more than 5 digits.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    you can flush the buffer with a loop. input is line oriented and you can be pretty sure that users have to type enter at the end, so you can just search for that character.
    Code:
    while (getchar() != '\n') ;
    sometimes that doesn't work as well as you want, like if you don't know if there's anything in the buffer but you want to flush it if there is. if there's nothing in the buffer that loop will wait for input before finishing instead of not running at all. i don't think there's a portable way to find out of there's anything in the buffer without waiting, but you can do it if you know how stdin works on the inside.
    Code:
    #if defined(_MSC_VER)
    #define flush(strm) ((strm)->_cnt = 0)
    #else
    #define flush(strm) while (getc(strm) != '\n')
    #endif
    this works on microsoft compilers. if it's a microsoft compiler then _MSC_VER will be defined and flush fixes the buffer's insides to do what i want. if _MSC_VER isn't defined, the portable loop strategy is used instead. you can add #elif's for other compilers too.
    Quote Originally Posted by vart
    simplest way - do not use scanf - use fgets+sscanf and you do not need flushing at all
    it's not that simple. if the input is longer than the size passed to fgets, the rest of the input stays in the stream buffer. unless you want to write a function that grows a string dynamically to take the whole line, you still have to do something with the leftovers or the rest of the input will get corrupted.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    about the salary, I understand what you mean but the program is just for testing. I coded a function showed below.

    Code:
    //in main
    char *name;
    
    name = getString( 50 );
    This is the function that returns a pointer to a character.

    Code:
    char *getString( int strSize )
    {
        char value;//, arrValue[ strSize ];
        char *rcvValue;
        int ctr = 0;
     
        //rcvValue = arrValue;
        rcvValue = malloc( 5 * sizeof( strSize ) );
        while( 1 )
        {
           value = getch();
           if( value == '\b' && ctr > 0 )
           {
              putch( value );
              putch( ' ' );
              putch( value );
              ctr--;
           }
           else if( value != '\b' && value != '\r' && ctr != 30 )
           {
              
              if( isspace( value ) || isalpha( value ) )
              {
                 //arrValue[ ctr ] = value;
                 rcvValue[ ctr ] = value;
                 putch( value );
                 ctr++;
              }
           }
                
           if( value == '\r' )
           {
              if(  ctr > 0 )
                 break;
           }
        }
        
        rcvValue[ ctr ] = '\0';
        //arrValue[ ctr ] = '\0';
        //rcvValue = malloc( strlen( arrValue ) + 1 );
        return rcvValue;
    }
    Maybe with this style of inputting string can avoid excess buffer coz I am able to restrict the user as you can see in the function. If you have any feedback with the code let me know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help flushing buffer
    By MSF1981 in forum C Programming
    Replies: 2
    Last Post: 02-15-2009, 07:30 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  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