How can I clear buffer between getch & cin?



 DEVELOP > c-Plus-Plus > How can I clear buffer between getch & cin?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "voidstar"
Date: 21 Jul 2004 03:31:04 PM
Object: How can I clear buffer between getch & cin?
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
.

User: "John Harrison"

Title: Re: How can I clear buffer between getch & cin? 21 Jul 2004 03:50:21 PM
On Wed, 21 Jul 2004 20:31:04 GMT, voidstar <voidstar@tin.it> 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
.
User: "Ash"

Title: Re: How can I clear buffer between getch & cin? 26 Jul 2004 08:17:24 PM
John Harrison wrote:

On Wed, 21 Jul 2004 20:31:04 GMT, voidstar <voidstar@tin.it> 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.
.
User: "Mark R Rivet"

Title: Re: How can I clear buffer between getch & cin? 27 Jul 2004 10:49:52 AM
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 <furroash@nb.sympatico.ca>
wrote:

John Harrison wrote:

On Wed, 21 Jul 2004 20:31:04 GMT, voidstar <voidstar@tin.it> 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.

.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER