Clearing input buffer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Carol Pedder

    #1

    Clearing input buffer

    Hello all, may I ask a question?

    Having just started C++ (using microsoft visual studio), I am using cin/cout
    for console applicarions (yes I know it doesn't happen in the real world!)
    and I'm finding it difficult to track down a method of clearing the input
    buffer after a cin (to get rid of potential rubbish).

    cin.flush() has been mentioned, but isn't an option on visual studio and
    cin.ignore() requires you to specify number of chars to ignore.

    Any help much appreciated.

    Regards

    Carol


  • Chris \( Val \)

    #2
    Re: Clearing input buffer


    "Carol Pedder" <carol@pedder00 2.freeserve.co. uk> wrote in message
    news:clauke$bv5 $1@news5.svr.po l.co.uk...
    | Hello all, may I ask a question?

    Sure.

    | Having just started C++ (using microsoft visual studio), I am using cin/cout
    | for console applicarions

    Well, I don't know what an 'applicarion' is, but let's
    see if we can work it out <g>.

    | (yes I know it doesn't happen in the real world!) and

    What do you mean by that ?

    | I'm finding it difficult to track down a method of
    | clearing the input buffer after a cin (to get rid of
    | potential rubbish).

    std::cin.clear( );

    ....will reset the stream state back to a good state.

    You can then follow that call with:

    std::cin.ignore ( std::numeric_li mits<
    std::streamsize >::max(), '\n' );

    ....to remove all remaining junk from the stream.

    It should now be ready for another read operation.

    | cin.flush() has been mentioned, but isn't an option on visual studio and

    It's not an option, because the C++ standard does not define
    such an operation for an input stream.

    | cin.ignore() requires you to specify number of chars to ignore.

    Not always - You can use the default call:
    cin.ignore();

    ....which will ignore '1' character from the stream,
    if one exists.

    [snip]

    Cheers.
    Chris Val


    Comment

    • Ioannis Vranos

      #3
      Re: Clearing input buffer

      Carol Pedder wrote:[color=blue]
      > Hello all, may I ask a question?
      >
      > Having just started C++ (using microsoft visual studio), I am using cin/cout
      > for console applicarions (yes I know it doesn't happen in the real world!)
      > and I'm finding it difficult to track down a method of clearing the input
      > buffer after a cin (to get rid of potential rubbish).
      >
      > cin.flush() has been mentioned, but isn't an option on visual studio and
      > cin.ignore() requires you to specify number of chars to ignore.[/color]


      cin.ignore() with '\n' as the terminal character, assuming you want to
      remoove the '\n' that remains in the stream.

      E.g. cin.ignore(1000 , '\n');



      --
      Ioannis Vranos


      Comment

      • Ron Natalie

        #4
        Re: Clearing input buffer

        Carol Pedder wrote:
        [color=blue]
        >
        > cin.flush() has been mentioned, but isn't an option on visual studio and
        > cin.ignore() requires you to specify number of chars to ignore.
        >[/color]
        flush() does not affect the input side of streams. If it's been mentioned
        they mentioners were wrong.

        cin.ignore works, it takes both a count and a delimeter.

        In actuality, what you think you're asking for is probably impossible.
        There's no concept of a "non-blocking" input in standard C++, that is,
        there's no way to tell that there isn't pending unread input.

        So in a trully portable program, you can't rely on that methodology.
        The best you can hope for is to read up to the next newline or other
        sentinal character when you want to discard input.

        Comment

        • Carol Pedder

          #5
          Re: Clearing input buffer


          Ron Natalie wrote:[color=blue]
          >
          > In actuality, what you think you're asking for is probably impossible.
          > There's no concept of a "non-blocking" input in standard C++, that is,
          > there's no way to tell that there isn't pending unread input.
          >
          > So in a trully portable program, you can't rely on that methodology.
          > The best you can hope for is to read up to the next newline or other
          > sentinal character when you want to discard input.[/color]

          Thanks for all your replies. I think I am asking the impossible.

          What I really wanted to do was clear the input buffer in the event of trying
          to input a letter to an integer variable, which sends the program into a
          loop as it attempts to extract an integer from the buffer ad infinitum.

          I realise that it is much better practice to use getch() and validate input
          properly, but could not understand why the output buffer can be flushed, but
          not the input buffer - I still can't, but I guess that's just the way it
          works!

          Thanks again

          Carol


          Comment

          • Ioannis Vranos

            #6
            Re: Clearing input buffer

            Carol Pedder wrote:
            [color=blue]
            > Thanks for all your replies. I think I am asking the impossible.
            >
            > What I really wanted to do was clear the input buffer in the event of trying
            > to input a letter to an integer variable, which sends the program into a
            > loop as it attempts to extract an integer from the buffer ad infinitum.
            >
            > I realise that it is much better practice to use getch() and validate input
            > properly, but could not understand why the output buffer can be flushed, but
            > not the input buffer - I still can't, but I guess that's just the way it
            > works![/color]


            Check this sample code from TC++PL 3, page 620:



            void read_a_line(int max)
            {
            // ...
            if (cin.fail()) { // Oops: bad input format
            cin.clear() ; // clear the input flags (21.3.3)
            cin.ignore(max, ´;´) ; // skip to semicolon

            if (!cin) {
            // oops: we reached the end of the stream
            }
            else if (cin.gcount()== max) {
            // oops: read max characters
            }
            else {
            // found and discarded the semicolon
            }
            }
            }



            So if you want to guard against invalid input and remove the remaining
            '\n' and other trash from the stream, one approach you can follow is this:



            while(cin)
            // ...


            if(cin.fail())
            {
            cin.clear();
            cin.ignore(1000 , '\n')
            }


            //If you want to detect the end of file
            if(cin.eof())
            // ...



            --
            Ioannis Vranos


            Comment

            • Carol Pedder

              #7
              Re: Clearing input buffer

              Ioannis Vranos wrote:
              [color=blue]
              > So if you want to guard against invalid input and remove the remaining
              > '\n' and other trash from the stream, one approach you can follow is this:
              >
              > .....[/color]
              [color=blue]
              > if(cin.fail())
              > {
              > cin.clear();
              > cin.ignore(1000 , '\n')
              > }
              >[/color]

              Placing this code after my cin does the trick - a perfect solution, thanks a
              lot.

              Regards

              Carol


              Comment

              Working...