How to determine end of std::cin

Discussion in 'C++' started by Johannes Meng, Mar 2, 2007.

  1. Good day,

    I'm experimenting with unbuffered input at the moment. To get input I
    basically use cin.get(). My problem are control sequences preceeded by an
    ESC character (i.e. up, down, f-keys et cetera). They basically look like
    this: <ESC><EXCODE><CODE>, where EXCODE is a number describing the type of
    key pressed and CODE is the actual key. I want to be able to decide whether
    ESC or such a control key was pressed. Now if I do it like this:

    char code = 0, excode = 0;
    code = cin.get()
    if (code == 27) {
    excode = cin.get();
    code = cin.get();
    }

    , then I will have proper codes for all control keys. If ESC is pressed,
    though, the user will actually have to press three times - because
    cin.get() seems to wait for actual input to appear in stdin.
    Now I tried stuff like

    char code = 0, excode = 0;
    code = cin.get()
    if (code == 27 && !cin.eof()) {
    excode = cin.get();
    code = cin.get();
    }

    , but that won't work, even if there is only the ESC character in stdin.

    Is there another way to determine, whether the last character was read
    from stdin? Am I doing anything wrong?

    Thanks alot.
     
    Johannes Meng, Mar 2, 2007
    #1
    1. Advertising

  2. Johannes Meng

    kwikius Guest

    On 2 Mar, 11:26, Johannes Meng <> wrote:

    First, I'm no expert on standard keryboard input in C++.

    However AFAIK std::cin is quite primitive and only accepts ASCII
    characters. The characters you want are extended ASCII AFAIK and a
    quick test on win32 seems to suggest that standard input simply
    ignores them.

    Unfortunately the proper way forward I think, is to start
    investigating a GUI toolkit. These firstly use a different paradigm
    from the "input as file" paradigm of standard C++. This is the event
    based program paradigm, in which your application sits idling and the
    system fires events at it to which it responds. Typical events are
    mouse move and keyboard events , and you then write functions to
    describe what your application should do in response.

    The simplest way to try some event based programming I would guess is
    to use another language such as Java. C++ has ( nor probably will ever
    have) much interest in standardisng a GUI.

    regards
    Andy Little
     
    kwikius, Mar 2, 2007
    #2
    1. Advertising

  3. kwikius wrote:

    > On 2 Mar, 11:26, Johannes Meng <> wrote:
    >
    > First, I'm no expert on standard keryboard input in C++.
    >
    > However AFAIK std::cin is quite primitive and only accepts ASCII
    > characters. The characters you want are extended ASCII AFAIK and a
    > quick test on win32 seems to suggest that standard input simply
    > ignores them.


    Well, I don't know about win32, but here on linux, when, say, "arrow up" is
    pressed, cin actually contains 27 91 65. Try the following:

    #include <iostream>
    using namespace std;

    int main(int argc, char **argv)
    {
    string test;
    cin >> test;

    for (int i = 0; i < test.length(); i++) {
    cout << (int)test << " ";
    }

    cout << endl;

    return 0;
    }

    If you start the programm, then press "arrow up" and then "enter", it should
    output "27 91 65".

    That means that the codes are there - I just need a way to determine whether
    there's still something left in cin after reading a 27 (code for esc).

    --
    Johannes Meng
     
    Johannes Meng, Mar 2, 2007
    #3
  4. > kwikius wrote:
    > The simplest way to try some event based programming I would guess is
    > to use another language such as Java. C++ has ( nor probably will ever
    > have) much interest in standardisng a GUI.


    I just noticed I might not have been clear about what I want: I do _not_
    want to program a gui. It's all about the command line ;)

    --
    Johannes Meng
     
    Johannes Meng, Mar 2, 2007
    #4
  5. Johannes Meng

    kwikius Guest

    On 2 Mar, 16:29, Johannes Meng <> wrote:
    <...>

    > #include <iostream>
    > using namespace std;
    >
    > int main(int argc, char **argv)
    > {
    > string test;
    > cin >> test;
    >
    > for (int i = 0; i < test.length(); i++) {
    > cout << (int)test << " ";
    > }
    >
    > cout << endl;
    >
    > return 0;
    >
    > }
    >
    > If you start the programm, then press "arrow up" and then "enter", it should
    > output "27 91 65".


    Aha . Works when I compile using gcc (in Windows) but in VC7.1 hangs
    when I press arrow up) until I press an ascii character.

    Not sure whether there is a right and wrong or if its implementaion
    defined. I suspect implementation defined though.

    The one common characteristic is that you have to press return to
    flush the buffer and signal end of input AFAIK, before you can make
    anything happen.

    And to avoid that ( I think) you need to be able to monitor system
    events ( e.g in a loop looking for e.g any new key pressed etc) which
    isnt provided for in a standardised form in C++

    Again though I'm no expert on this...

    regards
    Andy Little.
     
    kwikius, Mar 2, 2007
    #5
  6. * Johannes Meng:
    >> kwikius wrote:
    >> The simplest way to try some event based programming I would guess is
    >> to use another language such as Java. C++ has ( nor probably will ever
    >> have) much interest in standardisng a GUI.

    >
    > I just noticed I might not have been clear about what I want: I do _not_
    > want to program a gui. It's all about the command line ;)


    *Dragging up memories from the eighties*

    You probably need to do input at a lower, system-specific level because
    you need a timeout.

    You should check whether there's some library that translates escape
    sequences to something more digestible, not sure if 'curses' does that.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Mar 2, 2007
    #6
  7. kwikius wrote:

    > Aha . Works when I compile using gcc (in Windows) but in VC7.1 hangs
    > when I press arrow up) until I press an ascii character.
    >
    > Not sure whether there is a right and wrong or if its implementaion
    > defined. I suspect implementation defined though.
    >
    > The one common characteristic is that you have to press return to
    > flush the buffer and signal end of input AFAIK, before you can make
    > anything happen.
    >
    > And to avoid that ( I think) you need to be able to monitor system
    > events ( e.g in a loop looking for e.g any new key pressed etc) which
    > isnt provided for in a standardised form in C++
    >
    > Again though I'm no expert on this...


    Thanks for testing! Well, I use unbuffered input, thus there's no need to
    press enter. That works using some stuff from termios.h, which is most
    probably linux/unix(?) specific, though.

    --
    Johannes Meng
     
    Johannes Meng, Mar 2, 2007
    #7
  8. Alf P. Steinbach wrote:

    > *Dragging up memories from the eighties*


    Hehe. That's about where I'm going ;P

    >
    > You probably need to do input at a lower, system-specific level because
    > you need a timeout.
    >
    > You should check whether there's some library that translates escape
    > sequences to something more digestible, not sure if 'curses' does that.


    The thing is this: I'm currently coding an input prompt with bash-like
    editing capabilities, history and so on. It's almost functional and really,
    really small and I'd rather it didn't have any external dependencies. I'll
    try looking for a suitable library anyways.

    Thank you!

    --
    Johannes Meng
     
    Johannes Meng, Mar 2, 2007
    #8
  9. Johannes Meng

    kwikius Guest

    On 2 Mar, 17:05, Johannes Meng <> wrote:
    > kwikius wrote:
    > > Aha . Works when I compile using gcc (in Windows) but in VC7.1 hangs
    > > when I press arrow up) until I press an ascii character.

    >
    > > Not sure whether there is a right and wrong or if its implementaion
    > > defined. I suspect implementation defined though.

    >
    > > The one common characteristic is that you have to press return to
    > > flush the buffer and signal end of input AFAIK, before you can make
    > > anything happen.

    >
    > > And to avoid that ( I think) you need to be able to monitor system
    > > events ( e.g in a loop looking for e.g any new key pressed etc) which
    > > isnt provided for in a standardised form in C++

    >
    > > Again though I'm no expert on this...

    >
    > Thanks for testing! Well, I use unbuffered input, thus there's no need to
    > press enter. That works using some stuff from termios.h, which is most
    > probably linux/unix(?) specific, though.


    Yes look like a Unix header to me:

    http://www.opengroup.org/onlinepubs/007908799/xsh/termios.h.html

    regards
    Andy Little
     
    kwikius, Mar 2, 2007
    #9
    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,108
    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,177
    Kevin Saff
    Jan 6, 2004
  3. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,700
    Alex Vinokur
    Mar 6, 2005
  4. puzzlecracker
    Replies:
    3
    Views:
    1,774
    Mike Wahler
    May 8, 2006
  5. Sanyi
    Replies:
    17
    Views:
    837
    Sanyi
    Feb 24, 2008
Loading...

Share This Page