Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Two i/o questions

Reply
Thread Tools

Two i/o questions

 
 
Isliguezze
Guest
Posts: n/a
 
      06-25-2008
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?
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      06-25-2008
Isliguezze <(E-Mail Removed)> 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«.

 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      06-25-2008
In article <685c8634-bcfd-4f7e-b869-0d0e5c01dc20
@m73g2000hsh.googlegroups.com>, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      06-25-2008
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/>
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      06-25-2008
"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.


 
Reply With Quote
 
Isliguezze
Guest
Posts: n/a
 
      06-25-2008
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?
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      06-25-2008
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-26-2008
On Jun 25, 10:45 pm, "Default User" <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-26-2008
On Jun 25, 6:37 pm, "osmium" <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      06-26-2008
James Kanze wrote:
> On Jun 25, 6:37 pm, "osmium" <(E-Mail Removed)> 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:(E-Mail Removed)
> 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/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM
Two ISP -Two Routers - 1 PIX James Parks Cisco 5 12-11-2003 08:55 PM
Cisco 2611 with Two T1 from two ISPs Adam Embrey Cisco 3 07-24-2003 10:22 PM



Advertisments