473,379 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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?
Jun 27 '08 #1
10 1188
In article <685c8634-bcfd-4f7e-b869-0d0e5c01dc20
@m73g2000hsh.googlegroups.com>, is********@gmail.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.
Jun 27 '08 #2
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/>
Jun 27 '08 #3
"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.
Jun 27 '08 #4
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?
Jun 27 '08 #5
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
Jun 27 '08 #6
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:ja*********@gmail.com
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
Jun 27 '08 #7
On Jun 25, 6:37 pm, "osmium" <r124c4u...@comcast.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:ja*********@gmail.com
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
Jun 27 '08 #8
James Kanze wrote:
On Jun 25, 6:37 pm, "osmium" <r124c4u...@comcast.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:ja*********@gmail.com
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/>
Jun 27 '08 #9
Daniel Pitts <ne******************@virtualinfinity.netkirjuta s:
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

Jun 27 '08 #10
Paavo Helde wrote:
Daniel Pitts <ne******************@virtualinfinity.netkirjuta s:
>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/>
Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for...
0
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
4
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
8
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
1
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.