473,671 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Portable way to wait for a keypress

Hi,

I've been working on this C assignement for a CS course.... the
assignement is going pretty well and all my code works well on both
Windows and Linux.

The only thing that doesn't work is a stupid function that only has to
wait for a keypress from the user (it's a CLI program).

Here's what I have :

void wait(void)
{
fflush(stdin);
printf("\nPress Enter to continue...");
getchar();
}

The fflush() is there to get rid of some leftover \n in the input queue
(from a previous scanf() for instance). It works under Windows (the
fflush(stdin) works), but it doesn't under Linux. And I already know
that "fflush(std in)" is wrong (fflush() is supposed to be for output
buffers, not input... or something like that).

So what do you gurus think ? What would be a right/portable way to wait
for a keypress (any key if possible, not just ENTER) and be sure that no
"leftover" \n will jinks it ?

Thanks for your help !

Alex....
Nov 14 '05 #1
7 14851
Alex007 wrote:
So what do you gurus think ? What would be a right/portable way to wait
for a keypress (any key if possible, not just ENTER) and be sure that no
"leftover" \n will jinks it ?


Dammit... just read the faq... seems there is no quick/easy way outta
this...

I've got some 2000+ lines of code working perfectly windows/linux and
this is the one is giving me a headache :D
Nov 14 '05 #2
Alex007 <adery_re_move_ @really_hotmail .com> writes:
[...]
void wait(void)
{
fflush(stdin);
printf("\nPress Enter to continue...");
getchar();
}


The C FAQ is at <http://www.eskimo.com/~scs/C-faq/top.html>.

fflush(stdin) invokes undefined behavior; see question 12.26.

There is no portable way to read a single character from the keyboard
without waiting for the end of a line; see question 19.1. (Input may
be line-buffered; there's no portable way to turn this buffering off.)

But since your prompt is "Press Enter to continue...", you probably
want to read and discard everything up to the end of a line (i.e.,
until the user presses Enter).

Output may also be line-buffered, but you can override this with
fflush(stdout), which you should do immediately after the printf call.
Follow this with a loop that reads characters from stdin, terminating
when it sees a newline ('\n') character.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
On Mon, 14 Jun 2004, Alex007 wrote:
Hi,

I've been working on this C assignement for a CS course.... the
assignement is going pretty well and all my code works well on both
Windows and Linux.

The only thing that doesn't work is a stupid function that only has to
wait for a keypress from the user (it's a CLI program).

Here's what I have :

void wait(void)
{
fflush(stdin);
printf("\nPress Enter to continue...");
getchar();
}

The fflush() is there to get rid of some leftover \n in the input queue
(from a previous scanf() for instance). It works under Windows (the
fflush(stdin) works), but it doesn't under Linux. And I already know
that "fflush(std in)" is wrong (fflush() is supposed to be for output
buffers, not input... or something like that).

So what do you gurus think ? What would be a right/portable way to wait
for a keypress (any key if possible, not just ENTER) and be sure that no
"leftover" \n will jinks it ?
The only portable way for this to work is to make sure that all other
input gathering does not leave behind unwanted data on the input stream.
Otherwise you cannot have a portable way of handling this.
Thanks for your help !

Alex....


--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@ whitehouse.gov
Nov 14 '05 #4
Alex007 wrote:

The only thing that doesn't work is a stupid function that only has to
wait for a keypress from the user (it's a CLI program).


See the FAQ:

http://www.eskimo.com/~scs/C-faq/q19.1.html

--
Thomas M. Sommers -- tm*@nj.net -- AB2SB

Nov 14 '05 #5
Darrell Grainger wrote:
On Mon, 14 Jun 2004, Alex007 wrote:

.... snip ...

So what do you gurus think ? What would be a right/portable way
to wait for a keypress (any key if possible, not just ENTER) and
be sure that no "leftover" \n will jinks it ?


The only portable way for this to work is to make sure that all
other input gathering does not leave behind unwanted data on the
input stream. Otherwise you cannot have a portable way of
handling this.


Or alternatively to ensure all other input routines leave at least
a '\n' in the input stream. Then you can use:

int flushln(FILE *f) {
int ch;

while ((EOF != (ch = getc(f))) && ('\n' != ch)) continue;
return ch;
}

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #6
Alex007 <adery_re_move_ @really_hotmail .com> writes:
Alex007 wrote:
So what do you gurus think ? What would be a right/portable way to
wait for a keypress (any key if possible, not just ENTER) and be
sure that no "leftover" \n will jinks it ?


Dammit... just read the faq... seems there is no quick/easy way outta
this...

I've got some 2000+ lines of code working perfectly windows/linux and
this is the one is giving me a headache :D


There's probably a system-specific way to do this under Windows, and
another system-specific way to do it under Linux. Check the
documentation for each system (or, failing that, ask in a
system-specific newsgroup for each system). Then use #if or #ifdef to
select the appropriate code for each system.

Something like this:

#if defined(__linux __)
/* Linux-specific code to wait for a keypress */
#elif defined(__windo ws__)
/* Windows-specific code to wait for a keypress */
#else
#error "Unsupporte d system"
#endif

I don't know whether the symbol __windows__ is defined on Windows
systems.

You'll probably also need to use #ifdef to select system-specific
headers.

Or you might consider waiting for the enter key, rather than for any
keypress; you can do that portably.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7
Alex007 wrote:

Alex007 wrote:
So what do you gurus think ? What would be a right/portable way to wait
for a keypress (any key if possible, not just ENTER) and be sure that no
"leftover" \n will jinks it ?


Dammit... just read the faq... seems there is no quick/easy way outta
this...

I've got some 2000+ lines of code working perfectly windows/linux and
this is the one is giving me a headache :D


Nothing says you are forbidden from ever using an implementation-specific
feature, does it?

Isolate this "wait for a keypress" routine as a single function, called as
needed, and simply write a different version for each platform you need to
support.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody at spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Nov 14 '05 #8

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

Similar topics

22
2252
by: SeeBelow | last post by:
Is there any way, in C, of interacting with a running program, but still using code that is portable, at least between linux and Windows? By "interacting" it could be something as simple as detecting a key board hit so that it could invoke scanf(). So a portable key board data available routine would satisfy my minimum requirements. But more generally I want to know if one can write portable C code that allows a user to change the...
5
2388
by: Kobu | last post by:
In embedded systems (programmed in C), often times structure declarations are used to group together several status/control/data registers of external hardware (or even internal registers). The example below (from GBD's THE C BOOK) uses this. Is this a portable method? . /*
1
6170
by: Rene | last post by:
Hi, I am running is some problems with the KeyPreview and KeyPress events. The KeyPress event is only triggered when there this an focusable control on the form. When all controls are disabled the Form.KeyPress event does not trigger anymore. A way to 'solve' this is to set the focus to the form, but during testing it happens ofter the keypress event is not getting triggered on keys like
4
4524
by: Tom | last post by:
I have a VB.NET user control that I wrote - this control has three or four other controls on it (textbox, combobox, datetime picker, etc). Now, whenever the control is on a form and the user enters anything into the textbox (for instance) I trap the keypress event to handle some stuff (i.e. if it is an enter key, etc). Now, once I am done with handling the keypress event for the textbox, I then need to pass that keypress event BACK to the...
3
6852
by: Fia | last post by:
Hi In Visual Basic 6 I could call keypress with an integer of a choosen key. For example I could call a textbox's keypress with the number 13 (for the enter key). But now in Visual Basic .Net I don't know how I shall do to do the same thing. I have tried to send Keys.Numpad8, but then it complains about it cannot convert from Keys to keyEventArgs I have also tried to send the number 8, but then it complains it cannot convert from Integer...
131
6110
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write portable C code - *not only* because, in a 'C sense', I
1
5542
by: TheOneRedDragon | last post by:
At the moment, I have a command-line application which prints out quite a lot of text to the screen. However, when Windows users run it, the prompt disappears before they can read any of it. Is there any simple way to make a script wait for a keypress before completing? Thanks for any help.
4
2369
by: Brendan Miller | last post by:
I want to spawn a child process based on an external executable that I have the path for. I then want to wait on that executable, and capture it's output. In the os module, fork is only supported on unix, but spawn is only supported on windows. The os.system call is implemented by calling the C system call, which is of course inefficient and has portability gotchas because it calls the underlying system shell (sh for unix, cmd for...
23
3328
by: asit | last post by:
what is the difference between portable C, posix C and windows C ???
0
8390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8909
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8667
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7428
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4399
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1801
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.