Two i/o questions

I

Isliguezze

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?
 
S

Stefan Ram

Isliguezze said:
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«.
 
J

Jerry Coffin

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.
 
D

Daniel Pitts

Isliguezze said:
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.
 
O

osmium

Isliguezze said:
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.
 
I

Isliguezze

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?
 
D

Default User

Isliguezze said:
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
 
J

James Kanze

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.
 
J

James Kanze

"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.)
 
D

Daniel Pitts

James said:
:
[...]
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.
 
D

Daniel Pitts

Paavo said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top