Connecting Tech Pros Worldwide Help | Site Map

How can I clear buffer between getch & cin?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 04:44 PM
voidstar
Guest
 
Posts: n/a
Default 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

  #2  
Old July 22nd, 2005, 04:44 PM
John Harrison
Guest
 
Posts: n/a
Default Re: How can I clear buffer between getch & cin?

On Wed, 21 Jul 2004 20:31:04 GMT, voidstar <voidstar@tin.it> wrote:
[color=blue]
> 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[/color]

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

  #3  
Old July 22nd, 2005, 04:53 PM
Ash
Guest
 
Posts: n/a
Default Re: How can I clear buffer between getch & cin?

John Harrison wrote:[color=blue]
> On Wed, 21 Jul 2004 20:31:04 GMT, voidstar <voidstar@tin.it> wrote:
>[color=green]
>> 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[/color]
>
>
> 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
>[/color]
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.
  #4  
Old July 22nd, 2005, 04:54 PM
Mark R Rivet
Guest
 
Posts: n/a
Default Re: How can I clear buffer between getch & cin?


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:
[color=blue]
>John Harrison wrote:[color=green]
>> On Wed, 21 Jul 2004 20:31:04 GMT, voidstar <voidstar@tin.it> wrote:
>>[color=darkred]
>>> 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[/color]
>>
>>
>> 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
>>[/color]
>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.[/color]

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.