Two i/o questions

Discussion in 'C++' started by Isliguezze, Jun 25, 2008.

  1. Isliguezze

    Isliguezze Guest

    How do I do in C++ (!) this operation (usin C99 stdlib.h):

    fflush(stdin);

    And how do I make something like "Press any key to continue..." in
    standard way? I mean if I use getchar() (again from C99), if requires
    pressing <Enter> key. How do I do it without pressing <Enter>? Any
    unbuffered standard C++ variant? Maybe some std::cin member function?
    Isliguezze, Jun 25, 2008
    #1
    1. Advertising

  2. Isliguezze

    Stefan Ram Guest

    Isliguezze <> writes:
    >How do I do in C++ (!) this operation (usin C99 stdlib.h):
    >fflush(stdin);


    You want to obtain undefined behavior?

    »If stream points to an output stream or an update stream
    in which the most recent operation was not input, the
    fflush function causes any unwritten data for that stream
    to be delivered to the host environment to be written to
    the file; otherwise, the behavior is undefined.«

    ISO/IEC 9899:1999 (E), 7.19.5.2p2

    >And how do I make something like "Press any key to continue..." in
    >standard way? I mean if I use getchar() (again from C99), if requires
    >pressing <Enter> key. How do I do it without pressing <Enter>? Any
    >unbuffered standard C++ variant? Maybe some std::cin member function?


    Usually the problem is not a buffer in the program, but in the
    text console used. This is intended, so that the user always
    can use backspace to undo the last key. Usually, it can not be
    turned off from a program running in the text console. But it
    might be possible using a window of GUI toolkit. Also, some
    text consoles can be switched into a »raw mode«.
    Stefan Ram, Jun 25, 2008
    #2
    1. Advertising

  3. Isliguezze

    Jerry Coffin Guest

    In article <685c8634-bcfd-4f7e-b869-0d0e5c01dc20
    @m73g2000hsh.googlegroups.com>, says...
    > How do I do in C++ (!) this operation (usin C99 stdlib.h):
    >
    > fflush(stdin);


    This gives undefined behavior in C. There are many ways to get undefined
    behavior in C++, though I'm not sure why you'd want to do so.

    > And how do I make something like "Press any key to continue..." in
    > standard way? I mean if I use getchar() (again from C99), if requires
    > pressing <Enter> key. How do I do it without pressing <Enter>? Any
    > unbuffered standard C++ variant? Maybe some std::cin member function?


    No. C++ allows you to _view_ the I/O in different ways, but the basic
    requirements it makes of the underlying system are essentially identical
    to those of C.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Jun 25, 2008
    #3
  4. Isliguezze

    Daniel Pitts Guest

    Isliguezze wrote:
    > How do I do in C++ (!) this operation (usin C99 stdlib.h):
    >
    > fflush(stdin);

    What does flusing stdin give you? flushing will cause buffered data to
    be pushed out.
    >
    > And how do I make something like "Press any key to continue..." in
    > standard way? I mean if I use getchar() (again from C99), if requires
    > pressing <Enter> key. How do I do it without pressing <Enter>? Any
    > unbuffered standard C++ variant? Maybe some std::cin member function?

    It is implementation specific. I remember in Borland C++ 3.1, there was
    a getch() function that did what you want. Its better to just say "Press
    enter to continue..." and leave it at that if you want portability.


    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Jun 25, 2008
    #4
  5. Isliguezze

    osmium Guest

    "Isliguezze" wrote:

    > How do I do in C++ (!) this operation (usin C99 stdlib.h):
    >
    > fflush(stdin);


    To clear the input buffer maintained by the system, see istream::ignore().
    You may have to precede it with ios::clear() because the stream is in a fail
    state and it will not listen to any command but clear. There are ways to see
    if the stream is in a fail state.

    > And how do I make something like "Press any key to continue..." in
    > standard way? I mean if I use getchar() (again from C99), if requires
    > pressing <Enter> key. How do I do it without pressing <Enter>? Any
    > unbuffered standard C++ variant? Maybe some std::cin member function?


    The only ways I know of doing "Press any key" are non-standard. Only
    Microsoft would think that the up arrow key and it's cousins was not a key,
    so the message was wrong from the get-go. Easiest way out is to use "Press
    enter" and get rid of any undesired input.
    osmium, Jun 25, 2008
    #5
  6. Isliguezze

    Isliguezze Guest

    You see, I need to fflush() because I want to "Press any key to
    continue..." If there's something inside the buffer, that function
    used to halt the screen reads that and screen dies quickly... Of
    course, in Win/DOS I can use system("PAUSE");, In good old :) Borland
    3.1 getch() or getche(), is there a portable(standard) C++ function to
    do that?
    Isliguezze, Jun 25, 2008
    #6
  7. Isliguezze

    Default User Guest

    Isliguezze wrote:

    > You see, I need to fflush() because I want to "Press any key to
    > continue..." If there's something inside the buffer, that function
    > used to halt the screen reads that and screen dies quickly...


    You can't get fflush() to do that portably. It doesn't really matter,
    because you can't get the other thing either. You need to find a
    platform-specific group for your questions.





    Brian
    Default User, Jun 25, 2008
    #7
  8. Isliguezze

    James Kanze Guest

    On Jun 25, 10:45 pm, "Default User" <> wrote:
    > Isliguezze wrote:
    > > You see, I need to fflush() because I want to "Press any key
    > > to continue..." If there's something inside the buffer, that
    > > function used to halt the screen reads that and screen dies
    > > quickly...


    > You can't get fflush() to do that portably. It doesn't really
    > matter, because you can't get the other thing either. You need
    > to find a platform-specific group for your questions.


    Yes and no. Curses (or ncurses) is a pretty portable library,
    available for many platforms; if you're trying to do anything
    fancy in a classical "tty" window (console window under Windows,
    xterm or whatever under Unix, etc.), then it's surely the way to
    go.

    Of course, if all you really want is to block output until the
    user has acknowledged reading it: "Press enter to continue..."
    and std::getline() are the simplest solutions.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 26, 2008
    #8
  9. Isliguezze

    James Kanze Guest

    On Jun 25, 6:37 pm, "osmium" <> wrote:
    > "Isliguezze" wrote:


    [...]
    > The only ways I know of doing "Press any key" are
    > non-standard. Only Microsoft would think that the up arrow
    > key and it's cousins was not a key, so the message was wrong
    > from the get-go.


    I don't know about the up arrow key, but I've yet to see a
    program which responded to the control key or the shift key in
    such cases.

    (Back in the old days, before anyone had even heard of the term
    "computer literacy", you'd occasionally see such messages in
    demo programs at technical fairs. At which point, I'd call one
    of the sales droids over, and point out that the program wasn't
    working by hitting the control key. Nine times out of ten, the
    sales droid would then go off to find some technical support
    person to fix it.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 26, 2008
    #9
  10. Isliguezze

    Daniel Pitts Guest

    James Kanze wrote:
    > On Jun 25, 6:37 pm, "osmium" <> wrote:
    >> "Isliguezze" wrote:

    >
    > [...]
    >> The only ways I know of doing "Press any key" are
    >> non-standard. Only Microsoft would think that the up arrow
    >> key and it's cousins was not a key, so the message was wrong
    >> from the get-go.

    >
    > I don't know about the up arrow key, but I've yet to see a
    > program which responded to the control key or the shift key in
    > such cases.
    >
    > (Back in the old days, before anyone had even heard of the term
    > "computer literacy", you'd occasionally see such messages in
    > demo programs at technical fairs. At which point, I'd call one
    > of the sales droids over, and point out that the program wasn't
    > working by hitting the control key. Nine times out of ten, the
    > sales droid would then go off to find some technical support
    > person to fix it.)

    I've actually written programs that did handle literally ANY key press.
    More trouble than it was worth, and not portable in the slightest.

    >
    > --
    > James Kanze (GABI Software) email:
    > Conseils en informatique orientée objet/
    > Beratung in objektorientierter Datenverarbeitung
    > 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34



    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Jun 26, 2008
    #10
  11. Isliguezze

    Daniel Pitts Guest

    Paavo Helde wrote:
    > Daniel Pitts <> kirjutas:
    >> I've actually written programs that did handle literally ANY key press.
    >> More trouble than it was worth, and not portable in the slightest.

    >
    > Just for curiosity - did this include the Del key press in a Ctrl+Alt+Del
    > combination on Windows?
    >
    > Paavo
    >

    It was a DOS program, but in any case it could have, as it implemented
    the keyboard interrupt directly, bypassing BIOS handling.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Jun 26, 2008
    #11
    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. Fangs
    Replies:
    3
    Views:
    9,738
    darshana
    Oct 26, 2008
  2. DC Gringo
    Replies:
    2
    Views:
    2,278
    Nikhil Patel
    Feb 26, 2004
  3. Mark
    Replies:
    3
    Views:
    498
    bruce barker
    Aug 26, 2004
  4. Darrel
    Replies:
    2
    Views:
    297
    darrel
    Nov 4, 2004
  5. GenxLogic
    Replies:
    3
    Views:
    1,227
    andrewmcdonagh
    Dec 6, 2006
Loading...

Share This Page