Clearing buffer in a function

This is a discussion on Clearing buffer in a function within the C Programming forums, part of the General Programming Boards category; I have something that works like this Code: int main() { int currentNumber; int currentUnit[3]; int checkInput; while ((checkInput = ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    35

    Clearing buffer in a function

    I have something that works like this

    Code:
    int main()
    {
       int currentNumber;
       int currentUnit[3];
       int checkInput;
    
       while ((checkInput = scanf("%d%2s", &currentNumber, currentUnit)) != -1)
       {
          if (input OK) //do other stuff
          else
          {
              int clearScanfBuffer = 0;
              while ((clearScanfBuffer = getchar()) != '\n')
              {
                 //just loop and do nothing and clear out bad data 
              }
           }
        }
        //output some calculations
        return(0);
    }
    What I'd like to do, and can't seem to figure out what to do, is move the buffer clear code from the else statement, into it's own function out of the main loop where I can do other stuff with the data. I can get what I want to work in the main loop, but I don't want a huge chunk of code in there if I can write it something like this

    Code:
    int main()
    {
       ...
       else
       {
          clear( //some arguments?? )
       }
       return(0);
    }
    
    void clear( ?? )
    {
       put the clearScanfBuffer code here
    }
    The jist of my question is can I "move" that clear buffer code to it's own function, and if I can, what do I need to pass to the function to do that?

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    First of all to check scanf() for -1 but for EOF. Read the return value here. EOF can usually be -1, but that is not guaranteed (and EOF is prettier).

    Why can't you simply do
    Code:
    int main()
    {
       ...
       else
       {
          clear()
       }
       return(0);
    }
    
    void clear()
    {
       int clearScanfBuffer;
       while ((clearScanfBuffer = getchar()) != '\n')
       {
            //just loop and do nothing and clear out bad data 
        }
    }
    That should work perfectly fine. The clear() needs no arguments really if you work with stdin and stdout. You could make it generally and work with files also by passing the file handler.
    Tip: If scanf() doesn't leave any "trash" clear() will block and wait for a '\n'. You might want to check that, except if you feel you don't really need to. Don't know any "standard" way to do so, though. Except maybe with feof() or something similar.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    I see your trying to clean up the code

    you can just put a semicolon at the end of the while instead of braces

    Code:
      while ((clearScanfBuffer = getchar()) != '\n');

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Tetris Questions
    By KneeGrow in forum Game Programming
    Replies: 19
    Last Post: 10-28-2003, 11:12 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM

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