Portable way to wait for a keypress

A

Alex007

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

Darrell Grainger

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(stdin)" 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.
 
A

Alex007

Alex007 said:
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
 
K

Keith Thompson

Alex007 said:
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.
 
C

CBFalconer

Darrell said:
. snip ...

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;
}
 
K

Keith Thompson

Alex007 said:
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(__windows__)
/* Windows-specific code to wait for a keypress */
#else
#error "Unsupported 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.
 
K

Kenneth Brody

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

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,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top