Two i/o questions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Isliguezze

    Two i/o questions

    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 <Enterkey. How do I do it without pressing <Enter>? Any
    unbuffered standard C++ variant? Maybe some std::cin member function?
  • Jerry Coffin

    #2
    Re: Two i/o questions

    In article <685c8634-bcfd-4f7e-b869-0d0e5c01dc20
    @m73g2000hsh.go oglegroups.com> , isliguezze@gmai l.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 <Enterkey. 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.

    Comment

    • Daniel Pitts

      #3
      Re: Two i/o questions

      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 <Enterkey. 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/>

      Comment

      • osmium

        #4
        Re: Two i/o questions

        "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 <Enterkey. 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.


        Comment

        • Isliguezze

          #5
          Re: Two i/o questions

          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(standa rd) C++ function to
          do that?

          Comment

          • Default User

            #6
            Re: Two i/o questions

            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

            Comment

            • James Kanze

              #7
              Re: Two i/o questions

              On Jun 25, 10:45 pm, "Default User" <defaultuse...@ yahoo.comwrote:
              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:james.kan ze@gmail.com
              Conseils en informatique orientée objet/
              Beratung in objektorientier ter Datenverarbeitu ng
              9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

              Comment

              • James Kanze

                #8
                Re: Two i/o questions

                On Jun 25, 6:37 pm, "osmium" <r124c4u...@com cast.netwrote:
                "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:james.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                • Daniel Pitts

                  #9
                  Re: Two i/o questions

                  James Kanze wrote:
                  On Jun 25, 6:37 pm, "osmium" <r124c4u...@com cast.netwrote:
                  >"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:james.kan ze@gmail.com
                  Conseils en informatique orientée objet/
                  Beratung in objektorientier ter Datenverarbeitu ng
                  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/>

                  Comment

                  • Paavo Helde

                    #10
                    Re: Two i/o questions

                    Daniel Pitts <newsgroup.spam filter@virtuali nfinity.netkirj utas:
                    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

                    Comment

                    • Daniel Pitts

                      #11
                      Re: Two i/o questions

                      Paavo Helde wrote:
                      Daniel Pitts <newsgroup.spam filter@virtuali nfinity.netkirj utas:
                      >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/>

                      Comment

                      Working...