Topic: various questions on cin (and istream in general)


Author: mikeg@sashimi.wwa.com (Michael N. Goldshteyn)
Date: 15 Feb 1995 07:37:10 -0600
Raw View
I would like to know if it is possible to ask cin whether a character is
waiting to be processed and the pull that character in without having to
wait for a CR.

For example, the following code which is supposed to read a single char
waits for an entire line to be read (i.e. CR being pressed), before
the next statement executes:

char ch;

cin >> ch;
// ... do some processing

But, what if I want to do some processing as I am waiting for a keystroke?
For example:

string s;
char   ch;

while (1)
{
  if (character available from cin)
    {
    read the character into ch, without waiting for whole line (i.e. CR)
    if (ch!='\r')
      s=s+ch;
    else
      process_string(s);
    }
  do_some_other_processing();
}

The above contains 2 lines of pseudo code. One to ask cin whether or not
a keystroke is available for processing and the other to actually read
that character in a fashion such that only that character is read and
processing continues to the next statement. Is this possible? If so,
can someone tell me the code that is necessary to fill in the two
pseudo-code lines above?

Thanks

Mike

mikeg@wwa.com