How can I clear buffer between getch & cin?

Discussion in 'C++' started by voidstar, Jul 21, 2004.

  1. voidstar

    voidstar Guest

    Hi, I have the following problem:

    I use "getch" to monitor keypresses and the I use "cin" to input a
    string.

    When I type in 'y', the 'y' character appears, so I need to hit backspace
    before typing the string.

    c = getch();
    cin >> str;

    Please help me!
    Thanks in advance
    voidstar
    voidstar, Jul 21, 2004
    #1
    1. Advertising

  2. On Wed, 21 Jul 2004 20:31:04 GMT, voidstar <> wrote:

    > Hi, I have the following problem:
    >
    > I use "getch" to monitor keypresses and the I use "cin" to input a
    > string.
    >
    > When I type in 'y', the 'y' character appears, so I need to hit backspace
    > before typing the string.
    >
    > c = getch();
    > cin >> str;
    >
    > Please help me!
    > Thanks in advance
    > voidstar


    getch is not part of standard C++, cin is part of standard C++. If you try
    an mix the two then the results are going to be unpredictable. Basically
    don't do it.

    Standard C++ has no way of monitoring keypresses, so if you need to do
    this then you should forget about using standard C++ for any console input
    or output.

    john
    John Harrison, Jul 21, 2004
    #2
    1. Advertising

  3. voidstar

    Ash Guest

    John Harrison wrote:
    > On Wed, 21 Jul 2004 20:31:04 GMT, voidstar <> wrote:
    >
    >> Hi, I have the following problem:
    >>
    >> I use "getch" to monitor keypresses and the I use "cin" to input a
    >> string.
    >>
    >> When I type in 'y', the 'y' character appears, so I need to hit backspace
    >> before typing the string.
    >>
    >> c = getch();
    >> cin >> str;
    >>
    >> Please help me!
    >> Thanks in advance
    >> voidstar

    >
    >
    > getch is not part of standard C++, cin is part of standard C++. If you
    > try an mix the two then the results are going to be unpredictable.
    > Basically don't do it.
    >
    > Standard C++ has no way of monitoring keypresses, so if you need to do
    > this then you should forget about using standard C++ for any console
    > input or output.
    >
    > john
    >

    All true, but I found that it is compiler dependant. For example, in
    Borland's compiler:

    ch = getch();
    switch(ch){
    //select stuff
    }
    cin >> string; //or even cin.getline(const char[], int)

    after pressing a button to be used in the switch() statement, that char
    would be placed at the beginning of the string's array. However, using
    Bloodshed's Dev-C++ IDE, it seems to 'clear the buffer'. I have, however
    heard of functions that do this. I think http://www.cplusplus.com, but
    I'm not sure.
    Ash, Jul 27, 2004
    #3
  4. voidstar

    Mark R Rivet Guest

    I believe you should do a "cin>>ws;"
    the "ws" means white space and should clear the buffer after you do
    the getch();


    On Tue, 27 Jul 2004 01:17:24 GMT, Ash <>
    wrote:

    >John Harrison wrote:
    >> On Wed, 21 Jul 2004 20:31:04 GMT, voidstar <> wrote:
    >>
    >>> Hi, I have the following problem:
    >>>
    >>> I use "getch" to monitor keypresses and the I use "cin" to input a
    >>> string.
    >>>
    >>> When I type in 'y', the 'y' character appears, so I need to hit backspace
    >>> before typing the string.
    >>>
    >>> c = getch();
    >>> cin >> str;
    >>>
    >>> Please help me!
    >>> Thanks in advance
    >>> voidstar

    >>
    >>
    >> getch is not part of standard C++, cin is part of standard C++. If you
    >> try an mix the two then the results are going to be unpredictable.
    >> Basically don't do it.
    >>
    >> Standard C++ has no way of monitoring keypresses, so if you need to do
    >> this then you should forget about using standard C++ for any console
    >> input or output.
    >>
    >> john
    >>

    >All true, but I found that it is compiler dependant. For example, in
    >Borland's compiler:
    >
    >ch = getch();
    >switch(ch){
    > //select stuff
    >}
    >cin >> string; //or even cin.getline(const char[], int)
    >
    >after pressing a button to be used in the switch() statement, that char
    >would be placed at the beginning of the string's array. However, using
    >Bloodshed's Dev-C++ IDE, it seems to 'clear the buffer'. I have, however
    >heard of functions that do this. I think http://www.cplusplus.com, but
    >I'm not sure.
    Mark R Rivet, Jul 27, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,093
    P.J. Plauger
    Oct 17, 2003
  2. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,139
    Kevin Saff
    Jan 6, 2004
  3. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,669
    Alex Vinokur
    Mar 6, 2005
  4. Crow

    cin and getch()

    Crow, Mar 13, 2006, in forum: C++
    Replies:
    4
    Views:
    597
    Dietmar Kuehl
    Mar 13, 2006
  5. Fernando
    Replies:
    4
    Views:
    1,651
    Fernando
    Nov 16, 2011
Loading...

Share This Page