473,395 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
Jul 22 '05 #1
3 5334
On Wed, 21 Jul 2004 20:31:04 GMT, voidstar <vo******@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

Jul 22 '05 #2
Ash
John Harrison wrote:
On Wed, 21 Jul 2004 20:31:04 GMT, voidstar <vo******@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.
Jul 22 '05 #3

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 <fu******@nb.sympatico.ca>
wrote:
John Harrison wrote:
On Wed, 21 Jul 2004 20:31:04 GMT, voidstar <vo******@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.


Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Timothy Madden | last post by:
Hello Is my program legal and well formed: class Data; class SMax { class Data &Buffer; public: SMax(class Data &buffer)
3
by: Kyle Kolander | last post by:
I posted this in response to a previous thread but have not gotten any replies back... Hopefully someone has an answer? From looking at sections 27.7.3.2 and 27.7.1.2 of the standard, it appears...
1
by: Larry | last post by:
Hi my friends. How can I read keyboard buffer by C++ ? Best regards, Larry
5
by: nx-2000 | last post by:
I've got a very large C# forms app and now that its being used in bigger environments we're getting a steady stream of "why does it do this?" problems. The most nagging of which right now is that...
28
by: srikanth | last post by:
help me out with the subject
2
by: Sree | last post by:
Hello Friends, i was trying to read the Keyboard typeahead buffer from BIOS according to its address. But i'm not able to get the data. Please tell me how to do so. void main() { char far...
6
by: ppravin88 | last post by:
hi guys 'n' gals .. could anyone tell me the difference between getch(); and getche(); and when and where should we use this function... and also wat is the diff between return(); & return(0);...
1
by: nettleby | last post by:
The Microsoft site has confused me on this issue: http://www.microsoft.com/technet/prodtechnol/windows2000serv/reskit/iisbook/c06_redirection.mspx?mfr=true Says that: "ASP automatically discards...
3
by: NaN | last post by:
I've been trying to use _kbhit() but it didn't do what I thought it would from books, "Detects whether a keypress is available for reading." Herbert Schildt says, "If the user has pressed a key,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.